Skip to content

Instantly share code, notes, and snippets.

@sun
Created May 29, 2012 05:59

Revisions

  1. sun revised this gist May 29, 2012. 1 changed file with 48 additions and 14 deletions.
    62 changes: 48 additions & 14 deletions DrupalConfig.php
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,14 @@
    class DrupalConfig {

    /**
    * The storage engines to save this config object to.
    * The storage controllers to use.
    *
    * @var StorageInterface
    * @var array
    */
    protected $storageInterfaces;

    protected $storages;

    protected $storageInstances;

    /**
    * Config Objects.
    *
    @@ -27,31 +29,63 @@ class DrupalConfig {
    /**
    * Constructs a DrupalConfig object.
    *
    * @param StorageInterface $storage
    * The storage engine where this config object should be saved.
    * @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(StorageInterface $storage) {
    $this->storage = $storage;
    public function __construct($storages) {
    $this->storages = $storages;
    }

    public function getConfigObject($name) {
    public function load($name) {
    $storage = $this->findStorage('read', $name);
    if (empty($this->configObjects[$name])) {
    $this->configObjects[$name] = new ConfigObject($name, $this->storage);
    $this->configObjects[$name] = new ConfigObject($name, $this->storages[$storage]);
    }
    return $this->configObjects[$name];
    }

    public function save($configName, $configData) {
    public function save($name, $data) {
    // Some logic to decide which storage to write to...
    $this->storage->write($configName, $configData);
    $storage = $this->findStorage('write', $name);
    $this->storages[$storage]->write($name, $data);
    }

    public function delete($configName) {
    public function delete($name) {
    // Some logic to decide which storage to write to...
    $this->storage->delete($configName);
    $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...
    */
  2. beejeebus revised this gist May 29, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions DrupalConfig.php
    Original file line number Diff line number Diff line change
    @@ -11,11 +11,11 @@
    class DrupalConfig {

    /**
    * The storage engine to save this config object to.
    * The storage engines to save this config object to.
    *
    * @var StorageInterface
    */
    protected $storage;
    protected $storageInterfaces;

    /**
    * Config Objects.
    @@ -48,7 +48,7 @@ public function save($configName, $configData) {
    $this->storage->write($configName, $configData);
    }

    public function save($configName) {
    public function delete($configName) {
    // Some logic to decide which storage to write to...
    $this->storage->delete($configName);
    }
  3. beejeebus revised this gist May 29, 2012. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion DrupalConfig.php
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,16 @@ public function getConfigObject($name) {
    return $this->configObjects[$name];
    }

    public function save($configName, $configData) {
    // Some logic to decide which storage to write to...
    $this->storage->write($configName, $configData);
    }

    public function save($configName) {
    // Some logic to decide which storage to write to...
    $this->storage->delete($configName);
    }
    /**
    * More site-level stuff here, like config sync...
    */
    }
    }
  4. beejeebus revised this gist May 29, 2012. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions ConfigObject.php
    Original file line number Diff line number Diff line change
    @@ -10,14 +10,16 @@ class ConfigObject {

    protected $data = array();

    public function __construct($name, StorageInterface $storage) {
    protected $drupalConfig;

    public function __construct($name, $drupalConfig) {
    $this->name = $name;
    $this->storage = $storage;
    $this->drupalConfig = $drupalConfig;
    $this->read();
    }

    public function read() {
    $data = $this->storage->read($this->name);
    $data = $this->drupalConfig->read($this->name);
    $this->setData($data !== FALSE ? $data : array());
    return $this;
    }
    @@ -116,15 +118,15 @@ public function clear($key) {
    * Saves the configuration object.
    */
    public function save() {
    $this->storage->write($this->name, $this->data);
    $this->drupalConfig->save($this->name, $this->data);
    }

    /**
    * Deletes the configuration object.
    */
    public function delete() {
    $this->data = array();
    $this->storage->delete($this->name);
    $this->drupalConfig->delete($this->name);
    }

    }
  5. beejeebus revised this gist May 29, 2012. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions DrupalConfig.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <?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 engine to save this config object to.
    *
    * @var StorageInterface
    */
    protected $storage;

    /**
    * Config Objects.
    *
    * @var array
    */
    protected $configObjects = array();

    /**
    * Constructs a DrupalConfig object.
    *
    * @param StorageInterface $storage
    * The storage engine where this config object should be saved.
    *
    * @todo $this should really know about $name and make it publicly accessible.
    */
    public function __construct(StorageInterface $storage) {
    $this->storage = $storage;
    }

    public function getConfigObject($name) {
    if (empty($this->configObjects[$name])) {
    $this->configObjects[$name] = new ConfigObject($name, $this->storage);
    }
    return $this->configObjects[$name];
    }

    /**
    * More site-level stuff here, like config sync...
    */
    }
  6. beejeebus created this gist May 29, 2012.
    130 changes: 130 additions & 0 deletions ConfigObject.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,130 @@
    <?php

    namespace Drupal\Core\Config;

    use Drupal\Core\Config\StorageInterface;

    class ConfigObject {

    protected $name = '';

    protected $data = array();

    public function __construct($name, StorageInterface $storage) {
    $this->name = $name;
    $this->storage = $storage;
    $this->read();
    }

    public function read() {
    $data = $this->storage->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->storage->write($this->name, $this->data);
    }

    /**
    * Deletes the configuration object.
    */
    public function delete() {
    $this->data = array();
    $this->storage->delete($this->name);
    }

    }