3 namespace Drupal\Core\Plugin;
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;
22 * Base class for plugin managers.
26 class DefaultPluginManager extends PluginManagerBase implements PluginManagerInterface, CachedDiscoveryInterface, CacheableDependencyInterface {
28 use DiscoveryCachedTrait;
29 use UseCacheBackendTrait;
39 * An array of cache tags to use for the cached definitions.
43 protected $cacheTags = [];
46 * Name of the alter hook if one should be invoked.
53 * The subdirectory within a namespace to look for plugins, or FALSE if the
54 * plugins are in the top level of the namespace.
61 * The module handler to invoke the alter hook.
63 * @var \Drupal\Core\Extension\ModuleHandlerInterface
65 protected $moduleHandler;
68 * A set of defaults to be referenced by $this->processDefinition() if
69 * additional processing of plugins is necessary or helpful for development
74 protected $defaults = [];
77 * The name of the annotation that contains the plugin definition.
81 protected $pluginDefinitionAnnotationName;
84 * The interface each plugin should implement.
88 protected $pluginInterface;
91 * An object that implements \Traversable which contains the root paths
92 * keyed by the corresponding namespace to look for plugin implementations.
96 protected $namespaces;
99 * Additional namespaces the annotation discovery mechanism should scan for
100 * annotation definitions.
104 protected $additionalAnnotationNamespaces = [];
107 * Creates the discovery object.
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.
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;
134 * Initialize the cache backend.
136 * Plugin definitions are cached using the provided cache backend.
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.
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;
159 * Sets the alter hook name.
161 * @param string $alter_hook
162 * Name of the alter hook; for example, to invoke
163 * hook_mymodule_data_alter() pass in "mymodule_data".
165 protected function alterInfo($alter_hook) {
166 $this->alterHook = $alter_hook;
172 public function getDefinitions() {
173 $definitions = $this->getCachedDefinitions();
174 if (!isset($definitions)) {
175 $definitions = $this->findDefinitions();
176 $this->setCachedDefinitions($definitions);
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);
191 $this->cacheBackend->delete($this->cacheKey);
194 $this->definitions = NULL;
198 * Returns the cached plugin definitions of the decorated discovery class.
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.
206 protected function getCachedDefinitions() {
207 if (!isset($this->definitions) && $cache = $this->cacheGet($this->cacheKey)) {
208 $this->definitions = $cache->data;
210 return $this->definitions;
214 * Sets a cache of plugin definitions for the decorated discovery class.
216 * @param array $definitions
217 * List of definitions to store in cache.
219 protected function setCachedDefinitions($definitions) {
220 $this->cacheSet($this->cacheKey, $definitions, Cache::PERMANENT, $this->cacheTags);
221 $this->definitions = $definitions;
227 public function useCaches($use_caches = FALSE) {
228 $this->useCaches = $use_caches;
230 $this->definitions = NULL;
235 * Performs extra processing on plugin definitions.
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
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);
247 // Keep class definitions standard with no leading slash.
248 if ($definition instanceof PluginDefinitionInterface) {
249 $definition->setClass(ltrim($definition->getClass(), '\\'));
251 elseif (is_array($definition) && isset($definition['class'])) {
252 $definition['class'] = ltrim($definition['class'], '\\');
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);
264 return $this->discovery;
270 protected function getFactory() {
271 if (!$this->factory) {
272 $this->factory = new ContainerFactory($this, $this->pluginInterface);
274 return $this->factory;
278 * Finds plugin definitions.
281 * List of definitions to store in cache.
283 protected function findDefinitions() {
284 $definitions = $this->getDiscovery()->getDefinitions();
285 foreach ($definitions as $plugin_id => &$definition) {
286 $this->processDefinition($definition, $plugin_id);
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]);
301 * Extracts the provider from a plugin definition.
303 * @param mixed $plugin_definition
304 * The plugin definition. Usually either an array or an instance of
305 * \Drupal\Component\Plugin\Definition\PluginDefinitionInterface
307 * @return string|null
308 * The provider string, if it exists. NULL otherwise.
310 protected function extractProviderFromDefinition($plugin_definition) {
311 if ($plugin_definition instanceof PluginDefinitionInterface) {
312 return $plugin_definition->getProvider();
315 // Attempt to convert the plugin definition to an array.
316 if (is_object($plugin_definition)) {
317 $plugin_definition = (array) $plugin_definition;
320 if (isset($plugin_definition['provider'])) {
321 return $plugin_definition['provider'];
326 * Invokes the hook to alter the definitions if the alter hook is set.
328 * @param $definitions
329 * The discovered plugin definitions.
331 protected function alterDefinitions(&$definitions) {
332 if ($this->alterHook) {
333 $this->moduleHandler->alter($this->alterHook, $definitions);
338 * Determines if the provider of a definition exists.
341 * TRUE if provider exists, FALSE otherwise.
343 protected function providerExists($provider) {
344 return $this->moduleHandler->moduleExists($provider);
350 public function getCacheContexts() {
357 public function getCacheTags() {
358 return $this->cacheTags;
364 public function getCacheMaxAge() {
365 return Cache::PERMANENT;