function MTimeProtectedFastFileStorage::save

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::save()
  2. 8.9.x core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::save()
  3. 11.x core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::save()

Overrides FileStorage::save

File

core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php, line 65

Class

MTimeProtectedFastFileStorage
Stores PHP code in files with securely hashed names.

Namespace

Drupal\Component\PhpStorage

Code

public function save($name, $data) {
  $this->ensureDirectory($this->directory);
  // Write the file out to a temporary location. Prepend with a '.' to keep it
  // hidden from listings and web servers.
  $temporary_path = $this->tempnam($this->directory, '.');
  if (!$temporary_path || !@file_put_contents($temporary_path, $data)) {
    return FALSE;
  }
  // The file will not be chmod() in the future so this is the final
  // permission.
  chmod($temporary_path, 0444);
  // Determine the exact modification time of the file.
  $mtime = $this->getUncachedMTime($temporary_path);
  // Move the temporary file into the proper directory. Note that POSIX
  // compliant systems as well as modern Windows perform the rename operation
  // atomically, i.e. there is no point at which another process attempting to
  // access the new path will find it missing.
  $directory = $this->getContainingDirectoryFullPath($name);
  $this->ensureDirectory($directory);
  $full_path = $this->getFullPath($name, $directory, $mtime);
  $result = rename($temporary_path, $full_path);
  // Finally reset the modification time of the directory to match the one of
  // the newly created file. In order to prevent the creation of a file if the
  // directory does not exist, ensure that the path terminates with a
  // directory separator.
  //
  // Recall that when subsequently loading the file, the hash is calculated
  // based on the file name, the containing mtime, and a the secret string.
  // Hence updating the mtime here is comparable to pointing a symbolic link
  // at a new target, i.e., the newly created file.
  if ($result) {
    $result &= touch($directory . '/', $mtime);
  }
  return (bool) $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.