1. 8.2.x core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
  2. 8.0.x core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
  3. 8.1.x core/lib/Drupal/Core/Plugin/DefaultPluginManager.php

Namespace

Drupal\Core\Plugin

File

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

Classes

Namesort descending Description
DefaultPluginManager Base class for plugin managers.