file PluginManager.php

Namespace

Drupal\bootstrap\Plugin
  1. <?php
  2. namespace Drupal\bootstrap\Plugin;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\bootstrap\Theme;
  5. use Drupal\Core\Plugin\DefaultPluginManager;
  6. /**
  7. * Base class for Bootstrap plugin managers.
  8. *
  9. * @ingroup utility
  10. */
  11. class PluginManager extends DefaultPluginManager {
  12. /**
  13. * The current theme.
  14. *
  15. * @var \Drupal\bootstrap\Theme
  16. */
  17. protected $theme;
  18. /**
  19. * The theme handler to check if theme exists.
  20. *
  21. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  22. */
  23. protected $themeHandler;
  24. /**
  25. * The theme manager to invoke alter hooks.
  26. *
  27. * @var \Drupal\Core\Theme\ThemeManager
  28. */
  29. protected $themeManager;
  30. /**
  31. * Creates the discovery object.
  32. *
  33. * @param \Drupal\bootstrap\Theme $theme
  34. * The theme to use for discovery.
  35. * @param string|bool $subdir
  36. * The plugin's subdirectory, for example Plugin/views/filter.
  37. * @param string|null $plugin_interface
  38. * (optional) The interface each plugin should implement.
  39. * @param string $plugin_definition_annotation_name
  40. * (optional) Name of the annotation that contains the plugin definition.
  41. * Defaults to 'Drupal\Component\Annotation\Plugin'.
  42. */
  43. public function __construct(Theme $theme, $subdir, $plugin_interface = NULL, $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
  44. // Get the active theme.
  45. $this->theme = $theme;
  46. // Determine the namespaces to search for.
  47. $namespaces = [];
  48. foreach ($theme->getAncestry() as $ancestor) {
  49. $namespaces['Drupal\\' . $ancestor->getName()] = [DRUPAL_ROOT . '/' . $ancestor->getPath() . '/src'];
  50. }
  51. $this->namespaces = new \ArrayObject($namespaces);
  52. $this->subdir = $subdir;
  53. $this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
  54. $this->pluginInterface = $plugin_interface;
  55. $this->themeHandler = \Drupal::service('theme_handler');
  56. $this->themeManager = \Drupal::service('theme.manager');
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function alterDefinitions(&$definitions) {
  62. if ($this->alterHook) {
  63. $this->themeManager->alter($this->alterHook, $definitions);
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function createInstance($plugin_id, array $configuration = []) {
  70. if (!isset($configuration['theme'])) {
  71. $configuration['theme'] = $this->theme;
  72. }
  73. return parent::createInstance($plugin_id, $configuration);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function findDefinitions() {
  79. $definitions = parent::findDefinitions();
  80. $this->sortDefinitions($definitions);
  81. return $definitions;
  82. }
  83. /**
  84. * Retrieves the cache tags used to invalidate caches.
  85. *
  86. * @return array
  87. * An indexed array of cache tags.
  88. */
  89. public function getCacheTags() {
  90. return [Bootstrap::CACHE_TAG];
  91. }
  92. /**
  93. * Retrieves all definitions where the plugin ID matches a certain criteria.
  94. *
  95. * @param string $regex
  96. * The regex pattern to match.
  97. *
  98. * @return array[]
  99. * An array of plugin definitions (empty array if no definitions were
  100. * found). Keys are plugin IDs.
  101. */
  102. public function getDefinitionsLike($regex) {
  103. $definitions = [];
  104. foreach ($this->getDefinitions() as $plugin_id => $definition) {
  105. if (preg_match($regex, $plugin_id)) {
  106. $definitions[$plugin_id] = $definition;
  107. }
  108. }
  109. ksort($definitions, SORT_NATURAL);
  110. return $definitions;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. protected function providerExists($provider) {
  116. return $this->themeHandler->themeExists($provider);
  117. }
  118. /**
  119. * Sorts the plugin definitions.
  120. *
  121. * @param array $definitions
  122. * The unsorted plugin definitions, passed by reference.
  123. */
  124. protected function sortDefinitions(array &$definitions) {
  125. uasort($definitions, ['\Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
  126. }
  127. }

Classes

Name Description
PluginManager Base class for Bootstrap plugin managers.