class FileStorage

Same name in this branch

Stores the code as regular PHP files.

Hierarchy

Expanded class hierarchy of FileStorage

File

drupal/core/lib/Drupal/Component/PhpStorage/FileStorage.php, line 13
Definition of Drupal\Component\PhpStorage\FileStorage.

Namespace

Drupal\Component\PhpStorage
View source
class FileStorage implements PhpStorageInterface {

  /**
   * The directory where the files should be stored.
   *
   * @var string
   */
  protected $directory;

  /**
   * Constructs this FileStorage object.
   *
   * @param array $configuration
   *   An associative array, containing at least these two keys:
   *   - directory: The directory where the files should be stored.
   *   - bin: The storage bin. Multiple storage objects can be instantiated with
   *     the same configuration, but for different bins..
   */
  public function __construct(array $configuration) {
    $this->directory = $configuration['directory'] . '/' . $configuration['bin'];
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::exists().
   */
  public function exists($name) {
    return file_exists($this
      ->getFullPath($name));
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::load().
   */
  public function load($name) {

    // The FALSE returned on failure is enough for the caller to handle this,
    // we do not want a warning too.
    return @(include_once $this
      ->getFullPath($name)) !== FALSE;
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::save().
   */
  public function save($name, $code) {
    $path = $this
      ->getFullPath($name);
    $dir = dirname($path);
    if (!file_exists($dir)) {
      mkdir($dir, 0700, TRUE);
    }
    return (bool) file_put_contents($path, $code);
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::delete().
   */
  public function delete($name) {
    $path = $this
      ->getFullPath($name);
    if (file_exists($path)) {
      return $this
        ->unlink($path);
    }
    return FALSE;
  }

  /**
   * Returns the full path where the file is or should be stored.
   */
  protected function getFullPath($name) {
    return $this->directory . '/' . $name;
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::writeable().
   */
  public function writeable() {
    return TRUE;
  }

  /**
   * Implements Drupal\Component\PhpStorage\PhpStorageInterface::deleteAll().
   */
  public function deleteAll() {
    return $this
      ->unlink($this->directory);
  }

  /**
   * Deletes files and/or directories in the specified path.
   *
   * If the specified path is a directory the method will
   * call itself recursively to process the contents. Once the contents have
   * been removed the directory will also be removed.
   *
   * @param string $path
   *   A string containing either a file or directory path.
   *
   * @return boolean
   *   TRUE for success or if path does not exist, FALSE in the event of an
   *   error.
   */
  protected function unlink($path) {
    if (file_exists($path)) {

      // Ensure the file / folder is writable.
      chmod($path, 0700);
      if (is_dir($path)) {
        $dir = dir($path);
        while (($entry = $dir
          ->read()) !== FALSE) {
          if ($entry == '.' || $entry == '..') {
            continue;
          }
          $this
            ->unlink($path . '/' . $entry);
        }
        $dir
          ->close();
        return @rmdir($path);
      }
      return @unlink($path);
    }

    // If there's nothing to delete return TRUE anyway.
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileStorage::$directory protected property The directory where the files should be stored.
FileStorage::delete public function Implements Drupal\Component\PhpStorage\PhpStorageInterface::delete(). Overrides PhpStorageInterface::delete 2
FileStorage::deleteAll public function Implements Drupal\Component\PhpStorage\PhpStorageInterface::deleteAll(). Overrides PhpStorageInterface::deleteAll
FileStorage::exists public function Implements Drupal\Component\PhpStorage\PhpStorageInterface::exists(). Overrides PhpStorageInterface::exists 2
FileStorage::getFullPath protected function Returns the full path where the file is or should be stored. 1
FileStorage::load public function Implements Drupal\Component\PhpStorage\PhpStorageInterface::load(). Overrides PhpStorageInterface::load 1
FileStorage::save public function Implements Drupal\Component\PhpStorage\PhpStorageInterface::save(). Overrides PhpStorageInterface::save 1
FileStorage::unlink protected function Deletes files and/or directories in the specified path.
FileStorage::writeable public function Implements Drupal\Component\PhpStorage\PhpStorageInterface::writeable(). Overrides PhpStorageInterface::writeable
FileStorage::__construct public function Constructs this FileStorage object. 2