3 namespace Drupal\Core\Plugin;
5 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
6 use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
7 use Drupal\Core\Cache\CacheableDependencyInterface;
8 use Drupal\Core\Cache\CacheBackendInterface;
9 use Drupal\Core\Cache\UseCacheBackendTrait;
10 use Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait;
11 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
12 use Drupal\Component\Plugin\PluginManagerBase;
13 use Drupal\Component\Plugin\PluginManagerInterface;
14 use Drupal\Component\Utility\NestedArray;
15 use Drupal\Core\Cache\Cache;
16 use Drupal\Core\Extension\ModuleHandlerInterface;
17 use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
18 use Drupal\Core\Plugin\Factory\ContainerFactory;
21 * Base class for plugin managers.
25 class DefaultPluginManager extends PluginManagerBase implements PluginManagerInterface, CachedDiscoveryInterface, CacheableDependencyInterface {
27 use DiscoveryCachedTrait;
28 use UseCacheBackendTrait;
38 * An array of cache tags to use for the cached definitions.
42 protected $cacheTags = [];
45 * Name of the alter hook if one should be invoked.
52 * The subdirectory within a namespace to look for plugins, or FALSE if the
53 * plugins are in the top level of the namespace.
60 * The module handler to invoke the alter hook.
62 * @var \Drupal\Core\Extension\ModuleHandlerInterface
64 protected $moduleHandler;
67 * A set of defaults to be referenced by $this->processDefinition() if
68 * additional processing of plugins is necessary or helpful for development
73 protected $defaults = [];
76 * The name of the annotation that contains the plugin definition.
80 protected $pluginDefinitionAnnotationName;
83 * The interface each plugin should implement.
87 protected $pluginInterface;
90 * An object that implements \Traversable which contains the root paths
91 * keyed by the corresponding namespace to look for plugin implementations.
95 protected $namespaces;
98 * Additional namespaces the annotation discovery mechanism should scan for
99 * annotation definitions.
103 protected $additionalAnnotationNamespaces = [];
106 * Creates the discovery object.
108 * @param string|bool $subdir
109 * The plugin's subdirectory, for example Plugin/views/filter.
110 * @param \Traversable $namespaces
111 * An object that implements \Traversable which contains the root paths
112 * keyed by the corresponding namespace to look for plugin implementations.
113 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
114 * The module handler.
115 * @param string|null $plugin_interface
116 * (optional) The interface each plugin should implement.
117 * @param string $plugin_definition_annotation_name
118 * (optional) The name of the annotation that contains the plugin definition.
119 * Defaults to 'Drupal\Component\Annotation\Plugin'.
120 * @param string[] $additional_annotation_namespaces
121 * (optional) Additional namespaces to scan for annotation definitions.
123 public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', array $additional_annotation_namespaces = []) {
124 $this->subdir = $subdir;
125 $this->namespaces = $namespaces;
126 $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
127 $this->pluginInterface = $plugin_interface;
128 $this->moduleHandler = $module_handler;
129 $this->additionalAnnotationNamespaces = $additional_annotation_namespaces;
133 * Initialize the cache backend.
135 * Plugin definitions are cached using the provided cache backend.
137 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
138 * Cache backend instance to use.
139 * @param string $cache_key
140 * Cache key prefix to use.
141 * @param array $cache_tags
142 * (optional) When providing a list of cache tags, the cached plugin
143 * definitions are tagged with the provided cache tags. These cache tags can
144 * then be used to clear the corresponding cached plugin definitions. Note
145 * that this should be used with care! For clearing all cached plugin
146 * definitions of a plugin manager, call that plugin manager's
147 * clearCachedDefinitions() method. Only use cache tags when cached plugin
148 * definitions should be cleared along with other, related cache entries.
150 public function setCacheBackend(CacheBackendInterface $cache_backend, $cache_key, array $cache_tags = []) {
151 assert('\Drupal\Component\Assertion\Inspector::assertAllStrings($cache_tags)', 'Cache Tags must be strings.');
152 $this->cacheBackend = $cache_backend;
153 $this->cacheKey = $cache_key;
154 $this->cacheTags = $cache_tags;
158 * Initializes the alter hook.
160 * @param string $alter_hook
161 * Name of the alter hook; for example, to invoke
162 * hook_mymodule_data_alter() pass in "mymodule_data".
164 protected function alterInfo($alter_hook) {
165 $this->alterHook = $alter_hook;
171 public function getDefinitions() {
172 $definitions = $this->getCachedDefinitions();
173 if (!isset($definitions)) {
174 $definitions = $this->findDefinitions();
175 $this->setCachedDefinitions($definitions);
183 public function clearCachedDefinitions() {
184 if ($this->cacheBackend) {
185 if ($this->cacheTags) {
186 // Use the cache tags to clear the cache.
187 Cache::invalidateTags($this->cacheTags);
190 $this->cacheBackend->delete($this->cacheKey);
193 $this->definitions = NULL;
197 * Returns the cached plugin definitions of the decorated discovery class.
200 * On success this will return an array of plugin definitions. On failure
201 * this should return NULL, indicating to other methods that this has not
202 * yet been defined. Success with no values should return as an empty array
203 * and would actually be returned by the getDefinitions() method.
205 protected function getCachedDefinitions() {
206 if (!isset($this->definitions) && $cache = $this->cacheGet($this->cacheKey)) {
207 $this->definitions = $cache->data;
209 return $this->definitions;
213 * Sets a cache of plugin definitions for the decorated discovery class.
215 * @param array $definitions
216 * List of definitions to store in cache.
218 protected function setCachedDefinitions($definitions) {
219 $this->cacheSet($this->cacheKey, $definitions, Cache::PERMANENT, $this->cacheTags);
220 $this->definitions = $definitions;
226 public function useCaches($use_caches = FALSE) {
227 $this->useCaches = $use_caches;
229 $this->definitions = NULL;
234 * Performs extra processing on plugin definitions.
236 * By default we add defaults for the type to the definition. If a type has
237 * additional processing logic they can do that by replacing or extending the
240 public function processDefinition(&$definition, $plugin_id) {
241 // Only array-based definitions can have defaults merged in.
242 if (is_array($definition) && !empty($this->defaults) && is_array($this->defaults)) {
243 $definition = NestedArray::mergeDeep($this->defaults, $definition);
246 // Keep class definitions standard with no leading slash.
247 if ($definition instanceof PluginDefinitionInterface) {
248 $definition->setClass(ltrim($definition->getClass(), '\\'));
250 elseif (is_array($definition) && isset($definition['class'])) {
251 $definition['class'] = ltrim($definition['class'], '\\');
258 protected function getDiscovery() {
259 if (!$this->discovery) {
260 $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
261 $this->discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
263 return $this->discovery;
269 protected function getFactory() {
270 if (!$this->factory) {
271 $this->factory = new ContainerFactory($this, $this->pluginInterface);
273 return $this->factory;
277 * Finds plugin definitions.
280 * List of definitions to store in cache.
282 protected function findDefinitions() {
283 $definitions = $this->getDiscovery()->getDefinitions();
284 foreach ($definitions as $plugin_id => &$definition) {
285 $this->processDefinition($definition, $plugin_id);
287 $this->alterDefinitions($definitions);
288 // If this plugin was provided by a module that does not exist, remove the
289 // plugin definition.
290 foreach ($definitions as $plugin_id => $plugin_definition) {
291 $provider = $this->extractProviderFromDefinition($plugin_definition);
292 if ($provider && !in_array($provider, ['core', 'component']) && !$this->providerExists($provider)) {
293 unset($definitions[$plugin_id]);
300 * Extracts the provider from a plugin definition.
302 * @param mixed $plugin_definition
303 * The plugin definition. Usually either an array or an instance of
304 * \Drupal\Component\Plugin\Definition\PluginDefinitionInterface
306 * @return string|null
307 * The provider string, if it exists. NULL otherwise.
309 protected function extractProviderFromDefinition($plugin_definition) {
310 if ($plugin_definition instanceof PluginDefinitionInterface) {
311 return $plugin_definition->getProvider();
314 // Attempt to convert the plugin definition to an array.
315 if (is_object($plugin_definition)) {
316 $plugin_definition = (array) $plugin_definition;
319 if (isset($plugin_definition['provider'])) {
320 return $plugin_definition['provider'];
325 * Invokes the hook to alter the definitions if the alter hook is set.
327 * @param $definitions
328 * The discovered plugin definitions.
330 protected function alterDefinitions(&$definitions) {
331 if ($this->alterHook) {
332 $this->moduleHandler->alter($this->alterHook, $definitions);
337 * Determines if the provider of a definition exists.
340 * TRUE if provider exists, FALSE otherwise.
342 protected function providerExists($provider) {
343 return $this->moduleHandler->moduleExists($provider);
349 public function getCacheContexts() {
356 public function getCacheTags() {
357 return $this->cacheTags;
363 public function getCacheMaxAge() {
364 return Cache::PERMANENT;