Contains \Drupal\Core\Plugin\DefaultPluginManager

Namespace

Drupal\Core\Plugin

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

Classes

Namesort descending Description
DefaultPluginManager Base class for plugin managers.