diff --git a/core/lib/Drupal/Core/Config/DatabaseStorage.php b/core/lib/Drupal/Core/Config/DatabaseStorage.php index 0d4796f..dad9a19 100644 --- a/core/lib/Drupal/Core/Config/DatabaseStorage.php +++ b/core/lib/Drupal/Core/Config/DatabaseStorage.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Core\Config\DatabaseStorage. + * Contains \Drupal\Core\Config\DatabaseStorage. */ namespace Drupal\Core\Config; @@ -73,17 +73,10 @@ public function exists($name) { } /** - * Implements Drupal\Core\Config\StorageInterface::read(). - * - * @throws PDOException - * @throws \Drupal\Core\Database\DatabaseExceptionWrapper - * Only thrown in case $this->options['throw_exception'] is TRUE. + * {@inheritdoc} */ public function read($name) { $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 FALSE so the caller can handle it. try { $raw = $this->connection->query('SELECT data FROM {' . $this->connection->escapeTable($this->table) . '} WHERE name = :name', array(':name' => $name), $this->options)->fetchField(); if ($raw !== FALSE) { @@ -91,9 +84,9 @@ public function read($name) { } } catch (\Exception $e) { - if (!empty($this->options['no_table_creation'])) { - throw $e; - } + // 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 FALSE so the caller can handle it. } return $data; } @@ -102,10 +95,6 @@ public function read($name) { * {@inheritdoc} */ public function readMultiple(array $names) { - // 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 - // handle it if need be. $list = array(); try { $list = $this->connection->query('SELECT name, data FROM {' . $this->connection->escapeTable($this->table) . '} WHERE name IN (:names)', array(':names' => $names), $this->options)->fetchAllKeyed(); @@ -114,9 +103,10 @@ public function readMultiple(array $names) { } } catch (\Exception $e) { - if (!empty($this->options['no_table_creation'])) { - throw $e; - } + // 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 + // handle it if need be. } return $list; } @@ -135,7 +125,7 @@ public function write($name, array $data) { } catch (\Exception $e) { // If there was an exception, try to create the table. - if ($this->ensureTableExists()) { + if (empty($this->options['throw_exception']) && $this->ensureTableExists()) { return $this->doWrite($name, $data); } // Some other failure that we can not recover from. @@ -171,7 +161,7 @@ protected function doWrite($name, $data) { */ protected function ensureTableExists() { try { - if (!$this->connection->schema()->tableExists($this->table) && empty($this->options['no_table_creation'])) { + if (!$this->connection->schema()->tableExists($this->table)) { $this->connection->schema()->createTable($this->table, static::schemaDefinition()); return TRUE; } @@ -258,41 +248,33 @@ public function decode($raw) { } /** - * Implements Drupal\Core\Config\StorageInterface::listAll(). - * - * @throws PDOException - * @throws \Drupal\Core\Database\DatabaseExceptionWrapper - * Only thrown in case $this->options['throw_exception'] is TRUE. + * {@inheritdoc} */ public function listAll($prefix = '') { - // 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 - // handle it if need be. try { return $this->connection->query('SELECT name FROM {' . $this->connection->escapeTable($this->table) . '} WHERE name LIKE :name', array( ':name' => $this->connection->escapeLike($prefix) . '%', ), $this->options)->fetchCol(); } catch (\Exception $e) { - if (!empty($this->options['no_table_creation'])) { - throw $e; - } - return array(); + // Convert any errors to the expected exception. + throw new StorageException($e->getMessage(), 0, $e); } } /** - * Implements Drupal\Core\Config\StorageInterface::deleteAll(). - * - * @throws PDOException - * @throws \Drupal\Core\Database\DatabaseExceptionWrapper - * Only thrown in case $this->options['throw_exception'] is TRUE. + * {@inheritdoc} */ public function deleteAll($prefix = '') { - $options = array('return' => Database::RETURN_AFFECTED) + $this->options; - return (bool) $this->connection->delete($this->table, $options) - ->condition('name', $prefix . '%', 'LIKE') - ->execute(); + try { + $options = array('return' => Database::RETURN_AFFECTED) + $this->options; + return (bool) $this->connection->delete($this->table, $options) + ->condition('name', $prefix . '%', 'LIKE') + ->execute(); + } + catch (\Exception $e) { + // Convert any errors to the expected exception. + throw new StorageException($e->getMessage(), 0, $e); + } } } diff --git a/core/lib/Drupal/Core/Config/StorageInterface.php b/core/lib/Drupal/Core/Config/StorageInterface.php index 9b45fa8..09cc032 100644 --- a/core/lib/Drupal/Core/Config/StorageInterface.php +++ b/core/lib/Drupal/Core/Config/StorageInterface.php @@ -41,7 +41,7 @@ public function read($name); /** * Reads configuration data from the storage. * - * @param array $name + * @param array $names * List of names of the configuration objects to load. * * @return array @@ -131,6 +131,9 @@ public function decode($raw); * * @return array * An array containing matching configuration object names. + * + * @throws \Drupal\Core\Config\StorageException + * If there is an error accessing the storage. */ public function listAll($prefix = ''); @@ -150,6 +153,9 @@ public function listAll($prefix = ''); * * @return boolean * TRUE on success, FALSE otherwise. + * + * @throws \Drupal\Core\Config\StorageException + * If there is an error accessing the storage. */ public function deleteAll($prefix = ''); diff --git a/core/modules/config/lib/Drupal/config/Tests/Storage/DatabaseStorageTest.php b/core/modules/config/lib/Drupal/config/Tests/Storage/DatabaseStorageTest.php index 213ad3a..28987e6 100644 --- a/core/modules/config/lib/Drupal/config/Tests/Storage/DatabaseStorageTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/Storage/DatabaseStorageTest.php @@ -25,7 +25,7 @@ function setUp() { parent::setUp(); $this->storage = new DatabaseStorage($this->container->get('database'), 'config'); - $this->invalidStorage = new DatabaseStorage($this->container->get('database'), 'invalid', array('no_table_creation' => TRUE)); + $this->invalidStorage = new DatabaseStorage($this->container->get('database'), 'invalid', array('throw_exception' => TRUE)); // ::listAll() verifications require other configuration data to exist. $this->storage->write('system.performance', array());