View source
<?php
namespace Drupal\Tests\config_split\Kernel;
use Drupal\config_split\Config\SplitCollectionStorage;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\MemoryStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Site\Settings;
trait SplitTestTrait {
protected function createSplitConfig(string $name, array $data) : Config {
if (substr($name, 0, strlen('config_split.config_split.')) !== 'config_split.config_split.') {
$name = 'config_split.config_split.' . $name;
}
$data += [
'storage' => isset($data['folder']) && $data['folder'] != '' ? 'folder' : 'database',
'status' => TRUE,
'weight' => 0,
'folder' => isset($data['storage']) && $data['storage'] == 'folder' ? Settings::get('file_public_path') . '/config/split' : '',
'module' => [],
'theme' => [],
'complete_list' => [],
'partial_list' => [],
];
$data['id'] = substr($name, strlen('config_split.config_split.'));
$config = new Config($name, $this->container
->get('config.storage'), $this->container
->get('event_dispatcher'), $this->container
->get('config.typed'));
$config
->initWithData($data)
->save();
return $config;
}
protected function getSplitSourceStorage(Config $config) : StorageInterface {
switch ($config
->get('storage')) {
case 'folder':
return new FileStorage($config
->get('folder'));
case 'collection':
return new SplitCollectionStorage($this
->getSyncFileStorage(), $config
->get('id'));
case 'database':
return new DatabaseStorage($this->container
->get('database'), strtr($config
->getName(), [
'.' => '_',
]));
}
throw new \LogicException();
}
protected function getSplitPreviewStorage(Config $config, StorageInterface $export = NULL) : StorageInterface {
if ('collection' === $config
->get('storage')) {
if ($export === NULL) {
throw new \InvalidArgumentException();
}
return new SplitCollectionStorage($export, $config
->get('id'));
}
$name = substr($config
->getName(), strlen('config_split.config_split.'));
$storage = new DatabaseStorage($this->container
->get('database'), 'config_split_preview_' . strtr($name, [
'.' => '_',
]));
$memory = new MemoryStorage();
$this
->copyConfig($storage, $memory);
return $memory;
}
}