Skip to content

Instantly share code, notes, and snippets.

@sun
Created May 29, 2012 05:59
Show Gist options
  • Save sun/2822857 to your computer and use it in GitHub Desktop.
Save sun/2822857 to your computer and use it in GitHub Desktop.
simple config object
<?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);
}
}
<?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...
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment