diff --git a/core/includes/config.inc b/core/includes/config.inc index 78d12e8..90060d9 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -117,110 +117,3 @@ function config($name, $class = 'Drupal\Core\Config\DrupalConfig') { return new $class(new DatabaseStorage($name)); } -/** - * Decodes configuration data from its native format to an associative array. - * - * @param $data - * Configuration data. - * - * @return - * An associative array representation of the data. - */ -function config_decode($data) { - if (empty($data)) { - return array(); - } - - // This is the fastest and easiest way to get from a string of XML to a PHP - // array since SimpleXML and json_decode()/encode() are native to PHP. Our - // only other choice would be a custom userspace implementation which would - // be a lot less performant and more complex. - $xml = new SimpleXMLElement($data); - $json = json_encode($xml); - return json_decode($json, TRUE); -} - -/** - * Standardizes SimpleXML object output into simple arrays for easier use. - * - * @param $xmlObject - * A valid XML string. - * - * @return - * An array representation of A SimpleXML object. - */ -function config_xml_to_array($data) { - $out = array(); - $xmlObject = simplexml_load_string($data); - - if (is_object($xmlObject)) { - $attributes = (array) $xmlObject->attributes(); - if (isset($attributes['@attributes'])) { - $out['#attributes'] = $attributes['@attributes']; - } - } - if (trim((string) $xmlObject)) { - return trim((string) $xmlObject); - } - foreach ($xmlObject as $index => $content) { - if (is_object($content)) { - $out[$index] = config_xml_to_array($content); - } - } - - return $out; -} - -/** - * Encodes an array into the native configuration format. - * - * @param $data - * An associative array or an object - * - * @return - * A representation of this array or object in the native configuration - * format. - * - * @todo The loaded XML can be invalid; throwing plenty of PHP warnings but no - * catchable error. - */ -function config_encode($data) { - // Convert the supplied array into a SimpleXMLElement. - $xml_object = new SimpleXMLElement(""); - config_array_to_xml($data, $xml_object); - - // Pretty print the result. - $dom = new DOMDocument('1.0'); - $dom->preserveWhiteSpace = false; - $dom->formatOutput = true; - $dom->loadXML($xml_object->asXML()); - - return $dom->saveXML(); -} - -/** - * Encodes an array into XML - * - * @param $data - * An associative array or an object - * - * @return - * A representation of this array or object in the native configuration - * format. - */ -function config_array_to_xml($array, &$xml_object) { - foreach ($array as $key => $value) { - if (is_array($value)) { - if (!is_numeric($key)){ - $subnode = $xml_object->addChild("$key"); - config_array_to_xml($value, $subnode); - } - else { - config_array_to_xml($value, $xml_object); - } - } - else { - $xml_object->addChild($key, $value); - } - } -} diff --git a/core/lib/Drupal/Core/Config/DatabaseStorage.php b/core/lib/Drupal/Core/Config/DatabaseStorage.php index 63ae7b4..ba6ecbe 100644 --- a/core/lib/Drupal/Core/Config/DatabaseStorage.php +++ b/core/lib/Drupal/Core/Config/DatabaseStorage.php @@ -11,7 +11,7 @@ use Exception; class DatabaseStorage extends StorageBase { /** - * Overrides StorageBase::read(). + * Implements StorageInterface::read(). */ public function read() { // There are situations, like in the installer, where we may attempt a @@ -19,16 +19,20 @@ class DatabaseStorage extends StorageBase { // catch the exception and just return an empty array so the caller can // handle it if need be. try { - return db_query('SELECT data FROM {config} WHERE name = :name', array(':name' => $this->name))->fetchField(); - } catch (Exception $e) { - return array(); + $data = db_query('SELECT data FROM {config} WHERE name = :name', array(':name' => $this->name))->fetchField(); + $data = $this->decode($data); } + catch (Exception $e) { + $data = array(); + } + return $data; } /** * Implements StorageInterface::writeToActive(). */ public function writeToActive($data) { + $data = $this->encode($data); return db_merge('config') ->key(array('name' => $this->name)) ->fields(array('data' => $data)) @@ -45,6 +49,20 @@ class DatabaseStorage extends StorageBase { } /** + * Implements StorageInterface::encode(). + */ + public function encode($data) { + return serialize($data); + } + + /** + * Implements StorageInterface::decode(). + */ + public function decode($raw) { + return unserialize($raw); + } + + /** * Implements StorageInterface::getNamesWithPrefix(). */ static public function getNamesWithPrefix($prefix = '') { diff --git a/core/lib/Drupal/Core/Config/DrupalConfig.php b/core/lib/Drupal/Core/Config/DrupalConfig.php index c405757..25339e9 100644 --- a/core/lib/Drupal/Core/Config/DrupalConfig.php +++ b/core/lib/Drupal/Core/Config/DrupalConfig.php @@ -36,7 +36,7 @@ class DrupalConfig { * Reads config data from the active store into our object. */ public function read() { - $active = (array) config_decode($this->storage->read()); + $active = (array) $this->storage->read(); foreach ($active as $key => $value) { // If the setting is empty, return an empty string rather than an array. // This is necessary because SimpleXML's default behavior is to return @@ -201,7 +201,7 @@ class DrupalConfig { * Saves the configuration object to disk as XML. */ public function save() { - $this->storage->write(config_encode($this->data)); + $this->storage->write($this->data); } /** diff --git a/core/lib/Drupal/Core/Config/FileStorage.php b/core/lib/Drupal/Core/Config/FileStorage.php index e65e168..2076afd 100644 --- a/core/lib/Drupal/Core/Config/FileStorage.php +++ b/core/lib/Drupal/Core/Config/FileStorage.php @@ -67,6 +67,7 @@ class FileStorage { * Exception */ public function write($data) { + $data = $this->encode($data); if (!file_put_contents($this->getFilePath(), $data)) { throw new FileStorageException('Failed to write configuration file: ' . $this->getFilePath()); } @@ -81,6 +82,7 @@ class FileStorage { public function read() { if ($this->exists()) { $data = $this->readData(); + $data = $this->decode($data); return $data; } return FALSE; @@ -93,4 +95,64 @@ class FileStorage { // Needs error handling and etc. @drupal_unlink($this->getFilePath()); } + + /** + * Implements StorageInterface::encode(). + */ + public function encode($data) { + // Convert the supplied array into a SimpleXMLElement. + $xml_object = new SimpleXMLElement(""); + $this->encodeArrayToXml($data, $xml_object); + + // Pretty print the result. + $dom = new DOMDocument('1.0'); + $dom->preserveWhiteSpace = false; + $dom->formatOutput = true; + $dom->loadXML($xml_object->asXML()); + + return $dom->saveXML(); + } + + /** + * Encodes an array into XML + * + * @param $array + * An associative array to encode. + * + * @return + * A representation of $array in XML. + */ + protected function encodeArrayToXml($array, &$xml_object) { + foreach ($array as $key => $value) { + if (is_array($value)) { + if (!is_numeric($key)){ + $subnode = $xml_object->addChild("$key"); + $this->encodeArrayToXml($value, $subnode); + } + else { + $this->encodeArrayToXml($value, $xml_object); + } + } + else { + $xml_object->addChild($key, $value); + } + } + } + + /** + * Implements StorageInterface::decode(). + */ + public function decode($raw) { + if (empty($raw)) { + return array(); + } + + // This is the fastest and easiest way to get from a string of XML to a PHP + // array since SimpleXML and json_decode()/encode() are native to PHP. Our + // only other choice would be a custom userspace implementation which would + // be a lot less performant and more complex. + $xml = new SimpleXMLElement($raw); + $json = json_encode($xml); + return json_decode($json, TRUE); + } } diff --git a/core/lib/Drupal/Core/Config/StorageInterface.php b/core/lib/Drupal/Core/Config/StorageInterface.php index f1e8a3d..6d16334 100644 --- a/core/lib/Drupal/Core/Config/StorageInterface.php +++ b/core/lib/Drupal/Core/Config/StorageInterface.php @@ -75,6 +75,16 @@ interface StorageInterface { function writeToFile($data); /** + * Encodes configuration data into the storage-specific format. + */ + public function encode($data); + + /** + * Decodes configuration data from the storage-specific format. + */ + public function decode($raw); + + /** * Gets names starting with this prefix. * * @param $prefix