InstallStorage.php 4.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

/**
 * @file
 * Contains Drupal\Core\Config\InstallStorage.
 */

namespace Drupal\Core\Config;

/**
 * Storage controller used by the Drupal installer.
 *
 * @see install_begin_request()
 */
class InstallStorage extends FileStorage {

17
18
19
20
21
22
23
  /**
   * Folder map indexed by configuration name.
   *
   * @var array
   */
  protected $folders;

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  /**
   * Overrides Drupal\Core\Config\FileStorage::__construct().
   */
  public function __construct() {
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::getFilePath().
   *
   * Returns the path to the configuration file.
   *
   * Determines the owner and path to the default configuration file of a
   * requested config object name located in the installation profile, a module,
   * or a theme (in this order).
   *
   * @return string
   *   The path to the configuration file.
   *
   * @todo Improve this when figuring out how we want to handle configuration in
   *   installation profiles. E.g., a config object actually has to be searched
   *   in the profile first (whereas the profile is never the owner), only
   *   afterwards check for a corresponding module or theme.
   */
  public function getFilePath($name) {
48
49
50
    $folders = $this->getAllFolders();
    if (isset($folders[$name])) {
      return $folders[$name] . '/' . $name . '.' . $this->getFileExtension();
51
52
53
54
55
56
57
58
59
60
61
    }
    // If any code in the early installer requests a configuration object that
    // does not exist anywhere as default config, then that must be mistake.
    throw new StorageException(format_string('Missing configuration file: @name', array(
      '@name' => $name,
    )));
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::write().
   *
62
   * @throws \Drupal\Core\Config\StorageException
63
64
65
66
67
68
69
70
   */
  public function write($name, array $data) {
    throw new StorageException('Write operation is not allowed during install.');
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::delete().
   *
71
   * @throws \Drupal\Core\Config\StorageException
72
73
74
75
76
77
78
79
   */
  public function delete($name) {
    throw new StorageException('Delete operation is not allowed during install.');
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::rename().
   *
80
   * @throws \Drupal\Core\Config\StorageException
81
82
83
84
85
86
87
88
89
   */
  public function rename($name, $new_name) {
    throw new StorageException('Rename operation is not allowed during install.');
  }

  /**
   * Implements Drupal\Core\Config\StorageInterface::listAll().
   */
  public function listAll($prefix = '') {
90
91
92
93
94
95
96
97
98
    $names = array_keys($this->getAllFolders());
    if (!$prefix) {
      return $names;
    }
    else {
      $return = array();
      foreach ($names as $index => $name) {
        if (strpos($name, $prefix) === 0 ) {
          $return[$index] = $names[$index];
99
100
        }
      }
101
      return $return;
102
    }
103
  }
104

105
106
107
108
109
110
111
112
113
114
  /**
   * Returns a map of all config object names and their folders.
   *
   * @return array
   *   An array mapping config object names with directories.
   */
  protected function getAllFolders() {
    if (!isset($this->folders)) {
      $this->folders = $this->getComponentNames('profile', array(drupal_get_profile()));
      $this->folders += $this->getComponentNames('module', array_keys(drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.module$/', 'modules', 'name', 0)));
115
      $this->folders += $this->getComponentNames('theme', array_keys(drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info.yml$/', 'themes')));
116
    }
117
118
    return $this->folders;
  }
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  /**
   * Get all configuration names and folders for a list of modules or themes.
   *
   * @param string $type
   *   Type of components: 'module' | 'theme' | 'profile'
   * @param array $list
   *   Array of theme or module names.
   *
   * @return array
   *   Folders indexed by configuration name.
   */
  public function getComponentNames($type, array $list) {
    $extension = '.' . $this->getFileExtension();
    $folders = array();
    foreach ($list as $name) {
      $directory = $this->getComponentFolder($type, $name);
      if (file_exists($directory)) {
137
        $files = new \GlobIterator(DRUPAL_ROOT . '/' . $directory . '/*' . $extension);
138
139
        foreach ($files as $file) {
          $folders[$file->getBasename($extension)] = $directory;
140
141
142
        }
      }
    }
143
144
    return $folders;
  }
145

146
147
148
149
150
151
152
153
154
155
156
157
158
159
  /**
   * Get folder inside each component that contains the files.
   *
   * @param string $type
   *   Component type: 'module' | 'theme' | 'profile'
   * @param string $name
   *   Component name.
   *
   * @return string
   *   The configuration folder name for this component.
   */
  protected function getComponentFolder($type, $name) {
    return drupal_get_path($type, $name) . '/config';
  }
160
161
162
163

  /**
   * Overrides Drupal\Core\Config\FileStorage::deleteAll().
   *
164
   * @throws \Drupal\Core\Config\StorageException
165
166
167
168
169
   */
  public function deleteAll($prefix = '') {
    throw new StorageException('Delete operation is not allowed during install.');
  }

170
}