FileStorage.php

Definition of Drupal\Component\PhpStorage\FileStorage.

Namespace

Drupal\Component\PhpStorage

File

drupal/core/lib/Drupal/Component/PhpStorage/FileStorage.php
View source
<?php

/**
 * @file
 * Definition of Drupal\Component\PhpStorage\FileStorage.
 */
namespace Drupal\Component\PhpStorage;


/**
 * Stores the code as regular PHP files.
 */
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;
  }

}

Classes

Namesort descending Description
FileStorage Stores the code as regular PHP files.