View source
<?php
namespace Drupal\Core\Config;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class DatabaseStorage implements StorageInterface {
use DependencySerializationTrait;
protected $connection;
protected $table;
protected $options = [];
protected $collection = StorageInterface::DEFAULT_COLLECTION;
public function __construct(Connection $connection, $table, array $options = [], $collection = StorageInterface::DEFAULT_COLLECTION) {
$this->connection = $connection;
$this->table = $table;
$this->options = $options;
$this->collection = $collection;
}
public function exists($name) {
try {
return (bool) $this->connection
->queryRange('SELECT 1 FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :name', 0, 1, [
':collection' => $this->collection,
':name' => $name,
], $this->options)
->fetchField();
} catch (\Exception $e) {
return FALSE;
}
}
public function read($name) {
$data = FALSE;
try {
$raw = $this->connection
->query('SELECT [data] FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :name', [
':collection' => $this->collection,
':name' => $name,
], $this->options)
->fetchField();
if ($raw !== FALSE) {
$data = $this
->decode($raw);
}
} catch (\Exception $e) {
}
return $data;
}
public function readMultiple(array $names) {
$list = [];
try {
$list = $this->connection
->query('SELECT [name], [data] FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] IN ( :names[] )', [
':collection' => $this->collection,
':names[]' => $names,
], $this->options)
->fetchAllKeyed();
foreach ($list as &$data) {
$data = $this
->decode($data);
}
} catch (\Exception $e) {
}
return $list;
}
public function write($name, array $data) {
$data = $this
->encode($data);
try {
return $this
->doWrite($name, $data);
} catch (\Exception $e) {
if ($this
->ensureTableExists()) {
return $this
->doWrite($name, $data);
}
throw $e;
}
}
protected function doWrite($name, $data) {
$options = [
'return' => Database::RETURN_AFFECTED,
] + $this->options;
return (bool) $this->connection
->merge($this->table, $options)
->keys([
'collection',
'name',
], [
$this->collection,
$name,
])
->fields([
'data' => $data,
])
->execute();
}
protected function ensureTableExists() {
try {
if (!$this->connection
->schema()
->tableExists($this->table)) {
$this->connection
->schema()
->createTable($this->table, static::schemaDefinition());
return TRUE;
}
} catch (DatabaseException $e) {
return TRUE;
} catch (\Exception $e) {
throw new StorageException($e
->getMessage(), 0, $e);
}
return FALSE;
}
protected static function schemaDefinition() {
$schema = [
'description' => 'The base table for configuration data.',
'fields' => [
'collection' => [
'description' => 'Primary Key: Config object collection.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => 'Primary Key: Config object name.',
'type' => 'varchar_ascii',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'data' => [
'description' => 'A serialized configuration object data.',
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
],
],
'primary key' => [
'collection',
'name',
],
];
return $schema;
}
public function delete($name) {
$options = [
'return' => Database::RETURN_AFFECTED,
] + $this->options;
return (bool) $this->connection
->delete($this->table, $options)
->condition('collection', $this->collection)
->condition('name', $name)
->execute();
}
public function rename($name, $new_name) {
$options = [
'return' => Database::RETURN_AFFECTED,
] + $this->options;
return (bool) $this->connection
->update($this->table, $options)
->fields([
'name' => $new_name,
])
->condition('name', $name)
->condition('collection', $this->collection)
->execute();
}
public function encode($data) {
return serialize($data);
}
public function decode($raw) {
$data = @unserialize($raw);
return is_array($data) ? $data : FALSE;
}
public function listAll($prefix = '') {
try {
$query = $this->connection
->select($this->table);
$query
->fields($this->table, [
'name',
]);
$query
->condition('collection', $this->collection, '=');
$query
->condition('name', $prefix . '%', 'LIKE');
$query
->orderBy('collection')
->orderBy('name');
return $query
->execute()
->fetchCol();
} catch (\Exception $e) {
return [];
}
}
public function deleteAll($prefix = '') {
try {
$options = [
'return' => Database::RETURN_AFFECTED,
] + $this->options;
return (bool) $this->connection
->delete($this->table, $options)
->condition('name', $prefix . '%', 'LIKE')
->condition('collection', $this->collection)
->execute();
} catch (\Exception $e) {
return FALSE;
}
}
public function createCollection($collection) {
return new static($this->connection, $this->table, $this->options, $collection);
}
public function getCollectionName() {
return $this->collection;
}
public function getAllCollectionNames() {
try {
return $this->connection
->query('SELECT DISTINCT [collection] FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] <> :collection ORDER by [collection]', [
':collection' => StorageInterface::DEFAULT_COLLECTION,
])
->fetchCol();
} catch (\Exception $e) {
return [];
}
}
}