diff --git a/core/includes/config.inc b/core/includes/config.inc index 2925b99..e704d04 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -87,5 +87,7 @@ function config_get_storage_names_with_prefix($prefix = '') { * @todo Replace with DI container. */ function config($name, $class = 'Drupal\Core\Config\ConfigObject') { + // @todo Do not reload an already loaded config object. + // @todo Make it possible to skip load for known to be new config objects? return ConfigFactory::get($name, $class)->load(); } diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index 4667618..8ca8c5d 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -2,13 +2,64 @@ namespace Drupal\Core\Config; +/** + * Defines the configuration object factory. + * + * The configuration object factory instantiates a configuration object for each + * configuration object name that is accessed and returns the configuration + * object to callers. + * + * Each configuration object is wrapped by a configuration manager object, which + * is used to determine the storage controller to use for reading and writing + * the configuration object data. + * + * @see Drupal\Core\Config\ConfigObject + */ class ConfigFactory { - protected static $arbiters; + /** + * Instantiated storage manager objects. + * + * @var array + */ + protected static $storageManagers = array(); - static function get($name, $class = 'Drupal\Core\Config\ConfigObject', $arbiter_class = 'Drupal\Core\Config\DrupalConfig') { - if (!isset(self::$arbiters[$arbiter_class])) { - self::$arbiters[$class] = new $arbiter_class(); + /** + * Instantiated configuration objects. + * + * @todo Implement ConfigCacheArray to cache and preload config objects per + * request. + * + * @var array + */ + protected static $configObjects = array(); + + /** + * Returns a configuration object for a given name. + * + * Configuration objects are instantiated once only. + * + * @param string $name + * The name of the configuration object to construct. + * @param string $class + * (optional) The name of the class to use for the configuration object. + * Defaults to Drupal\Core\Config\ConfigObject. + * @param string $manager_class + * (optional) The name of the storage manager class to use. Defaults to + * Drupal\Core\Config\DrupalConfig. + * + * @return Drupal\Core\Config\ConfigObject + * A configuration object with the given $name. + * + * @todo Allow to instantiante a custom ConfigObject class for configuration + * data. + */ + public static function get($name, $class = 'Drupal\Core\Config\ConfigObject', $manager_class = 'Drupal\Core\Config\DrupalConfig') { + if (!isset(self::$storageManagers[$manager_class])) { + self::$storageManagers[$class] = new $manager_class(); + } + if (!isset(self::$configObjects[$name])) { + self::$configObjects[$name] = new $class($name, self::$storageManagers[$class]); } - return new $class($name, self::$arbiters[$class]); + return self::$configObjects[$name]; } } diff --git a/core/lib/Drupal/Core/Config/ConfigObject.php b/core/lib/Drupal/Core/Config/ConfigObject.php index 9438edd..b083c45 100644 --- a/core/lib/Drupal/Core/Config/ConfigObject.php +++ b/core/lib/Drupal/Core/Config/ConfigObject.php @@ -22,16 +22,23 @@ class ConfigObject { protected $data = array(); /** + * The wrapping storage manager object. + * + * @var Drupal\Core\Config\DrupalConfig + */ + protected $drupalConfig; + + /** * Constructs a configuration object. * * @param string $name * The name of the configuration object. - * @param Drupal\Core\Config\DrupalConfig $arbiter + * @param Drupal\Core\Config\DrupalConfig $drupalConfig * The wrapping configuration manager object. */ - public function __construct($name, DrupalConfig $arbiter) { + public function __construct($name, DrupalConfig $drupalConfig) { $this->setName($name); - $this->storageArbiter = $arbiter; + $this->drupalConfig = $drupalConfig; } /** @@ -198,7 +205,7 @@ class ConfigObject { * Loads configuration data into this object. */ public function load() { - $data = $this->storageArbiter->selectStorage('read', $this->name)->read($this->name); + $data = $this->drupalConfig->selectStorage('read', $this->name)->read($this->name); $this->setData($data !== FALSE ? $data : array()); return $this; } @@ -207,7 +214,7 @@ class ConfigObject { * Saves the configuration object. */ public function save() { - $this->storageArbiter->selectStorage('write', $this->name)->write($this->name, $this->data); + $this->drupalConfig->selectStorage('write', $this->name)->write($this->name, $this->data); return $this; } @@ -216,7 +223,7 @@ class ConfigObject { */ public function delete() { $this->data = array(); - $this->storageArbiter->selectStorage('write', $this->name)->delete($this->name); + $this->drupalConfig->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 f281263..92dc61f 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; /** - * Arbiters configuration storage engines. + * Manages configuration storage controllers. * * A high-level storage manager that determines which storage out of multiple * is configured and allowed to handle a particular configuration object, @@ -39,7 +39,7 @@ class DrupalConfig { protected $storageInstances; /** - * Instantiated storage controller objects. + * Constructs the storage manager object. * * @param array $storage_info * An associative array defining the storage controllers to use and any @@ -59,12 +59,10 @@ class DrupalConfig { * ) * @endcode */ - public function __construct($storage_info = NULL) { - if (isset($storage_info)) { - $this->storageInfo = $storage_info; - } - else { - $this->storageInfo = array( + public function __construct(array $storage_info = NULL) { + // @todo This is the wrong place to supply defaults. + if (!isset($storage_info)) { + $storage_info = array( 'Drupal\Core\Config\DatabaseStorage' => array( 'target' => 'default', 'read' => TRUE, @@ -77,6 +75,7 @@ class DrupalConfig { ), ); } + $this->storageInfo = $storage_info; } /**