ConfigDevelAutoExportSubscriber.php in Configuration development 8
File
src/EventSubscriber/ConfigDevelAutoExportSubscriber.php
View source
<?php
namespace Drupal\config_devel\EventSubscriber;
use Drupal\config_devel\Event\ConfigDevelEvents;
use Drupal\config_devel\Event\ConfigDevelSaveEvent;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Yaml\Exception\DumpException;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigRenameEvent;
use Drupal\Core\Config\ConfigEvents;
class ConfigDevelAutoExportSubscriber extends ConfigDevelSubscriberBase implements EventSubscriberInterface {
protected $eventDispatcher;
public function __construct(ConfigFactoryInterface $config_factory, ConfigManagerInterface $config_manager, EventDispatcherInterface $event_dispatcher) {
parent::__construct($config_factory, $config_manager);
$this->configFactory = $config_factory;
$this->configManager = $config_manager;
$this->eventDispatcher = $event_dispatcher;
}
protected $autoExportFiles;
public function onConfigSave(ConfigCrudEvent $event) {
$this
->autoExportConfig($event
->getConfig());
}
public function onConfigRename(ConfigRenameEvent $event) {
$this
->autoExportConfig($event
->getConfig());
}
protected function autoExportConfig(Config $config) {
$config_name = $config
->getName();
$file_names = array_filter($this
->getSettings()
->get('auto_export') ?: [], function ($file_name) use ($config_name) {
return basename($file_name, '.' . FileStorage::getFileExtension()) == $config_name;
});
$this
->writeBackConfig($config, $file_names);
}
public function writeBackConfig(Config $config, array $file_names) {
if ($file_names) {
$data = $config
->get();
$config_name = $config
->getName();
unset($data['_core']);
if ($entity_type_id = $this->configManager
->getEntityTypeIdByName($config_name)) {
unset($data['uuid']);
}
$event = new ConfigDevelSaveEvent($file_names, $data);
$this->eventDispatcher
->dispatch(ConfigDevelEvents::SAVE, $event);
$data = $event
->getData();
$file_names = $event
->getFileNames();
foreach ($file_names as $file_name) {
try {
file_put_contents($file_name, (new InstallStorage())
->encode($data));
} catch (DumpException $e) {
}
}
}
}
static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = array(
'onConfigSave',
10,
);
$events[ConfigEvents::RENAME][] = array(
'onConfigRename',
10,
);
return $events;
}
}