a1a79a1a0fd4b5c0853bd5bbb3ae238fe5e3ef83
[yaffs-website] / vendor / drupal / console / src / Extension / Manager.php
1 <?php
2
3 namespace Drupal\Console\Extension;
4
5 use Drupal\Console\Utils\Site;
6
7 /**
8  * Class ExtensionManager
9  *
10  * @package Drupal\Console
11  */
12 class Manager
13 {
14     /**
15      * @var Site
16      */
17     protected $site;
18     /**
19      * @var string
20      */
21     protected $appRoot;
22
23     /**
24      * @var array
25      */
26     private $extensions = [];
27
28     /**
29      * @var array
30      */
31     private $filters = [];
32
33     /**
34      * @var string
35      */
36     private $extension = null;
37
38     /**
39      * ExtensionManager constructor.
40      *
41      * @param Site   $site
42      * @param string $appRoot
43      */
44     public function __construct(
45         Site $site,
46         $appRoot
47     ) {
48         $this->site = $site;
49         $this->appRoot = $appRoot;
50         $this->initialize();
51     }
52
53     /**
54      * @return $this
55      */
56     public function showInstalled()
57     {
58         $this->filters['showInstalled'] = true;
59         return $this;
60     }
61
62     /**
63      * @return $this
64      */
65     public function showUninstalled()
66     {
67         $this->filters['showUninstalled'] = true;
68         return $this;
69     }
70
71     /**
72      * @return $this
73      */
74     public function showCore()
75     {
76         $this->filters['showCore'] = true;
77         return $this;
78     }
79
80     /**
81      * @return $this
82      */
83     public function showNoCore()
84     {
85         $this->filters['showNoCore'] = true;
86         return $this;
87     }
88
89     /**
90      * @param string $nameOnly
91      * @return array
92      */
93     public function getList($nameOnly)
94     {
95         return $this->getExtensions($this->extension, $nameOnly);
96     }
97
98     /**
99      * @return $this
100      */
101     public function discoverModules()
102     {
103         $this->initialize();
104         $this->discoverExtension('module');
105
106         return $this;
107     }
108
109     /**
110      * @return $this
111      */
112     public function discoverThemes()
113     {
114         $this->initialize();
115         $this->discoverExtension('theme');
116
117         return $this;
118     }
119
120     /**
121      * @return $this
122      */
123     public function discoverProfiles()
124     {
125         $this->initialize();
126         $this->discoverExtension('profile');
127
128         return $this;
129     }
130
131     /**
132      * @param string $extension
133      */
134     private function discoverExtension($extension)
135     {
136         $this->extension = $extension;
137         $this->extensions[$extension] = $this->discoverExtensions($extension);
138     }
139
140     /**
141      * initializeFilters
142      */
143     private function initialize()
144     {
145         $this->extension = 'module';
146         $this->extensions = [
147             'module' => [],
148             'theme' => [],
149             'profile' => [],
150         ];
151         $this->filters = [
152             'showInstalled' => false,
153             'showUninstalled' => false,
154             'showCore' => false,
155             'showNoCore' => false
156         ];
157     }
158
159     /**
160      * @param string     $type
161      * @param bool|false $nameOnly
162      * @return array
163      */
164     private function getExtensions(
165         $type = 'module',
166         $nameOnly = false
167     ) {
168         $showInstalled = $this->filters['showInstalled'];
169         $showUninstalled = $this->filters['showUninstalled'];
170         $showCore = $this->filters['showCore'];
171         $showNoCore = $this->filters['showNoCore'];
172
173         $extensions = [];
174         if (!array_key_exists($type, $this->extensions)) {
175             return $extensions;
176         }
177
178         foreach ($this->extensions[$type] as $extension) {
179             $name = $extension->getName();
180
181             $isInstalled = false;
182             if (property_exists($extension, 'status')) {
183                 $isInstalled = ($extension->status)?true:false;
184             }
185             if (!$showInstalled && $isInstalled) {
186                 continue;
187             }
188             if (!$showUninstalled && !$isInstalled) {
189                 continue;
190             }
191             if (!$showCore && $extension->origin == 'core') {
192                 continue;
193             }
194             if (!$showNoCore && $extension->origin != 'core') {
195                 continue;
196             }
197
198             $extensions[$name] = $extension;
199         }
200
201
202         return $nameOnly?array_keys($extensions):$extensions;
203     }
204
205     /**
206      * @param string $type
207      * @return \Drupal\Core\Extension\Extension[]
208      */
209     private function discoverExtensions($type)
210     {
211         if ($type === 'module') {
212             $this->site->loadLegacyFile('/core/modules/system/system.module');
213             system_rebuild_module_data();
214         }
215
216         if ($type === 'theme') {
217             $themeHandler = \Drupal::service('theme_handler');
218             $themeHandler->rebuildThemeData();
219         }
220
221         /*
222          * @see Remove DrupalExtensionDiscovery subclass once
223          * https://www.drupal.org/node/2503927 is fixed.
224          */
225         $discovery = new Discovery($this->appRoot);
226         $discovery->reset();
227
228         return $discovery->scan($type);
229     }
230
231     /**
232      * @param string $name
233      * @return \Drupal\Console\Extension\Extension
234      */
235     public function getModule($name)
236     {
237         if ($extension = $this->getExtension('module', $name)) {
238             return $this->createExtension($extension);
239         }
240
241         return null;
242     }
243
244     /**
245      * @param string $name
246      * @return \Drupal\Console\Extension\Extension
247      */
248     public function getProfile($name)
249     {
250         if ($extension = $this->getExtension('profile', $name)) {
251             return $this->createExtension($extension);
252         }
253
254         return null;
255     }
256
257     /**
258      * @param string $name
259      * @return \Drupal\Console\Extension\Extension
260      */
261     public function getTheme($name)
262     {
263         if ($extension = $this->getExtension('theme', $name)) {
264             return $this->createExtension($extension);
265         }
266
267         return null;
268     }
269
270     /**
271      * @param string $type
272      * @param string $name
273      *
274      * @return \Drupal\Core\Extension\Extension
275      */
276     private function getExtension($type, $name)
277     {
278         if (!$this->extensions[$type]) {
279             $this->discoverExtension($type);
280         }
281
282         if (array_key_exists($name, $this->extensions[$type])) {
283             return $this->extensions[$type][$name];
284         }
285
286         return null;
287     }
288
289     /**
290      * @param \Drupal\Core\Extension\Extension $extension
291      * @return \Drupal\Console\Extension\Extension
292      */
293     private function createExtension($extension)
294     {
295         $consoleExtension = new Extension(
296             $this->appRoot,
297             $extension->getType(),
298             $extension->getPathname(),
299             $extension->getExtensionFilename()
300         );
301         $consoleExtension->unserialize($extension->serialize());
302
303         return $consoleExtension;
304     }
305
306     /**
307      * @param string   $testType
308      * @param $fullPath
309      * @return string
310      */
311     public function getTestPath($testType, $fullPath = false)
312     {
313         return $this->getPath($fullPath) . '/Tests/' . $testType;
314     }
315
316     public function validateModuleFunctionExist($moduleName, $function, $moduleFile = null)
317     {
318         //Load module file to prevent issue of missing functions used in update
319         $module = $this->getModule($moduleName);
320         $modulePath = $module->getPath();
321         if ($moduleFile) {
322             $this->site->loadLegacyFile($modulePath . '/'. $moduleFile);
323         } else {
324             $this->site->loadLegacyFile($modulePath . '/' . $module->getName() . '.module');
325         }
326
327         if (function_exists($function)) {
328             return true;
329         }
330         return false;
331     }
332
333     /**
334      * @param string $moduleName
335      * @param string $pluginType
336      * @return string
337      */
338     public function getPluginPath($moduleName, $pluginType)
339     {
340         $module = $this->getModule($moduleName);
341
342         return $module->getPath() . '/src/Plugin/'.$pluginType;
343     }
344
345     public function getDrupalExtension($type, $name)
346     {
347         $extension = $this->getExtension($type, $name);
348         return $this->createExtension($extension);
349     }
350 }