Created
May 29, 2012 05:59
-
-
Save sun/2822857 to your computer and use it in GitHub Desktop.
simple config object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\Core\Config; | |
use Drupal\Core\Config\StorageInterface; | |
class ConfigObject { | |
protected $name = ''; | |
protected $data = array(); | |
protected $drupalConfig; | |
public function __construct($name, $drupalConfig) { | |
$this->name = $name; | |
$this->drupalConfig = $drupalConfig; | |
$this->read(); | |
} | |
public function read() { | |
$data = $this->drupalConfig->read($this->name); | |
$this->setData($data !== FALSE ? $data : array()); | |
return $this; | |
} | |
/** | |
* Replaces the data of this configuration object. | |
* | |
* @param array $data | |
* The new configuration data. | |
*/ | |
public function setData(array $data) { | |
$this->data = $data; | |
return $this; | |
} | |
/** | |
* Sets value in this config object. | |
* | |
* @param $key | |
* @todo | |
* @param $value | |
* @todo | |
*/ | |
public function set($key, $value) { | |
// Type-cast value into a string. | |
$value = $this->castValue($value); | |
// The dot/period is a reserved character; it may appear between keys, but | |
// not within keys. | |
$parts = explode('.', $key); | |
if (count($parts) == 1) { | |
$this->data[$key] = $value; | |
} | |
else { | |
drupal_array_set_nested_value($this->data, $parts, $value); | |
} | |
return $this; | |
} | |
/** | |
* Casts a saved value to a string. | |
* | |
* The configuration system only saves strings or arrays. Any scalar | |
* non-string value is cast to a string. The one exception is boolean FALSE | |
* which would normally become '' when cast to a string, but is manually | |
* cast to '0' here for convenience and consistency. | |
* | |
* Any non-scalar value that is not an array (aka objects) gets cast | |
* to an array. | |
* | |
* @param $value | |
* A value being saved into the configuration system. | |
* @param $value | |
* The value cast to a string or array. | |
*/ | |
public function castValue($value) { | |
if (is_scalar($value)) { | |
// Handle special case of FALSE, which should be '0' instead of ''. | |
if ($value === FALSE) { | |
$value = '0'; | |
} | |
else { | |
$value = (string) $value; | |
} | |
} | |
else { | |
// Any non-scalar value must be an array. | |
if (!is_array($value)) { | |
$value = (array) $value; | |
} | |
// Recurse into any nested keys. | |
foreach ($value as $key => $nested_value) { | |
$value[$key] = $this->castValue($nested_value); | |
} | |
} | |
return $value; | |
} | |
/** | |
* Unsets value in this config object. | |
* | |
* @param $key | |
* Name of the key whose value should be unset. | |
*/ | |
public function clear($key) { | |
$parts = explode('.', $key); | |
if (count($parts) == 1) { | |
unset($this->data[$key]); | |
} | |
else { | |
drupal_array_unset_nested_value($this->data, $parts); | |
} | |
} | |
/** | |
* Saves the configuration object. | |
*/ | |
public function save() { | |
$this->drupalConfig->save($this->name, $this->data); | |
} | |
/** | |
* Deletes the configuration object. | |
*/ | |
public function delete() { | |
$this->data = array(); | |
$this->drupalConfig->delete($this->name); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\Core\Config; | |
use Drupal\Core\Config\StorageInterface; | |
use Drupal\Core\Config\ConfigException; | |
/** | |
* Represents the default configuration storage object. | |
*/ | |
class DrupalConfig { | |
/** | |
* The storage controllers to use. | |
* | |
* @var array | |
*/ | |
protected $storages; | |
protected $storageInstances; | |
/** | |
* Config Objects. | |
* | |
* @var array | |
*/ | |
protected $configObjects = array(); | |
/** | |
* Constructs a DrupalConfig object. | |
* | |
* @param array $storages | |
* An associative array defining the storage controllers to use and any | |
* required configuration for them; e.g.: | |
* @code | |
* array( | |
* 'Drupal\Core\Config\DatabaseStorage' => array( | |
* 'connection' => 'default', | |
* 'read' => TRUE, | |
* 'write' => TRUE, | |
* ), | |
* 'Drupal\Core\Config\FileStorage' => array( | |
* 'directory' => 'sites/default/files/config', | |
* 'read' => TRUE, | |
* 'write' => FALSE, | |
* ), | |
* ) | |
* @endcode | |
* | |
* @todo $this should really know about $name and make it publicly accessible. | |
*/ | |
public function __construct($storages) { | |
$this->storages = $storages; | |
} | |
public function load($name) { | |
$storage = $this->findStorage('read', $name); | |
if (empty($this->configObjects[$name])) { | |
$this->configObjects[$name] = new ConfigObject($name, $this->storages[$storage]); | |
} | |
return $this->configObjects[$name]; | |
} | |
public function save($name, $data) { | |
// Some logic to decide which storage to write to... | |
$storage = $this->findStorage('write', $name); | |
$this->storages[$storage]->write($name, $data); | |
} | |
public function delete($name) { | |
// Some logic to decide which storage to write to... | |
$storage = $this->findStorage('write', $name); | |
$this->storage->delete($name); | |
} | |
public function findStorage($op, $name) { | |
foreach ($this->storages as $class => $storage_config) { | |
// Take the first storage that allows $op. | |
if (!empty($storage_config[$op])) { | |
break; | |
} | |
} | |
if (!isset($this->storageInstances[$class])) { | |
$this->storageInstances[$class] = new $class($storage_config); | |
} | |
return $this->storageInstances[$class]; | |
} | |
/** | |
* More site-level stuff here, like config sync... | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment