Commit d74c1441 authored by webchick's avatar webchick

Issue #1666632 by sun, chx: Add Config::isNew() to allow code to determine...

Issue #1666632 by sun, chx: Add Config::isNew() to allow code to determine whether a config object already exists.
parent 23127843
......@@ -19,6 +19,13 @@ class Config {
*/
protected $name;
/**
* Whether the configuration object is new or has been saved to the storage.
*
* @var bool
*/
protected $isNew = TRUE;
/**
* The data of the configuration object.
*
......@@ -59,6 +66,13 @@ public function setName($name) {
return $this;
}
/**
* Returns whether this configuration object is new.
*/
public function isNew() {
return $this->isNew;
}
/**
* Gets data from this config object.
*
......@@ -208,9 +222,13 @@ public function clear($key) {
* Loads configuration data into this object.
*/
public function load() {
$this->setData(array());
$data = $this->storageDispatcher->selectStorage('read', $this->name)->read($this->name);
if ($data !== FALSE) {
if ($data === FALSE) {
$this->isNew = TRUE;
$this->setData(array());
}
else {
$this->isNew = FALSE;
$this->setData($data);
}
return $this;
......@@ -222,6 +240,7 @@ public function load() {
public function save() {
$this->sortByKey($this->data);
$this->storageDispatcher->selectStorage('write', $this->name)->write($this->name, $this->data);
$this->isNew = FALSE;
return $this;
}
......@@ -251,6 +270,7 @@ public function sortByKey(array &$data) {
public function delete() {
$this->data = array();
$this->storageDispatcher->selectStorage('write', $this->name)->delete($this->name);
$this->isNew = TRUE;
return $this;
}
}
......@@ -51,7 +51,7 @@ protected function getConnection() {
* Only thrown in case $this->options['throw_exception'] is TRUE.
*/
public function read($name) {
$data = array();
$data = FALSE;
// There are situations, like in the installer, where we may attempt a
// read without actually having the database available. In this case,
// catch the exception and just return an empty array so the caller can
......@@ -114,7 +114,7 @@ public static function encode($data) {
*/
public static function decode($raw) {
$data = @unserialize($raw);
return $data !== FALSE ? $data : array();
return is_array($data) ? $data : FALSE;
}
/**
......
......@@ -70,19 +70,12 @@ public function exists($name) {
*/
public function read($name) {
if (!$this->exists($name)) {
return array();
return FALSE;
}
$data = file_get_contents($this->getFilePath($name));
// @todo Yaml throws a ParseException on invalid data. Is it expected to be
// caught or not?
$data = $this->decode($data);
if ($data === FALSE) {
return array();
}
// A simple string is valid YAML for any reason.
if (!is_array($data)) {
return array();
}
return $data;
}
......@@ -131,10 +124,12 @@ public static function encode($data) {
* @throws Symfony\Component\Yaml\Exception\ParseException
*/
public static function decode($raw) {
if (empty($raw)) {
return array();
$data = Yaml::parse($raw);
// A simple string is valid YAML for any reason.
if (!is_array($data)) {
return FALSE;
}
return Yaml::parse($raw);
return $data;
}
/**
......
......@@ -30,9 +30,9 @@ public function __construct(array $options = array());
* @param string $name
* The name of a configuration object to load.
*
* @return array
* @return array|bool
* The configuration data stored for the configuration object name. If no
* configuration data exists for the given name, an empty array is returned.
* configuration data exists for the given name, FALSE is returned.
*/
public function read($name);
......
......@@ -29,10 +29,13 @@ function testCRUD() {
$storage = new DatabaseStorage();
$name = 'config_test.crud';
// Create a new configuration object.
$config = config($name);
$this->assertIdentical($config->isNew(), TRUE);
// Create a new configuration object.
$config->set('value', 'initial');
$config->save();
$this->assertIdentical($config->isNew(), FALSE);
// Verify the active store contains the saved value.
$actual_data = $storage->read($name);
......@@ -41,6 +44,7 @@ function testCRUD() {
// Update the configuration object instance.
$config->set('value', 'instance-update');
$config->save();
$this->assertIdentical($config->isNew(), FALSE);
// Verify the active store contains the updated value.
$actual_data = $storage->read($name);
......@@ -49,24 +53,28 @@ function testCRUD() {
// Verify a call to config() immediately returns the updated value.
$new_config = config($name);
$this->assertIdentical($new_config->get(), $config->get());
$this->assertIdentical($config->isNew(), FALSE);
// Delete the configuration object.
$config->delete();
// Verify the configuration object is empty.
$this->assertIdentical($config->get(), array());
$this->assertIdentical($config->isNew(), TRUE);
// Verify the active store contains no value.
$actual_data = $storage->read($name);
$this->assertIdentical($actual_data, array());
$this->assertIdentical($actual_data, FALSE);
// Verify config() returns no data.
$new_config = config($name);
$this->assertIdentical($new_config->get(), $config->get());
$this->assertIdentical($config->isNew(), TRUE);
// Re-create the configuration object.
$config->set('value', 're-created');
$config->save();
$this->assertIdentical($config->isNew(), FALSE);
// Verify the active store contains the updated value.
$actual_data = $storage->read($name);
......@@ -75,6 +83,7 @@ function testCRUD() {
// Verify a call to config() immediately returns the updated value.
$new_config = config($name);
$this->assertIdentical($new_config->get(), $config->get());
$this->assertIdentical($config->isNew(), FALSE);
}
/**
......
......@@ -68,7 +68,7 @@ function testReadWriteConfig() {
// Verify nothing was saved.
$db_data = $database_storage->read($name);
$this->assertIdentical($db_data, array());
$this->assertIdentical($db_data, FALSE);
// Add a top level value
$config = config($name);
......@@ -181,7 +181,7 @@ function testReadWriteConfig() {
// Verify the database entry no longer exists.
$db_data = $database_storage->read($name);
$this->assertIdentical($db_data, array());
$this->assertIdentical($db_data, FALSE);
}
/**
......
......@@ -32,18 +32,18 @@ abstract class ConfigStorageTestBase extends WebTestBase {
function testCRUD() {
$name = 'config_test.storage';
// Reading a non-existing name returns an empty data array.
// Reading a non-existing name returns FALSE.
$data = $this->storage->read($name);
$this->assertIdentical($data, array());
$this->assertIdentical($data, FALSE);
// Reading a name containing non-decodeable data returns an empty array.
// Reading a name containing non-decodeable data returns FALSE.
$this->insert($name, '');
$data = $this->storage->read($name);
$this->assertIdentical($data, array());
$this->assertIdentical($data, FALSE);
$this->update($name, 'foo');
$data = $this->storage->read($name);
$this->assertIdentical($data, array());
$this->assertIdentical($data, FALSE);
$this->delete($name);
......@@ -76,9 +76,9 @@ function testCRUD() {
$result = $this->storage->delete($name);
$this->assertIdentical($result, FALSE);
// Reading from a non-existing storage bin returns an empty data array.
// Reading from a non-existing storage bin returns FALSE.
$data = $this->invalidStorage->read($name);
$this->assertIdentical($data, array());
$this->assertIdentical($data, FALSE);
// Writing to a non-existing storage bin throws an exception.
try {
......
......@@ -548,13 +548,11 @@ function image_styles() {
* @see image_effect_load()
*/
function image_style_load($name) {
$style = config('image.style.' . $name)->get();
// @todo Requires a more reliable + generic method to check for whether the
// configuration object exists.
if (!isset($style['name'])) {
$config = config('image.style.' . $name);
if ($config->isNew()) {
return FALSE;
}
$style = $config->get();
if (!empty($style['effects'])) {
foreach ($style['effects'] as $ieid => $effect) {
......@@ -579,6 +577,8 @@ function image_style_load($name) {
*/
function image_style_save($style) {
$config = config('image.style.' . $style['name']);
$is_new = $config->isNew();
$config->set('name', $style['name']);
if (isset($style['effects'])) {
$config->set('effects', $style['effects']);
......@@ -587,11 +587,9 @@ function image_style_save($style) {
$config->set('effects', array());
}
$config->save();
// @todo is_new must only be set when the configuration object did not exist
// yet.
$style['is_new'] = TRUE;
// Let other modules update as necessary on save.
$style['is_new'] = $is_new;
module_invoke_all('image_style_save', $style);
// Clear all caches and flush.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment