Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / DefaultPluginManager.php
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
5 use Drupal\Component\Assertion\Inspector;
6 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
7 use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
8 use Drupal\Core\Cache\CacheableDependencyInterface;
9 use Drupal\Core\Cache\CacheBackendInterface;
10 use Drupal\Core\Cache\UseCacheBackendTrait;
11 use Drupal\Component\Plugin\Discovery\DiscoveryCachedTrait;
12 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
13 use Drupal\Component\Plugin\PluginManagerBase;
14 use Drupal\Component\Plugin\PluginManagerInterface;
15 use Drupal\Component\Utility\NestedArray;
16 use Drupal\Core\Cache\Cache;
17 use Drupal\Core\Extension\ModuleHandlerInterface;
18 use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
19 use Drupal\Core\Plugin\Factory\ContainerFactory;
20
21 /**
22  * Base class for plugin managers.
23  *
24  * @ingroup plugin_api
25  */
26 class DefaultPluginManager extends PluginManagerBase implements PluginManagerInterface, CachedDiscoveryInterface, CacheableDependencyInterface {
27
28   use DiscoveryCachedTrait;
29   use UseCacheBackendTrait;
30
31   /**
32    * The cache key.
33    *
34    * @var string
35    */
36   protected $cacheKey;
37
38   /**
39    * An array of cache tags to use for the cached definitions.
40    *
41    * @var array
42    */
43   protected $cacheTags = [];
44
45   /**
46    * Name of the alter hook if one should be invoked.
47    *
48    * @var string
49    */
50   protected $alterHook;
51
52   /**
53    * The subdirectory within a namespace to look for plugins, or FALSE if the
54    * plugins are in the top level of the namespace.
55    *
56    * @var string|bool
57    */
58   protected $subdir;
59
60   /**
61    * The module handler to invoke the alter hook.
62    *
63    * @var \Drupal\Core\Extension\ModuleHandlerInterface
64    */
65   protected $moduleHandler;
66
67   /**
68    * A set of defaults to be referenced by $this->processDefinition() if
69    * additional processing of plugins is necessary or helpful for development
70    * purposes.
71    *
72    * @var array
73    */
74   protected $defaults = [];
75
76   /**
77    * The name of the annotation that contains the plugin definition.
78    *
79    * @var string
80    */
81   protected $pluginDefinitionAnnotationName;
82
83   /**
84    * The interface each plugin should implement.
85    *
86    * @var string|null
87    */
88   protected $pluginInterface;
89
90   /**
91    * An object that implements \Traversable which contains the root paths
92    * keyed by the corresponding namespace to look for plugin implementations.
93    *
94    * @var \Traversable
95    */
96   protected $namespaces;
97
98   /**
99    * Additional namespaces the annotation discovery mechanism should scan for
100    * annotation definitions.
101    *
102    * @var string[]
103    */
104   protected $additionalAnnotationNamespaces = [];
105
106   /**
107    * Creates the discovery object.
108    *
109    * @param string|bool $subdir
110    *   The plugin's subdirectory, for example Plugin/views/filter.
111    * @param \Traversable $namespaces
112    *   An object that implements \Traversable which contains the root paths
113    *   keyed by the corresponding namespace to look for plugin implementations.
114    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
115    *   The module handler.
116    * @param string|null $plugin_interface
117    *   (optional) The interface each plugin should implement.
118    * @param string $plugin_definition_annotation_name
119    *   (optional) The name of the annotation that contains the plugin definition.
120    *   Defaults to 'Drupal\Component\Annotation\Plugin'.
121    * @param string[] $additional_annotation_namespaces
122    *   (optional) Additional namespaces to scan for annotation definitions.
123    */
124   public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin', array $additional_annotation_namespaces = []) {
125     $this->subdir = $subdir;
126     $this->namespaces = $namespaces;
127     $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
128     $this->pluginInterface = $plugin_interface;
129     $this->moduleHandler = $module_handler;
130     $this->additionalAnnotationNamespaces = $additional_annotation_namespaces;
131   }
132
133   /**
134    * Initialize the cache backend.
135    *
136    * Plugin definitions are cached using the provided cache backend.
137    *
138    * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
139    *   Cache backend instance to use.
140    * @param string $cache_key
141    *   Cache key prefix to use.
142    * @param array $cache_tags
143    *   (optional) When providing a list of cache tags, the cached plugin
144    *   definitions are tagged with the provided cache tags. These cache tags can
145    *   then be used to clear the corresponding cached plugin definitions. Note
146    *   that this should be used with care! For clearing all cached plugin
147    *   definitions of a plugin manager, call that plugin manager's
148    *   clearCachedDefinitions() method. Only use cache tags when cached plugin
149    *   definitions should be cleared along with other, related cache entries.
150    */
151   public function setCacheBackend(CacheBackendInterface $cache_backend, $cache_key, array $cache_tags = []) {
152     assert(Inspector::assertAllStrings($cache_tags), 'Cache Tags must be strings.');
153     $this->cacheBackend = $cache_backend;
154     $this->cacheKey = $cache_key;
155     $this->cacheTags = $cache_tags;
156   }
157
158   /**
159    * Sets the alter hook name.
160    *
161    * @param string $alter_hook
162    *   Name of the alter hook; for example, to invoke
163    *   hook_mymodule_data_alter() pass in "mymodule_data".
164    */
165   protected function alterInfo($alter_hook) {
166     $this->alterHook = $alter_hook;
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function getDefinitions() {
173     $definitions = $this->getCachedDefinitions();
174     if (!isset($definitions)) {
175       $definitions = $this->findDefinitions();
176       $this->setCachedDefinitions($definitions);
177     }
178     return $definitions;
179   }
180
181   /**
182    * {@inheritdoc}
183    */
184   public function clearCachedDefinitions() {
185     if ($this->cacheBackend) {
186       if ($this->cacheTags) {
187         // Use the cache tags to clear the cache.
188         Cache::invalidateTags($this->cacheTags);
189       }
190       else {
191         $this->cacheBackend->delete($this->cacheKey);
192       }
193     }
194     $this->definitions = NULL;
195   }
196
197   /**
198    * Returns the cached plugin definitions of the decorated discovery class.
199    *
200    * @return array|null
201    *   On success this will return an array of plugin definitions. On failure
202    *   this should return NULL, indicating to other methods that this has not
203    *   yet been defined. Success with no values should return as an empty array
204    *   and would actually be returned by the getDefinitions() method.
205    */
206   protected function getCachedDefinitions() {
207     if (!isset($this->definitions) && $cache = $this->cacheGet($this->cacheKey)) {
208       $this->definitions = $cache->data;
209     }
210     return $this->definitions;
211   }
212
213   /**
214    * Sets a cache of plugin definitions for the decorated discovery class.
215    *
216    * @param array $definitions
217    *   List of definitions to store in cache.
218    */
219   protected function setCachedDefinitions($definitions) {
220     $this->cacheSet($this->cacheKey, $definitions, Cache::PERMANENT, $this->cacheTags);
221     $this->definitions = $definitions;
222   }
223
224   /**
225    * {@inheritdoc}
226    */
227   public function useCaches($use_caches = FALSE) {
228     $this->useCaches = $use_caches;
229     if (!$use_caches) {
230       $this->definitions = NULL;
231     }
232   }
233
234   /**
235    * Performs extra processing on plugin definitions.
236    *
237    * By default we add defaults for the type to the definition. If a type has
238    * additional processing logic they can do that by replacing or extending the
239    * method.
240    */
241   public function processDefinition(&$definition, $plugin_id) {
242     // Only array-based definitions can have defaults merged in.
243     if (is_array($definition) && !empty($this->defaults) && is_array($this->defaults)) {
244       $definition = NestedArray::mergeDeep($this->defaults, $definition);
245     }
246
247     // Keep class definitions standard with no leading slash.
248     if ($definition instanceof PluginDefinitionInterface) {
249       $definition->setClass(ltrim($definition->getClass(), '\\'));
250     }
251     elseif (is_array($definition) && isset($definition['class'])) {
252       $definition['class'] = ltrim($definition['class'], '\\');
253     }
254   }
255
256   /**
257    * {@inheritdoc}
258    */
259   protected function getDiscovery() {
260     if (!$this->discovery) {
261       $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
262       $this->discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
263     }
264     return $this->discovery;
265   }
266
267   /**
268    * {@inheritdoc}
269    */
270   protected function getFactory() {
271     if (!$this->factory) {
272       $this->factory = new ContainerFactory($this, $this->pluginInterface);
273     }
274     return $this->factory;
275   }
276
277   /**
278    * Finds plugin definitions.
279    *
280    * @return array
281    *   List of definitions to store in cache.
282    */
283   protected function findDefinitions() {
284     $definitions = $this->getDiscovery()->getDefinitions();
285     foreach ($definitions as $plugin_id => &$definition) {
286       $this->processDefinition($definition, $plugin_id);
287     }
288     $this->alterDefinitions($definitions);
289     // If this plugin was provided by a module that does not exist, remove the
290     // plugin definition.
291     foreach ($definitions as $plugin_id => $plugin_definition) {
292       $provider = $this->extractProviderFromDefinition($plugin_definition);
293       if ($provider && !in_array($provider, ['core', 'component']) && !$this->providerExists($provider)) {
294         unset($definitions[$plugin_id]);
295       }
296     }
297     return $definitions;
298   }
299
300   /**
301    * Extracts the provider from a plugin definition.
302    *
303    * @param mixed $plugin_definition
304    *   The plugin definition. Usually either an array or an instance of
305    *   \Drupal\Component\Plugin\Definition\PluginDefinitionInterface
306    *
307    * @return string|null
308    *   The provider string, if it exists. NULL otherwise.
309    */
310   protected function extractProviderFromDefinition($plugin_definition) {
311     if ($plugin_definition instanceof PluginDefinitionInterface) {
312       return $plugin_definition->getProvider();
313     }
314
315     // Attempt to convert the plugin definition to an array.
316     if (is_object($plugin_definition)) {
317       $plugin_definition = (array) $plugin_definition;
318     }
319
320     if (isset($plugin_definition['provider'])) {
321       return $plugin_definition['provider'];
322     }
323   }
324
325   /**
326    * Invokes the hook to alter the definitions if the alter hook is set.
327    *
328    * @param $definitions
329    *   The discovered plugin definitions.
330    */
331   protected function alterDefinitions(&$definitions) {
332     if ($this->alterHook) {
333       $this->moduleHandler->alter($this->alterHook, $definitions);
334     }
335   }
336
337   /**
338    * Determines if the provider of a definition exists.
339    *
340    * @return bool
341    *   TRUE if provider exists, FALSE otherwise.
342    */
343   protected function providerExists($provider) {
344     return $this->moduleHandler->moduleExists($provider);
345   }
346
347   /**
348    * {@inheritdoc}
349    */
350   public function getCacheContexts() {
351     return [];
352   }
353
354   /**
355    * {@inheritdoc}
356    */
357   public function getCacheTags() {
358     return $this->cacheTags;
359   }
360
361   /**
362    * {@inheritdoc}
363    */
364   public function getCacheMaxAge() {
365     return Cache::PERMANENT;
366   }
367
368 }