Commit 3ee3f5ad authored by catch's avatar catch

Issue #1764278 by tstoeckler, tim.plunkett, yched: Fixed Run...

Issue #1764278 by tstoeckler, tim.plunkett, yched: Fixed Run PluginManagerBase::processDefinition() in a ProcessDecorator.
parent cde72309
<?php
/**
* @file
* Contains \Drupal\Component\Plugin\Discovery\ProcessDecorator.
*/
namespace Drupal\Component\Plugin\Discovery;
/**
* Allows custom processing of the discovered definition.
*
* Example use cases include adding in default values for a definition, or
* providing a backwards compatibility layer for renamed definition properties.
*/
class ProcessDecorator implements DiscoveryInterface {
/**
* The Discovery object being decorated.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
*/
protected $decorated;
/**
* The processor callback to run on each discovered definition.
*
* @var callable
*/
protected $processCallback;
/**
* Constructs a \Drupal\Component\Plugin\Discovery\ProcessDecorator object.
*
* @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
* The discovery object that is being decorated.
* @param callable $process_callback
* The processor callback to run on each discovered definition. The
* callback will be called with the following arguments:
* - array $definition: the discovered definition, that the callback
* should accept by reference and modify in place.
* - string $plugin_id: the corresponding plugin_id.
*/
public function __construct(DiscoveryInterface $decorated, $process_callback) {
$this->decorated = $decorated;
$this->processCallback = $process_callback;
}
/**
* Implements \Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition().
*/
public function getDefinition($plugin_id) {
$definitions = $this->getDefinitions();
if (isset($definitions[$plugin_id])) {
return $definitions[$plugin_id];
}
}
/**
* Implements \Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions().
*/
public function getDefinitions() {
$definitions = $this->decorated->getDefinitions();
foreach ($definitions as $plugin_id => &$definition) {
call_user_func_array($this->processCallback, array(&$definition, $plugin_id));
}
return $definitions;
}
/**
* Passes through all unknown calls onto the decorated object.
*/
public function __call($method, $args) {
return call_user_func_array(array($this->decorated, $method), $args);
}
}
......@@ -49,23 +49,14 @@ abstract class PluginManagerBase implements PluginManagerInterface, CachedDiscov
* Implements Drupal\Component\Plugin\PluginManagerInterface::getDefinition().
*/
public function getDefinition($plugin_id) {
$definition = $this->discovery->getDefinition($plugin_id);
if (isset($definition)) {
$this->processDefinition($definition, $plugin_id);
}
return $definition;
return $this->discovery->getDefinition($plugin_id);;
}
/**
* Implements Drupal\Component\Plugin\PluginManagerInterface::getDefinitions().
*/
public function getDefinitions() {
$definitions = $this->discovery->getDefinitions();
foreach ($definitions as $plugin_id => &$definition) {
$this->processDefinition($definition, $plugin_id);
}
return $definitions;
return $this->discovery->getDefinitions();
}
/**
......@@ -98,7 +89,7 @@ public function getInstance(array $options) {
* additional processing logic they can do that by replacing or extending the
* method.
*/
protected function processDefinition(&$definition, $plugin_id) {
public function processDefinition(&$definition, $plugin_id) {
$definition = NestedArray::mergeDeep($this->defaults, $definition);
}
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
......@@ -228,7 +229,11 @@ class EntityManager extends PluginManagerBase {
*/
public function __construct() {
// Allow the plugin definition to be altered by hook_entity_info_alter().
$this->discovery = new AlterDecorator(new InfoHookDecorator(new AnnotatedClassDiscovery('Core', 'Entity'), 'entity_info'), 'entity_info');
$this->discovery = new AnnotatedClassDiscovery('Core', 'Entity');
$this->discovery = new InfoHookDecorator($this->discovery, 'entity_info');
$this->discovery = new AlterDecorator($this->discovery, 'entity_info');
// @todo Run process before altering, see http://drupal.org/node/1848964.
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->factory = new DefaultFactory($this);
// Entity type plugins includes translated strings, so each language is
......@@ -266,7 +271,7 @@ public function getDefinitions() {
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase::processDefinition().
*/
protected function processDefinition(&$definition, $plugin_id) {
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
// @todo Remove this check once http://drupal.org/node/1780396 is resolved.
......
......@@ -8,6 +8,7 @@
namespace Drupal\field\Plugin\Type\Formatter;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\field\Plugin\Type\Formatter\FormatterLegacyDiscoveryDecorator;
......@@ -32,6 +33,7 @@ class FormatterPluginManager extends PluginManagerBase {
public function __construct() {
$this->discovery = new AnnotatedClassDiscovery('field', 'formatter');
$this->discovery = new FormatterLegacyDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new AlterDecorator($this->discovery, 'field_formatter_info');
$this->discovery = new CacheDecorator($this->discovery, 'field_formatter_types', 'field');
......
......@@ -8,6 +8,7 @@
namespace Drupal\field\Plugin\Type\Widget;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
......@@ -34,6 +35,7 @@ class WidgetPluginManager extends PluginManagerBase {
public function __construct() {
$this->discovery = new AnnotatedClassDiscovery('field', 'widget');
$this->discovery = new WidgetLegacyDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new AlterDecorator($this->discovery, 'field_widget_info');
$this->discovery = new CacheDecorator($this->discovery, 'field_widget_types', 'field');
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Component\Plugin\Factory\ReflectionFactory;
......@@ -26,7 +27,10 @@ class LayoutManager extends PluginManagerBase {
*/
public function __construct() {
// Create layout plugin derivatives from declaratively defined layouts.
$this->discovery = new DerivativeDiscoveryDecorator(new AnnotatedClassDiscovery('layout', 'layout'));
$this->discovery = new AnnotatedClassDiscovery('layout', 'layout');
$this->discovery = new DerivativeDiscoveryDecorator($this->discovery);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->factory = new ReflectionFactory($this);
}
}
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Discovery\StaticDiscovery;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Component\Plugin\Factory\DefaultFactory;
/**
......@@ -22,6 +23,7 @@ public function __construct() {
// discovery implementation, but StaticDiscovery lets us add some simple
// mock plugins for unit testing.
$this->discovery = new StaticDiscovery();
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'ProcessDefinition'));
$this->factory = new DefaultFactory($this);
// Specify default values.
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
......@@ -19,7 +20,11 @@ class JoinManager extends PluginManagerBase {
* Constructs a JoinManager object.
*/
public function __construct() {
$this->discovery = new CacheDecorator(new AlterDecorator(new AnnotatedClassDiscovery('views', 'join'), 'views_plugins_join'), 'views:join', 'views_info');
$this->discovery = new AnnotatedClassDiscovery('views', 'join');
$this->discovery = new AlterDecorator($this->discovery, 'views_plugins_join');
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new CacheDecorator($this->discovery, 'views:join', 'views_info');
$this->factory = new DefaultFactory($this);
$this->defaults = array(
'module' => 'views',
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
......@@ -19,7 +20,11 @@ class PluginManager extends PluginManagerBase {
* Constructs a PluginManager object.
*/
public function __construct($type) {
$this->discovery = new CacheDecorator(new AlterDecorator(new AnnotatedClassDiscovery('views', $type), 'views_plugins_' . $type), 'views:' . $type, 'views_info');
$this->discovery = new AnnotatedClassDiscovery('views', $type);
$this->discovery = new AlterDecorator($this->discovery, 'views_plugins_' . $type);
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new CacheDecorator($this->discovery, 'views:' . $type, 'views_info');
$this->factory = new DefaultFactory($this);
$this->defaults += array(
'parent' => 'parent',
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
......@@ -19,7 +20,10 @@ class WizardManager extends PluginManagerBase {
* Constructs a WizardManager object.
*/
public function __construct() {
$this->discovery = new CacheDecorator(new AlterDecorator(new AnnotatedClassDiscovery('views', 'wizard'), 'views_plugins_wizard'), 'views:wizard', 'views_info');
$this->discovery = new AnnotatedClassDiscovery('views', 'wizard');
$this->discovery = new AlterDecorator($this->discovery, 'views_plugins_wizard');
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new CacheDecorator($this->discovery, 'views:wizard', 'views_info');
$this->factory = new DefaultFactory($this);
$this->defaults = array(
'module' => 'views',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment