2011-06-10 01:13:07 +00:00
|
|
|
<?php
|
|
|
|
|
2012-05-10 14:54:04 +00:00
|
|
|
use Drupal\Core\Config\DatabaseStorage;
|
2012-05-16 03:02:24 +00:00
|
|
|
use Drupal\Core\Config\FileStorage;
|
2012-02-12 16:17:29 +00:00
|
|
|
|
2011-09-05 14:13:26 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* This is the API for configuration storage.
|
|
|
|
*/
|
|
|
|
|
2011-09-05 13:58:04 +00:00
|
|
|
/**
|
2012-01-08 12:26:20 +00:00
|
|
|
* Gets the randomly generated config directory name.
|
2011-09-05 13:58:04 +00:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The directory name.
|
|
|
|
*/
|
2011-09-11 18:04:20 +00:00
|
|
|
function config_get_config_directory() {
|
2012-01-08 22:18:19 +00:00
|
|
|
global $config_directory_name;
|
2012-01-08 01:29:22 +00:00
|
|
|
|
2012-02-13 17:53:07 +00:00
|
|
|
if ($test_prefix = drupal_valid_test_ua()) {
|
2012-05-10 06:01:48 +00:00
|
|
|
// @see Drupal\simpletest\WebTestBase::setUp()
|
2012-04-21 01:38:11 +00:00
|
|
|
$path = conf_path() . '/files/simpletest/' . substr($test_prefix, 10) . '/config';
|
2012-02-13 17:53:07 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$path = conf_path() . '/files/' . $config_directory_name;
|
|
|
|
}
|
|
|
|
return $path;
|
2011-09-05 13:58:04 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 13:42:35 +00:00
|
|
|
/**
|
2012-01-08 12:26:20 +00:00
|
|
|
* Moves the default config supplied by a module to the live config directory.
|
2011-09-26 13:42:35 +00:00
|
|
|
*
|
|
|
|
* @param
|
|
|
|
* The name of the module we are installing.
|
2012-02-23 05:28:37 +00:00
|
|
|
*
|
|
|
|
* @todo Make this acknowledge other storage engines rather than having
|
|
|
|
* SQL be hardcoded.
|
2011-09-26 13:42:35 +00:00
|
|
|
*/
|
|
|
|
function config_install_default_config($module) {
|
|
|
|
$module_config_dir = drupal_get_path('module', $module) . '/config';
|
|
|
|
$drupal_config_dir = config_get_config_directory();
|
|
|
|
if (is_dir(drupal_get_path('module', $module) . '/config')) {
|
|
|
|
$files = glob($module_config_dir . '/' . '*.xml');
|
2011-10-17 01:05:16 +00:00
|
|
|
foreach ($files as $key => $file) {
|
|
|
|
// Load config data into the active store and write it out to the
|
2011-09-26 13:42:35 +00:00
|
|
|
// file system in the drupal config directory. Note the config name
|
|
|
|
// needs to be the same as the file name WITHOUT the extension.
|
|
|
|
$parts = explode('/', $file);
|
|
|
|
$file = array_pop($parts);
|
|
|
|
$config_name = str_replace('.xml', '', $file);
|
|
|
|
|
2012-05-10 14:54:04 +00:00
|
|
|
$storage = new DatabaseStorage($config_name);
|
2012-05-16 03:02:24 +00:00
|
|
|
$data = FileStorage::decode(file_get_contents($module_config_dir . '/' . $file));
|
|
|
|
$storage->write($data);
|
2011-09-26 13:42:35 +00:00
|
|
|
}
|
2011-10-17 01:05:16 +00:00
|
|
|
}
|
2011-09-26 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2011-06-12 07:07:10 +00:00
|
|
|
/**
|
2012-01-08 12:26:20 +00:00
|
|
|
* Retrieves an iterable array which lists the children under a config 'branch'.
|
2011-06-12 07:51:38 +00:00
|
|
|
*
|
|
|
|
* Given the following configuration files:
|
2012-01-08 01:29:22 +00:00
|
|
|
* - core.entity.node_type.article.xml
|
|
|
|
* - core.entity.node_type.page.xml
|
2011-06-12 07:51:38 +00:00
|
|
|
*
|
|
|
|
* You can pass a prefix 'core.entity.node_type' and get back an array of the
|
2012-01-08 02:54:32 +00:00
|
|
|
* filenames that match. This allows you to iterate through all files in a
|
2012-02-23 13:01:20 +00:00
|
|
|
* branch.
|
2011-06-12 07:51:38 +00:00
|
|
|
*
|
|
|
|
* @param $prefix
|
|
|
|
* The prefix of the files we are searching for.
|
2012-01-08 01:29:22 +00:00
|
|
|
*
|
2011-06-12 07:07:10 +00:00
|
|
|
* @return
|
2011-06-12 07:51:38 +00:00
|
|
|
* An array of file names under a branch.
|
2011-06-12 07:07:10 +00:00
|
|
|
*/
|
2012-05-08 05:04:54 +00:00
|
|
|
function config_get_files_with_prefix($prefix = '') {
|
2011-09-14 11:18:04 +00:00
|
|
|
$files = glob(config_get_config_directory() . '/' . $prefix . '*.xml');
|
2011-06-10 05:07:10 +00:00
|
|
|
$clean_name = function ($value) {
|
2011-09-11 15:34:59 +00:00
|
|
|
return basename($value, '.xml');
|
2011-06-10 05:07:10 +00:00
|
|
|
};
|
2011-06-10 05:42:09 +00:00
|
|
|
return array_map($clean_name, $files);
|
2011-06-10 02:54:46 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 12:26:20 +00:00
|
|
|
/**
|
|
|
|
* @todo
|
|
|
|
*
|
|
|
|
* @param $prefix
|
|
|
|
* @todo
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* @todo
|
|
|
|
*/
|
2012-05-10 14:54:04 +00:00
|
|
|
function config_get_storage_names_with_prefix($prefix = '') {
|
|
|
|
return DatabaseStorage::getNamesWithPrefix($prefix);
|
2011-09-23 12:23:56 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 00:45:56 +00:00
|
|
|
/**
|
2012-01-08 12:26:20 +00:00
|
|
|
* Retrieves a configuration object.
|
2011-10-17 00:45:56 +00:00
|
|
|
*
|
|
|
|
* This is the main entry point to the configuration API. Calling
|
|
|
|
* @code config(book.admin) @endcode will return a configuration object in which
|
|
|
|
* the book module can store its administrative settings.
|
|
|
|
*
|
|
|
|
* @param $name
|
|
|
|
* The name of the configuration object to retrieve. The name corresponds to
|
|
|
|
* an XML configuration file. For @code config(book.admin) @endcode, the
|
|
|
|
* config object returned will contain the contents of book.admin.xml.
|
|
|
|
* @param $class
|
|
|
|
* The class name of the config object to be returned. Defaults to
|
|
|
|
* DrupalConfig.
|
2012-01-08 01:29:22 +00:00
|
|
|
*
|
2011-10-17 00:45:56 +00:00
|
|
|
* @return
|
|
|
|
* An instance of the class specified in the $class parameter.
|
|
|
|
*
|
2012-02-23 05:28:37 +00:00
|
|
|
* @todo Replace this with an appropriate factory / ability to inject in
|
2012-02-23 13:01:20 +00:00
|
|
|
* alternate storage engines..
|
2011-10-17 00:45:56 +00:00
|
|
|
*/
|
2012-02-12 16:17:29 +00:00
|
|
|
function config($name, $class = 'Drupal\Core\Config\DrupalConfig') {
|
2012-05-10 14:54:04 +00:00
|
|
|
return new $class(new DatabaseStorage($name));
|
2011-09-23 12:23:56 +00:00
|
|
|
}
|