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. * Retrieves the cache tags used to invalidate caches.
  77. *
  78. * @return array
  79. * An indexed array of cache tags.
  80. */
  81. public function getCacheTags() {
  82. return [Bootstrap::CACHE_TAG];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getDefinitions($sorted = TRUE) {
  88. $definitions = parent::getDefinitions();
  89. if ($sorted) {
  90. uasort($definitions, ['\Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
  91. }
  92. return $definitions;
  93. }
  94. /**
  95. * Retrieves all definitions where the plugin ID matches a certain criteria.
  96. *
  97. * @param string $regex
  98. * The regex pattern to match.
  99. *
  100. * @return array[]
  101. * An array of plugin definitions (empty array if no definitions were
  102. * found). Keys are plugin IDs.
  103. */
  104. public function getDefinitionsLike($regex) {
  105. $definitions = [];
  106. foreach ($this->getDefinitions() as $plugin_id => $definition) {
  107. if (preg_match($regex, $plugin_id)) {
  108. $definitions[$plugin_id] = $definition;
  109. }
  110. }
  111. ksort($definitions, SORT_NATURAL);
  112. return $definitions;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. protected function providerExists($provider) {
  118. return $this->themeHandler->themeExists($provider);
  119. }
  120. }

Classes

Name Description
PluginManager Base class for Bootstrap plugin managers.