diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index 0748b76..a749a4b 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -20,6 +20,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. * * @var array @@ -60,6 +67,13 @@ class Config { } /** + * Returns whether this configuration object is new. + */ + public function isNew() { + return $this->isNew; + } + + /** * Gets data from this config object. * * @param $key @@ -208,9 +222,13 @@ class Config { * 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 @@ class Config { 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 @@ class Config { public function delete() { $this->data = array(); $this->storageDispatcher->selectStorage('write', $this->name)->delete($this->name); + $this->isNew = TRUE; return $this; } } diff --git a/core/lib/Drupal/Core/Config/DatabaseStorage.php b/core/lib/Drupal/Core/Config/DatabaseStorage.php index 1dd8507..f30011c 100644 --- a/core/lib/Drupal/Core/Config/DatabaseStorage.php +++ b/core/lib/Drupal/Core/Config/DatabaseStorage.php @@ -51,7 +51,7 @@ class DatabaseStorage implements StorageInterface { * 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 @@ class DatabaseStorage implements StorageInterface { */ public static function decode($raw) { $data = @unserialize($raw); - return $data !== FALSE ? $data : array(); + return is_array($data) ? $data : FALSE; } /** diff --git a/core/lib/Drupal/Core/Config/FileStorage.php b/core/lib/Drupal/Core/Config/FileStorage.php index 7827dc0..033555d 100644 --- a/core/lib/Drupal/Core/Config/FileStorage.php +++ b/core/lib/Drupal/Core/Config/FileStorage.php @@ -70,19 +70,12 @@ class FileStorage implements StorageInterface { */ 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 @@ class FileStorage implements StorageInterface { * @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; } /** diff --git a/core/lib/Drupal/Core/Config/StorageInterface.php b/core/lib/Drupal/Core/Config/StorageInterface.php index 907e884..806ee87 100644 --- a/core/lib/Drupal/Core/Config/StorageInterface.php +++ b/core/lib/Drupal/Core/Config/StorageInterface.php @@ -30,9 +30,9 @@ interface StorageInterface { * @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); diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php index aeca024..37aa854 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigCRUDTest.php @@ -29,10 +29,13 @@ class ConfigCRUDTest extends WebTestBase { $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 @@ class ConfigCRUDTest extends WebTestBase { // 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 @@ class ConfigCRUDTest extends WebTestBase { // 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 @@ class ConfigCRUDTest extends WebTestBase { // 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); } /** diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php index 3add6d8..abbd2ae 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigFileContentTest.php @@ -68,7 +68,7 @@ class ConfigFileContentTest extends WebTestBase { // 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 @@ class ConfigFileContentTest extends WebTestBase { // Verify the database entry no longer exists. $db_data = $database_storage->read($name); - $this->assertIdentical($db_data, array()); + $this->assertIdentical($db_data, FALSE); } /** diff --git a/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php index 3e6b254..2dbc627 100644 --- a/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/ConfigStorageTestBase.php @@ -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 @@ abstract class ConfigStorageTestBase extends WebTestBase { $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 { diff --git a/core/modules/image/image.module b/core/modules/image/image.module index f3bf83d..79fdc88 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -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) {