FileStorage.php 3.95 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Config;

5 6
use Symfony\Component\Yaml\Yaml;

7
/**
8 9 10 11
 * Represents the file storage controller.
 *
 * @todo Implement StorageInterface after removing DrupalConfig methods.
 * @todo Consider to extend StorageBase.
12
 */
13
class FileStorage {
14 15

  /**
16
   * The name of the configuration object.
17
   *
18 19 20 21 22 23
   * @var string
   */
  protected $name;

  /**
   * The filesystem path containing the configuration object.
24
   *
25
   * @var string
26
   */
27
  protected $path;
28 29

  /**
30 31 32 33 34 35 36 37 38 39 40
   * Implements StorageInterface::__construct().
   */
  public function __construct($name = NULL) {
    $this->name = $name;
  }

  /**
   * Returns the path containing the configuration file.
   *
   * @return string
   *   The relative path to the configuration object.
41
   */
42 43 44 45 46
  public function getPath() {
    // If the path has not been set yet, retrieve and assign the default path
    // for configuration files.
    if (!isset($this->path)) {
      $this->setPath(config_get_config_directory());
47
    }
48 49 50 51 52 53 54 55 56
    return $this->path;
  }

  /**
   * Sets the path containing the configuration file.
   */
  public function setPath($directory) {
    $this->path = $directory;
    return $this;
57 58 59
  }

  /**
60
   * Returns the path to the configuration file.
61
   *
62 63
   * @return string
   *   The path to the configuration file.
64
   */
65 66
  public function getFilePath() {
    return $this->getPath() . '/' . $this->getName() . '.' . self::getFileExtension();
67 68 69 70 71 72 73 74 75 76
  }

  /**
   * Returns the file extension used by the file storage for all configuration files.
   *
   * @return string
   *   The file extension.
   */
  public static function getFileExtension() {
    return 'yml';
77 78 79
  }

  /**
80
   * Returns whether the configuration file exists.
81
   *
82 83 84
   * @return bool
   *   TRUE if the configuration file exists, FALSE otherwise.
   */
85 86
  protected function exists() {
    return file_exists($this->getFilePath());
87 88 89
  }

  /**
90
   * Implements StorageInterface::write().
91
   *
92
   * @throws FileStorageException
93
   */
94 95 96 97
  public function write($data) {
    $data = $this->encode($data);
    if (!file_put_contents($this->getFilePath(), $data)) {
      throw new FileStorageException('Failed to write configuration file: ' . $this->getFilePath());
98
    }
99 100 101
  }

  /**
102
   * Implements StorageInterface::read().
103
   *
104
   * @throws FileStorageReadException
105
   */
106 107 108
  public function read() {
    if (!$this->exists()) {
      throw new FileStorageReadException("Configuration file '$this->name' does not exist.");
109
    }
110 111 112 113 114 115 116

    $data = file_get_contents($this->getFilePath());
    $data = $this->decode($data);
    if ($data === FALSE) {
      throw new FileStorageReadException("Failed to decode configuration file '$this->name'.");
    }
    return $data;
117 118 119
  }

  /**
120
   * Deletes a configuration file.
121
   */
122 123 124
  public function delete() {
    // Needs error handling and etc.
    @drupal_unlink($this->getFilePath());
125
  }
126 127

  /**
128
   * Implements StorageInterface::encode().
129 130
   */
  public static function encode($data) {
131 132 133
    // The level where you switch to inline YAML is set to PHP_INT_MAX to ensure
    // this does not occur.
    return Yaml::dump($data, PHP_INT_MAX);
134 135 136
  }

  /**
137
   * Implements StorageInterface::decode().
138 139 140 141 142
   */
  public static function decode($raw) {
    if (empty($raw)) {
      return array();
    }
143 144 145 146
    return Yaml::parse($raw);
  }

  /**
147
   * Implements StorageInterface::getName().
148
   */
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  public function getName() {
    return $this->name;
  }

  /**
   * Implements StorageInterface::setName().
   */
  public function setName($name) {
    $this->name = $name;
  }

  /**
   * Implements StorageInterface::getNamesWithPrefix().
   */
  public static function getNamesWithPrefix($prefix = '') {
    // @todo Use $this->getPath() to allow for contextual search of files in
    //   custom paths.
    $files = glob(config_get_config_directory() . '/' . $prefix . '*.' . FileStorage::getFileExtension());
    $clean_name = function ($value) {
      return basename($value, '.' . FileStorage::getFileExtension());
169 170
    };
    return array_map($clean_name, $files);
171
  }
172
}