FileStorage.php 4.37 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
  public function read($name) {
    if (!$this->exists($name)) {
73
      return FALSE;
74 75 76 77 78 79
    }
    $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);
    return $data;
80 81 82
  }

  /**
83
   * Implements Drupal\Core\Config\StorageInterface::write().
84
   *
85 86
   * @throws Symfony\Component\Yaml\Exception\DumpException
   * @throws Drupal\Core\Config\StorageException
87
   */
88 89 90 91 92
  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));
93
    }
94
    return TRUE;
95 96 97
  }

  /**
98
   * Implements Drupal\Core\Config\StorageInterface::delete().
99
   */
100 101 102 103 104 105 106 107
  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));
108
  }
109

110
  /**
111
   * Implements Drupal\Core\Config\StorageInterface::rename().
112 113 114 115 116 117 118 119 120
   */
  public function rename($name, $new_name) {
    $status = @rename($this->getFilePath($name), $this->getFilePath($new_name));
    if ($status === FALSE) {
      throw new StorageException('Failed to rename configuration file from: ' . $this->getFilePath($name) . ' to: ' . $this->getFilePath($new_name));
    }
    return TRUE;
  }

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

  /**
133 134 135
   * Implements Drupal\Core\Config\StorageInterface::decode().
   *
   * @throws Symfony\Component\Yaml\Exception\ParseException
136 137
   */
  public static function decode($raw) {
138 139 140 141
    $data = Yaml::parse($raw);
    // A simple string is valid YAML for any reason.
    if (!is_array($data)) {
      return FALSE;
142
    }
143
    return $data;
144 145 146
  }

  /**
147
   * Implements Drupal\Core\Config\StorageInterface::listAll().
148
   */
149 150 151 152 153 154 155 156 157 158
  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);
159 160
    };
    return array_map($clean_name, $files);
161
  }
162
}