FileStorage.php 4.06 KB
Newer Older
1 2
<?php

3 4 5 6 7
/**
 * @file
 * Definition of Drupal\Core\Config\FileStorage.
 */

8 9
namespace Drupal\Core\Config;

10 11
use Symfony\Component\Yaml\Yaml;

12
/**
13
 * Defines the file storage controller.
14
 */
15
class FileStorage implements StorageInterface {
16 17

  /**
18
   * Configuration options for this storage controller.
19
   *
20
   * - directory: The filesystem path for configuration objects.
21
   *
22
   * @var array
23
   */
24
  protected $options;
25 26

  /**
27
   * Implements Drupal\Core\Config\StorageInterface::__construct().
28
   */
29 30 31
  public function __construct(array $options = array()) {
    if (!isset($options['directory'])) {
      $options['directory'] = config_get_config_directory();
32
    }
33
    $this->options = $options;
34 35 36
  }

  /**
37
   * Returns the path to the configuration file.
38
   *
39 40
   * @return string
   *   The path to the configuration file.
41
   */
42 43
  public function getFilePath($name) {
    return $this->options['directory'] . '/' . $name . '.' . self::getFileExtension();
44 45 46 47 48 49 50 51 52 53
  }

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

  /**
57
   * Returns whether the configuration file exists.
58
   *
59 60 61
   * @return bool
   *   TRUE if the configuration file exists, FALSE otherwise.
   */
62 63
  public function exists($name) {
    return file_exists($this->getFilePath($name));
64 65 66
  }

  /**
67
   * Implements Drupal\Core\Config\StorageInterface::read().
68
   *
69
   * @throws Symfony\Component\Yaml\Exception\ParseException
70
   */
71 72 73 74 75 76 77 78 79 80
  public function read($name) {
    if (!$this->exists($name)) {
      return array();
    }
    $data = file_get_contents($this->getFilePath($name));
    // @todo Yaml throws a ParseException on invalid data. Is it expected to be
    //   caught or not?
    $data = $this->decode($data);
    if ($data === FALSE) {
      return array();
81
    }
82 83 84 85 86
    // A simple string is valid YAML for any reason.
    if (!is_array($data)) {
      return array();
    }
    return $data;
87 88 89
  }

  /**
90
   * Implements Drupal\Core\Config\StorageInterface::write().
91
   *
92 93
   * @throws Symfony\Component\Yaml\Exception\DumpException
   * @throws Drupal\Core\Config\StorageException
94
   */
95 96 97 98 99
  public function write($name, array $data) {
    $data = $this->encode($data);
    $status = @file_put_contents($this->getFilePath($name), $data);
    if ($status === FALSE) {
      throw new StorageException('Failed to write configuration file: ' . $this->getFilePath($name));
100
    }
101
    return TRUE;
102 103 104
  }

  /**
105
   * Implements Drupal\Core\Config\StorageInterface::delete().
106
   */
107 108 109 110 111 112 113 114
  public function delete($name) {
    if (!$this->exists($name)) {
      if (!file_exists($this->options['directory'])) {
        throw new StorageException($this->options['directory'] . '/ not found.');
      }
      return FALSE;
    }
    return drupal_unlink($this->getFilePath($name));
115
  }
116 117

  /**
118 119 120
   * Implements Drupal\Core\Config\StorageInterface::encode().
   *
   * @throws Symfony\Component\Yaml\Exception\DumpException
121 122
   */
  public static function encode($data) {
123 124 125
    // 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);
126 127 128
  }

  /**
129 130 131
   * Implements Drupal\Core\Config\StorageInterface::decode().
   *
   * @throws Symfony\Component\Yaml\Exception\ParseException
132 133 134 135 136
   */
  public static function decode($raw) {
    if (empty($raw)) {
      return array();
    }
137 138 139 140
    return Yaml::parse($raw);
  }

  /**
141
   * Implements Drupal\Core\Config\StorageInterface::listAll().
142
   */
143 144 145 146 147 148 149 150 151 152
  public function listAll($prefix = '') {
    // glob() silently ignores the error of a non-existing search directory,
    // even with the GLOB_ERR flag.
    if (!file_exists($this->options['directory'])) {
      throw new StorageException($this->options['directory'] . '/ not found.');
    }
    $extension = '.' . self::getFileExtension();
    $files = glob($this->options['directory'] . '/' . $prefix . '*' . $extension);
    $clean_name = function ($value) use ($extension) {
      return basename($value, $extension);
153 154
    };
    return array_map($clean_name, $files);
155
  }
156
}