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