DefaultPluginManager.php

You are here

Contains \Drupal\Core\Plugin\DefaultPluginManager

Namespace

Drupal\Core\Plugin

Classes

Namesort descending Description
DefaultPluginManager Base class for plugin managers.

File

core/lib/Drupal/Core/Plugin/DefaultPluginManager.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Core\Plugin\DefaultPluginManager
  5. */
  6. namespace Drupal\Core\Plugin;
  7. use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
  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\Cache\CacheBackendInterface;
  14. use Drupal\Core\Extension\ModuleHandlerInterface;
  15. use Drupal\Core\Language\LanguageManagerInterface;
  16. use Drupal\Core\Language\Language;
  17. use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
  18. use Drupal\Core\Plugin\Factory\ContainerFactory;
  19. /**
  20. * Base class for plugin managers.
  21. */
  22. class DefaultPluginManager extends PluginManagerBase implements PluginManagerInterface, CachedDiscoveryInterface {
  23. /**
  24. * Cached definitions array.
  25. *
  26. * @var array
  27. */
  28. protected $definitions;
  29. /**
  30. * Cache backend instance.
  31. *
  32. * @var \Drupal\Core\Cache\CacheBackendInterface
  33. */
  34. protected $cacheBackend;
  35. /**
  36. * Provided cache key prefix.
  37. *
  38. * @var string
  39. */
  40. protected $cacheKeyPrefix;
  41. /**
  42. * Actually used cache key with the language code appended.
  43. *
  44. * @var string
  45. */
  46. protected $cacheKey;
  47. /**
  48. * An array of cache tags to use for the cached definitions.
  49. *
  50. * @var array
  51. */
  52. protected $cacheTags = array();
  53. /**
  54. * Name of the alter hook if one should be invoked.
  55. *
  56. * @var string
  57. */
  58. protected $alterHook;
  59. /**
  60. * The subdirectory within a namespace to look for plugins, or FALSE if the
  61. * plugins are in the top level of the namespace.
  62. *
  63. * @var string|bool
  64. */
  65. protected $subdir;
  66. /**
  67. * The module handler to invoke the alter hook.
  68. *
  69. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  70. */
  71. protected $moduleHandler;
  72. /**
  73. * The language manager.
  74. *
  75. * @var \Drupal\Core\Language\LanguageManagerInterface
  76. */
  77. protected $languageManager;
  78. /**
  79. * A set of defaults to be referenced by $this->processDefinition() if
  80. * additional processing of plugins is necessary or helpful for development
  81. * purposes.
  82. *
  83. * @var array
  84. */
  85. protected $defaults = array();
  86. /**
  87. * Creates the discovery object.
  88. *
  89. * @param string|bool $subdir
  90. * The plugin's subdirectory, for example Plugin/views/filter.
  91. * @param \Traversable $namespaces
  92. * An object that implements \Traversable which contains the root paths
  93. * keyed by the corresponding namespace to look for plugin implementations.
  94. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  95. * The module handler.
  96. * @param string $plugin_definition_annotation_name
  97. * (optional) The name of the annotation that contains the plugin definition.
  98. * Defaults to 'Drupal\Component\Annotation\Plugin'.
  99. */
  100. public function __construct($subdir, \Traversable $namespaces, ModuleHandlerInterface $module_handler, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
  101. $this->subdir = $subdir;
  102. $this->discovery = new AnnotatedClassDiscovery($subdir, $namespaces, $plugin_definition_annotation_name);
  103. $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
  104. $this->factory = new ContainerFactory($this);
  105. $this->moduleHandler = $module_handler;
  106. }
  107. /**
  108. * Initialize the cache backend.
  109. *
  110. * Plugin definitions are cached using the provided cache backend. The
  111. * interface language is added as a suffix to the cache key.
  112. *
  113. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  114. * Cache backend instance to use.
  115. * @param \Drupal\Core\Language\LanguageManagerInterface
  116. * The language manager.
  117. * @param string $cache_key_prefix
  118. * Cache key prefix to use, the language code will be appended
  119. * automatically.
  120. * @param array $cache_tags
  121. * (optional) When providing a list of cache tags, the cached plugin
  122. * definitions are tagged with the provided cache tags. These cache tags can
  123. * then be used to clear the corresponding cached plugin definitions. Note
  124. * that this should be used with care! For clearing all cached plugin
  125. * definitions of a plugin manager, call that plugin manager's
  126. * clearCachedDefinitions() method. Only use cache tags when cached plugin
  127. * definitions should be cleared along with other, related cache entries.
  128. */
  129. public function setCacheBackend(CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, $cache_key_prefix, array $cache_tags = array()) {
  130. $this->languageManager = $language_manager;
  131. $this->cacheBackend = $cache_backend;
  132. $this->cacheKeyPrefix = $cache_key_prefix;
  133. $this->cacheKey = $cache_key_prefix . ':' . $language_manager->getCurrentLanguage()->id;
  134. $this->cacheTags = $cache_tags;
  135. }
  136. /**
  137. * Initializes the alter hook.
  138. *
  139. * @param string $alter_hook
  140. * Name of the alter hook; for example, to invoke
  141. * hook_mymodule_data_alter() pass in "mymodule_data".
  142. */
  143. protected function alterInfo($alter_hook) {
  144. $this->alterHook = $alter_hook;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getDefinition($plugin_id) {
  150. // Fetch definitions if they're not loaded yet.
  151. if (!isset($this->definitions)) {
  152. $this->getDefinitions();
  153. }
  154. // Avoid using a ternary that would create a copy of the array.
  155. if (isset($this->definitions[$plugin_id])) {
  156. return $this->definitions[$plugin_id];
  157. }
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getDefinitions() {
  163. $definitions = $this->getCachedDefinitions();
  164. if (!isset($definitions)) {
  165. $definitions = $this->findDefinitions();
  166. $this->setCachedDefinitions($definitions);
  167. }
  168. return $definitions;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function clearCachedDefinitions() {
  174. if ($this->cacheBackend) {
  175. if ($this->cacheTags) {
  176. // Use the cache tags to clear the cache.
  177. Cache::deleteTags($this->cacheTags);
  178. }
  179. elseif ($this->languageManager) {
  180. $cache_keys = array();
  181. foreach ($this->languageManager->getLanguages() as $langcode => $language) {
  182. $cache_keys[] = $this->cacheKeyPrefix . ':' . $langcode;
  183. }
  184. $this->cacheBackend->deleteMultiple($cache_keys);
  185. }
  186. else {
  187. $this->cacheBackend->delete($this->cacheKey);
  188. }
  189. }
  190. $this->definitions = NULL;
  191. }
  192. /**
  193. * Returns the cached plugin definitions of the decorated discovery class.
  194. *
  195. * @return array|null
  196. * On success this will return an array of plugin definitions. On failure
  197. * this should return NULL, indicating to other methods that this has not
  198. * yet been defined. Success with no values should return as an empty array
  199. * and would actually be returned by the getDefinitions() method.
  200. */
  201. protected function getCachedDefinitions() {
  202. if (!isset($this->definitions) && $this->cacheBackend && $cache = $this->cacheBackend->get($this->cacheKey)) {
  203. $this->definitions = $cache->data;
  204. }
  205. return $this->definitions;
  206. }
  207. /**
  208. * Sets a cache of plugin definitions for the decorated discovery class.
  209. *
  210. * @param array $definitions
  211. * List of definitions to store in cache.
  212. */
  213. protected function setCachedDefinitions($definitions) {
  214. if ($this->cacheBackend) {
  215. $this->cacheBackend->set($this->cacheKey, $definitions, Cache::PERMANENT, $this->cacheTags);
  216. }
  217. $this->definitions = $definitions;
  218. }
  219. /**
  220. * Performs extra processing on plugin definitions.
  221. *
  222. * By default we add defaults for the type to the definition. If a type has
  223. * additional processing logic they can do that by replacing or extending the
  224. * method.
  225. */
  226. public function processDefinition(&$definition, $plugin_id) {
  227. if (!empty($this->defaults) && is_array($this->defaults)) {
  228. $definition = NestedArray::mergeDeep($this->defaults, $definition);
  229. }
  230. }
  231. /**
  232. * Finds plugin definitions.
  233. *
  234. * @return array
  235. * List of definitions to store in cache.
  236. */
  237. protected function findDefinitions() {
  238. $definitions = $this->discovery->getDefinitions();
  239. foreach ($definitions as $plugin_id => &$definition) {
  240. $this->processDefinition($definition, $plugin_id);
  241. }
  242. if ($this->alterHook) {
  243. $this->moduleHandler->alter($this->alterHook, $definitions);
  244. }
  245. // If this plugin was provided by a module that does not exist, remove the
  246. // plugin definition.
  247. foreach ($definitions as $plugin_id => $plugin_definition) {
  248. if (isset($plugin_definition['provider']) && !in_array($plugin_definition['provider'], array('Core', 'Component')) && !$this->moduleHandler->moduleExists($plugin_definition['provider'])) {
  249. unset($definitions[$plugin_id]);
  250. }
  251. }
  252. return $definitions;
  253. }
  254. }