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