diff --git a/core/core.services.yml b/core/core.services.yml index 9dca742..f25e3aa 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -151,9 +151,15 @@ services: plugin.manager.entity: class: Drupal\Core\Entity\EntityManager arguments: ['@container.namespaces'] + calls: + - [setAlterHook, ['@module_handler', 'entity_info']] + - [setCache, ['@cache.cache', 'entity_info']] plugin.manager.archiver: class: Drupal\Core\Archiver\ArchiverManager arguments: ['@container.namespaces'] + calls: + - [setAlterHook, ['@module_handler', 'archiver_info']] + - [setCache, ['@cache.block', 'archiver_info']] request: class: Symfony\Component\HttpFoundation\Request event_dispatcher: @@ -179,6 +185,9 @@ services: validation.constraint: class: Drupal\Core\Validation\ConstraintManager arguments: ['@container.namespaces'] + calls: + - [setAlterHook, ['@module_handler', 'validation_constraint']] + - [setCache, ['@cache.cache', 'validation_constraint']] lock: class: Drupal\Core\Lock\DatabaseLockBackend user.tempstore: @@ -399,6 +408,9 @@ services: plugin.manager.condition: class: Drupal\Core\Condition\ConditionManager arguments: ['@container.namespaces'] + calls: + - [setAlterHook, ['@module_handler', 'condition_info']] + - [setCache, ['@cache.cache', 'condition']] kernel_destruct_subscriber: class: Drupal\Core\EventSubscriber\KernelDestructionSubscriber tags: @@ -412,6 +424,8 @@ services: image.toolkit.manager: class: Drupal\system\Plugin\ImageToolkitManager arguments: ['@container.namespaces'] + calls: + - [setCache, ['@cache.cache', 'image_toolkit']] image.toolkit: class: Drupal\system\Plugin\ImageToolkitInterface factory_method: getDefaultToolkit diff --git a/core/lib/Drupal/Core/Archiver/ArchiverManager.php b/core/lib/Drupal/Core/Archiver/ArchiverManager.php index f94869f..51a3ff8 100644 --- a/core/lib/Drupal/Core/Archiver/ArchiverManager.php +++ b/core/lib/Drupal/Core/Archiver/ArchiverManager.php @@ -7,15 +7,11 @@ namespace Drupal\Core\Archiver; use Drupal\Component\Plugin\Factory\DefaultFactory; -use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Core\Plugin\Discovery\AlterDecorator; -use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; -use Drupal\Core\Plugin\Discovery\CacheDecorator; - +use Drupal\Core\Plugin\DefaultPluginManager; /** * Archiver plugin manager. */ -class ArchiverManager extends PluginManagerBase { +class ArchiverManager extends DefaultPluginManager { /** * Constructs a ArchiverManager object. @@ -25,9 +21,7 @@ class ArchiverManager extends PluginManagerBase { * keyed by the corresponding namespace to look for plugin implementations, */ public function __construct(\Traversable $namespaces) { - $this->discovery = new AnnotatedClassDiscovery('Archiver', $namespaces); - $this->discovery = new AlterDecorator($this->discovery, 'archiver_info'); - $this->discovery = new CacheDecorator($this->discovery, 'archiver_info'); + parent::__construct('Archiver', $namespaces); } /** diff --git a/core/lib/Drupal/Core/Condition/ConditionManager.php b/core/lib/Drupal/Core/Condition/ConditionManager.php index 4131330..185a86b 100644 --- a/core/lib/Drupal/Core/Condition/ConditionManager.php +++ b/core/lib/Drupal/Core/Condition/ConditionManager.php @@ -12,6 +12,7 @@ use Drupal\Core\Executable\ExecutableInterface; use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; +use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Plugin\Discovery\AlterDecorator; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; use Drupal\Core\Plugin\Discovery\CacheDecorator; @@ -19,7 +20,7 @@ /** * A plugin manager for condition plugins. */ -class ConditionManager extends PluginManagerBase implements ExecutableManagerInterface { +class ConditionManager extends DefaultPluginManager implements ExecutableManagerInterface { /** * Constructs aa ConditionManager object. @@ -29,12 +30,7 @@ class ConditionManager extends PluginManagerBase implements ExecutableManagerInt * keyed by the corresponding namespace to look for plugin implementations, */ public function __construct(\Traversable $namespaces) { - $this->discovery = new AnnotatedClassDiscovery('Condition', $namespaces); - $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); - $this->discovery = new AlterDecorator($this->discovery, 'condition_info'); - $this->discovery = new CacheDecorator($this->discovery, 'condition:' . language(LANGUAGE_TYPE_INTERFACE)->langcode); - - $this->factory = new DefaultFactory($this); + parent::__construct('Condition', $namespaces); } /** diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 9e1d12f..11c3a10 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -7,13 +7,7 @@ namespace Drupal\Core\Entity; -use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Component\Plugin\Factory\DefaultFactory; -use Drupal\Core\Plugin\Discovery\AlterDecorator; -use Drupal\Core\Plugin\Discovery\CacheDecorator; -use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; -use Drupal\Core\Plugin\Discovery\InfoHookDecorator; -use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Plugin\DefaultPluginManager; /** * Manages entity type plugin definitions. @@ -29,7 +23,7 @@ * @see entity_get_info() * @see hook_entity_info_alter() */ -class EntityManager extends PluginManagerBase { +class EntityManager extends DefaultPluginManager { /** * Contains instantiated controllers keyed by controller type and entity type. @@ -50,12 +44,7 @@ public function __construct(\Traversable $namespaces) { $annotation_namespaces = array( 'Drupal\Core\Entity\Annotation' => DRUPAL_ROOT . '/core/lib', ); - $this->discovery = new AnnotatedClassDiscovery('Core/Entity', $namespaces, $annotation_namespaces, 'Drupal\Core\Entity\Annotation\EntityType'); - $this->discovery = new InfoHookDecorator($this->discovery, 'entity_info'); - $this->discovery = new AlterDecorator($this->discovery, 'entity_info'); - $this->discovery = new CacheDecorator($this->discovery, 'entity_info:' . language(LANGUAGE_TYPE_INTERFACE)->langcode, 'cache', CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE)); - - $this->factory = new DefaultFactory($this->discovery); + parent::__construct('Core/Entity', $namespaces, $annotation_namespaces, 'Drupal\Core\Entity\Annotation\EntityType'); } /** diff --git a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php index ce1cf0d..856cefa 100644 --- a/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php +++ b/core/lib/Drupal/Core/Plugin/DefaultPluginManager.php @@ -7,50 +7,20 @@ namespace Drupal\Core\Plugin; +use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface; use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\CacheBackendInterface; -use Drupal\Core\Extension\ModuleHandler; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; /** * Base class for plugin managers. */ -class DefaultPluginManager extends PluginManagerBase implements PluginManagerInterface { - - /** - * The object that discovers plugins managed by this manager. - * - * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface - */ - protected $discovery; - - /** - * The object that instantiates plugins managed by this manager. - * - * @var \Drupal\Component\Plugin\Factory\FactoryInterface - */ - protected $factory; - - /** - * The object that returns the preconfigured plugin instance appropriate for - * a particular runtime condition. - * - * @var \Drupal\Component\Plugin\Mapper\MapperInterface - */ - protected $mapper; - - /** - * A set of defaults to be referenced by $this->processDefinition() if - * additional processing of plugins is necessary or helpful for development - * purposes. - * - * @var array - */ - protected $defaults = array(); +class DefaultPluginManager extends PluginManagerBase implements PluginManagerInterface, CachedDiscoveryInterface { /** * Cached definitions array. @@ -113,10 +83,16 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt * @param \Traversable $namespaces * An object that implements \Traversable which contains the root paths * keyed by the corresponding namespace to look for plugin implementations - */ - public function __construct($subdir, \Traversable $namespaces) { + * @param array $annotation_namespaces + * (optional) The namespaces of classes that can be used as annotations. + * Defaults to an empty array. + * @param string $plugin_definition_annotation_name + * (optional) The name of the annotation that contains the plugin definition. + * Defaults to 'Drupal\Component\Annotation\Plugin'. + */ + function __construct($subdir, \Traversable $namespaces, $annotation_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') { $this->subdir = $subdir; - $this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces); + $this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces, $annotation_namespaces, $plugin_definition_annotation_name); $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); $this->factory = new DefaultFactory($this); @@ -146,64 +122,27 @@ public function setCache(CacheBackendInterface $cache, $cache_key_prefix) { * @param string $alter_hook * (optional) Name of the alter hook. Defaults to $owner_$type if not given. */ - public function setAlterHook(ModuleHandler $module_handler, $alter_hook = NULL) { + public function setAlterHook(ModuleHandlerInterface $module_handler, $alter_hook = NULL) { $this->moduleHandler = $module_handler; $this->alterHook = $alter_hook ? $alter_hook : strtolower($this->subdir); } /** - * Implements Drupal\Component\Plugin\PluginManagerInterface::createInstance(). - */ - public function createInstance($plugin_id, array $configuration = array()) { - return $this->factory->createInstance($plugin_id, $configuration); - } - - /** - * Implements Drupal\Component\Plugin\PluginManagerInterface::getInstance(). - */ - public function getInstance(array $options) { - if (!empty($this->mapper)) { - return $this->mapper->getInstance($options); - } - } - - /** - * Performs extra processing on plugin definitions. - * - * By default we add defaults for the type to the definition. If a type has - * additional processing logic they can do that by replacing or extending the - * method. - */ - public function processDefinition(&$definition, $plugin_id) { - if ($this->defaults) { - $definition = NestedArray::mergeDeep($this->defaults, $definition); - } - } - - /** - * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition(). + * {@inheritdoc} */ public function getDefinition($plugin_id) { - // Optimize for fast access to definitions if they are already in memory. - if (isset($this->definitions)) { - // Avoid using a ternary that would create a copy of the array. - if (isset($this->definitions[$plugin_id])) { - return $this->definitions[$plugin_id]; - } - else { - return; - } + // Fetch definitions if they're not loaded yet. + if (!isset($this->definitions)) { + $this->getDefinitions(); } - - $definitions = $this->getDefinitions(); // Avoid using a ternary that would create a copy of the array. - if (isset($definitions[$plugin_id])) { - return $definitions[$plugin_id]; + if (isset($this->definitions[$plugin_id])) { + return $this->definitions[$plugin_id]; } } /** - * Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::getDefinitions(). + * {@inheritdoc} */ public function getDefinitions() { $definitions = $this->getCachedDefinitions(); @@ -221,7 +160,7 @@ public function getDefinitions() { } /** - * Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::clearCachedDefinitions(). + * {@inheritdoc} */ public function clearCachedDefinitions() { if ($this->cache) { @@ -237,7 +176,7 @@ public function clearCachedDefinitions() { /** * Returns the cached plugin definitions of the decorated discovery class. * - * @return mixed + * @return array|NULL * On success this will return an array of plugin definitions. On failure * this should return NULL, indicating to other methods that this has not * yet been defined. Success with no values should return as an empty array diff --git a/core/lib/Drupal/Core/Validation/ConstraintManager.php b/core/lib/Drupal/Core/Validation/ConstraintManager.php index 8afd195..2a1719f 100644 --- a/core/lib/Drupal/Core/Validation/ConstraintManager.php +++ b/core/lib/Drupal/Core/Validation/ConstraintManager.php @@ -7,14 +7,9 @@ namespace Drupal\Core\Validation; -use Drupal\Component\Plugin\Factory\DefaultFactory; -use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator; use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator; -use Drupal\Component\Plugin\Discovery\ProcessDecorator; -use Drupal\Core\Plugin\Discovery\AlterDecorator; -use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; -use Drupal\Core\Plugin\Discovery\CacheDecorator; +use Drupal\Core\Plugin\DefaultPluginManager; /** * Constraint plugin manager. @@ -34,7 +29,7 @@ * types FALSE may be specified. The key defaults to an empty array, i.e. no * types are supported. */ -class ConstraintManager extends PluginManagerBase { +class ConstraintManager extends DefaultPluginManager { /** * Overrides \Drupal\Component\Plugin\PluginManagerBase::__construct(). @@ -44,14 +39,9 @@ class ConstraintManager extends PluginManagerBase { * keyed by the corresponding namespace to look for plugin implementations, */ public function __construct(\Traversable $namespaces) { - $this->discovery = new AnnotatedClassDiscovery('Validation/Constraint', $namespaces); + parent::__construct('Validation/Constraint', $namespaces); $this->discovery = new StaticDiscoveryDecorator($this->discovery, array($this, 'registerDefinitions')); $this->discovery = new DerivativeDiscoveryDecorator($this->discovery); - $this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition')); - $this->discovery = new AlterDecorator($this->discovery, 'validation_constraint'); - $this->discovery = new CacheDecorator($this->discovery, 'validation_constraints:' . language(LANGUAGE_TYPE_INTERFACE)->langcode); - - $this->factory = new DefaultFactory($this); } /** diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php index dc5fb34..7f9af89 100644 --- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php +++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkitManager.php @@ -7,14 +7,12 @@ namespace Drupal\system\Plugin; -use Drupal\Component\Plugin\Factory\DefaultFactory; -use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery; +use Drupal\Core\Plugin\DefaultPluginManager; /** * Manages toolkit plugins. */ -class ImageToolkitManager extends PluginManagerBase { +class ImageToolkitManager extends DefaultPluginManager { /** * Constructs the ImageToolkitManager object. @@ -24,8 +22,7 @@ class ImageToolkitManager extends PluginManagerBase { * keyed by the corresponding namespace to look for plugin implementations, */ public function __construct(\Traversable $namespaces) { - $this->discovery = new AnnotatedClassDiscovery('ImageToolkit', $namespaces); - $this->factory = new DefaultFactory($this->discovery); + parent::__construct('ImageToolkit', $namespaces); } /**