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