diff --git a/core/includes/config.inc b/core/includes/config.inc index 23df9f2..2925b99 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -2,6 +2,7 @@ use Drupal\Core\Config\DatabaseStorage; use Drupal\Core\Config\FileStorage; +use Drupal\Core\Config\ConfigFactory; /** * @file @@ -78,35 +79,13 @@ function config_get_storage_names_with_prefix($prefix = '') { * object returned will contain the contents of book.admin configuration file. * @param $class * The class name of the config object to be returned. Defaults to - * DrupalConfig. + * ConfigObject. * * @return * An instance of the class specified in the $class parameter. * * @todo Replace with DI container. */ -function config($name, $class = 'Drupal\Core\Config\DrupalConfig') { - global $config_info; - static $instances = array(); - - if (!isset($config_info)) { - $config_info = array( - 'Drupal\Core\Config\DatabaseStorage' => array( - 'target' => 'default', - 'read' => TRUE, - 'write' => TRUE, - ), - 'Drupal\Core\Config\FileStorage' => array( - 'directory' => config_get_config_directory(), - 'read' => TRUE, - 'write' => TRUE, - ), - ); - } - - if (!isset($instances[$class])) { - $instances[$class] = new $class($config_info); - } - - return $instances[$class]->load($name); +function config($name, $class = 'Drupal\Core\Config\ConfigObject') { + return ConfigFactory::get($name, $class)->load(); } diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php new file mode 100644 index 0000000..4667618 --- /dev/null +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -0,0 +1,14 @@ +setName($name); - $this->drupalConfig = $drupalConfig; + $this->storageArbiter = $arbiter; } /** @@ -207,11 +198,8 @@ class ConfigObject { * Loads configuration data into this object. */ public function load() { - // Embed DrupalConfig::load() to retain this object, regardless of its name. - $storage = $this->drupalConfig->selectStorage('read', $this->name); - $data = $storage->read($this->name); - $data = ($data !== FALSE ? $data : array()); - $this->setData($data); + $data = $this->storageArbiter->selectStorage('read', $this->name)->read($this->name); + $this->setData($data !== FALSE ? $data : array()); return $this; } @@ -219,7 +207,7 @@ class ConfigObject { * Saves the configuration object. */ public function save() { - $this->drupalConfig->save($this->name, $this->data); + $this->storageArbiter->selectStorage('write', $this->name)->write($this->name, $this->data); return $this; } @@ -228,7 +216,7 @@ class ConfigObject { */ public function delete() { $this->data = array(); - $this->drupalConfig->delete($this->name); + $this->storageArbiter->selectStorage('write', $this->name)->delete($this->name); return $this; } } diff --git a/core/lib/Drupal/Core/Config/DrupalConfig.php b/core/lib/Drupal/Core/Config/DrupalConfig.php index 6ca5c65..dd176cd 100644 --- a/core/lib/Drupal/Core/Config/DrupalConfig.php +++ b/core/lib/Drupal/Core/Config/DrupalConfig.php @@ -3,7 +3,7 @@ namespace Drupal\Core\Config; /** - * Defines the default configuration manager. + * Arbiters configuration storage engines. * * A high-level storage manager that determines which storage out of multiple * is configured and allowed to handle a particular configuration object, @@ -19,15 +19,6 @@ namespace Drupal\Core\Config; * only once per storage. * * @see Drupal\Core\Config\StorageInterface - * - * The configuration manager instantiates a new configuration object for each - * configuration object that is accessed, and which is returned to callers of - * the load/save/delete methods instead of the configuration manager, since most - * consuming code wants to act on the configuration object directly. Each - * configuration object is able to call back into the configuration manager - * object that instantiated it when necessary (e.g., for CRUD operations). - * - * @see Drupal\Core\Config\ConfigObject */ class DrupalConfig { @@ -48,13 +39,6 @@ class DrupalConfig { protected $storageInstances; /** - * Config Objects. - * - * @var array - */ - protected $configObjects = array(); - - /** * Constructs a DrupalConfig object. * * @param array $storage_info @@ -75,61 +59,24 @@ class DrupalConfig { * ) * @endcode */ - public function __construct($storage_info) { - $this->storageInfo = $storage_info; - } - - /** - * Loads a configuration object. - * - * @param string $name - * The name of the configuration object to load. - */ - public function load($name) { - $storage = $this->selectStorage('read', $name); - $data = $storage->read($name); - $data = ($data !== FALSE ? $data : array()); - if (!isset($this->configObjects[$name])) { - $this->configObjects[$name] = new ConfigObject($name, $this); - } - $this->configObjects[$name]->setData($data); - return $this->configObjects[$name]; - } - - /** - * Saves a configuration object. - * - * @param string $name - * The name of the configuration object to save. - * @param array $data - * The configuration data to save. - */ - public function save($name, array $data) { - if (!isset($this->configObjects[$name])) { - $this->configObjects[$name] = new ConfigObject($name, $this); + public function __construct($storage_info = NULL) { + if (isset($storage_info)) { + $this->storageInfo = $storage_info; } - $this->configObjects[$name]->setData($data); - - $storage = $this->selectStorage('write', $name); - $storage->write($name, $data); - return $this->configObjects[$name]; - } - - /** - * Deletes a configuration object. - * - * @param string $name - * The name of the configuration object to delete. - */ - public function delete($name) { - if (!isset($this->configObjects[$name])) { - $this->configObjects[$name] = new ConfigObject($name, $this); + else { + $this->storageInfo = array( + 'Drupal\Core\Config\DatabaseStorage' => array( + 'target' => 'default', + 'read' => TRUE, + 'write' => TRUE, + ), + 'Drupal\Core\Config\FileStorage' => array( + 'directory' => config_get_config_directory(), + 'read' => TRUE, + 'write' => TRUE, + ), + ); } - $this->configObjects[$name]->setData(array()); - - $storage = $this->selectStorage('write', $name); - $storage->delete($name); - return $this->configObjects[$name]; } /**