View source
<?php
namespace Drupal\config_actions;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigActionsPluginBase extends PluginBase implements ConfigActionsPluginInterface, ContainerFactoryPluginInterface {
protected $allowedOptions = [];
protected $pluginData = [];
protected $actionService;
protected $pluginId;
protected $id;
protected $key;
protected $source;
protected $dest;
protected $source_type;
protected $dest_type;
protected $base;
protected $module;
protected $template;
protected $auto;
protected $replace;
protected $replace_in = [];
protected $did_replace = [];
protected $config;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigActionsServiceInterface $config_action_service) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->actionService = $config_action_service;
$this->pluginId = $plugin_id;
$this->allowedOptions = $plugin_definition['options'];
$this->replace_in = $plugin_definition['replace_in'];
$this->pluginData = $plugin_definition['data'];
$this
->addAllowed([
'id' => '',
'key' => '',
'source' => '',
'source_type' => '',
'dest' => NULL,
'dest_type' => '',
'replace' => [],
'replace_in' => [],
'base' => '',
'module' => '',
'auto' => TRUE,
'template' => '',
]);
$this
->initPlugin($configuration);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$config_action_service = $container
->get('config_actions');
return new static($configuration, $plugin_id, $plugin_definition, $config_action_service);
}
protected function initPlugin(array $configuration) {
$this
->setOptions($configuration);
}
protected function addAllowed(array $allowed) {
$this->allowedOptions = array_merge($this->allowedOptions, $allowed);
}
public function setOptions(array $options) {
$default_list = [];
foreach ($options as $key => $value) {
if (preg_match('/^\\@[A-Za-z0-9_\\-]+\\@$/', $key) === 1) {
if (!isset($options['replace'][$key])) {
$options['replace'][$key] = $value;
}
unset($options[$key]);
}
}
foreach ($this->allowedOptions as $key => $default) {
if (array_key_exists($key, $options)) {
$this->{$key} = $options[$key];
}
elseif (property_exists($this, $key) && is_null($this->{$key})) {
$this->{$key} = $default;
$default_list[] = $key;
}
}
$parsed_options = $this
->parseOptions($options, [
'source',
'dest',
]);
if (!empty($this->key)) {
$this->replace = array_merge(ConfigActionsTransform::parseWildcards($this->key, $this->id), $this->replace);
}
if (!empty($this->replace_in)) {
foreach ($this->replace_in as $option) {
if (isset($parsed_options[$option])) {
$this->{$option} = $this
->replaceData($parsed_options[$option], $this->replace, $option);
}
}
}
if (!isset($this->dest) && !is_array($this->source)) {
$this->dest = $this->source;
$this->dest_type = $this->source_type;
}
}
public function getOption($name) {
return isset($this->{$name}) ? $this->{$name} : NULL;
}
protected function isSequential($arr) {
if (array() === $arr) {
return false;
}
return is_array($arr) && array_keys($arr) == range(0, count($arr) - 1);
}
public function parseOptions(array $options, array $setKeys = []) {
$result = [];
if (!empty($this->module)) {
$this->base = DRUPAL_ROOT . '/' . drupal_get_path('module', $this->module);
}
$replacements = [];
foreach ($this->allowedOptions as $key => $default) {
if (isset($this->{$key}) && is_string($this->{$key})) {
$replacements["@{$key}@"] = $this->{$key};
}
}
foreach ($this->allowedOptions as $key => $default) {
if (isset($this->{$key})) {
$result[$key] = $this
->replaceData($this->{$key}, $replacements);
if ((is_string($this->{$key}) || $this
->isSequential($this->{$key})) && in_array($key, $setKeys)) {
$this->{$key} = $result[$key];
}
}
}
if (!empty($this->replace)) {
foreach ($this->replace as $key => $value) {
if (isset($value) && !empty($replacements)) {
$this->replace[$key] = ConfigActionsTransform::replace($value, $replacements);
}
}
}
return $result;
}
protected function getData($property) {
if (array_key_exists($property, $this->pluginData)) {
return $this->pluginData[$property];
}
}
protected function replaceData($data, $replacements = NULL, $in = '') {
$replacements = isset($replacements) ? $replacements : (!empty($this->replace) ? $this->replace : []);
$replace = [];
$replace_keys = [];
foreach ($replacements as $pattern => $value) {
if (is_array($value)) {
$replace_in = isset($value['in']) ? $value['in'] : $this->replace_in;
$pattern = isset($value['pattern']) ? $value['pattern'] : $pattern;
if (empty($in) || in_array($in, $replace_in)) {
$with = isset($value['with']) ? $value['with'] : '';
$type = isset($value['type']) ? $value['type'] : 'value';
if (is_string($type)) {
$type = explode(',', $type);
}
if (in_array('key', $type)) {
$replace_keys[$pattern] = $with;
}
if (in_array('value', $type)) {
$replace[$pattern] = $with;
}
}
}
else {
$replace[$pattern] = $value;
$replace_keys[$pattern] = $value;
}
}
return ConfigActionsTransform::replace($data, $replace, $replace_keys);
}
public function transform(array $source) {
return $source;
}
public function execute(array $action) {
if (!empty($this->template)) {
$this->replace = array_merge($this->replace, ConfigActionsTransform::parseWildcards($this->template, $this->dest));
if (is_array($this->source)) {
$this->source[] = 'template::' . $this->template;
}
else {
$this->source = $this->template;
$this->source_type = 'template';
}
}
$tree = $this->actionService
->loadSource($this->source, $this->source_type, $this->base);
if (!is_array($tree)) {
$tree = [];
}
if (in_array('load', $this->replace_in)) {
$tree = $this
->replaceData($tree, $this->replace, 'load');
}
$tree = $this
->transform($tree);
if (in_array('save', $this->replace_in)) {
$tree = $this
->replaceData($tree, $this->replace, 'save');
}
$this->actionService
->saveSource($tree, $this->dest, $this->dest_type, $this->base, is_array($this->source));
return $tree;
}
}