You are here

public function MTimeProtectedFastFileStorage::save in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::save()

Saves PHP code to storage.

Parameters

string $name: The virtual file name. Can be a relative path.

string $code: The PHP code to be saved.

Return value

bool TRUE if the save succeeded, FALSE if it failed.

Overrides FileStorage::save

File

core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php, line 68
Contains \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage.

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;
}