Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Shared / ModuleTrait.php
1 <?php
2
3 namespace Drupal\Console\Command\Shared;
4
5 use Drupal\Console\Core\Style\DrupalStyle;
6
7 /**
8  * Class ModuleTrait
9  *
10  * @package Drupal\Console\Command
11  */
12 trait ModuleTrait
13 {
14     /**
15      * Ask the user to choose a module or profile.
16      *
17      * @param DrupalStyle $io
18      *   Console interface.
19      * @param bool        $showProfile
20      *   If profiles should be discovered.
21      *
22      * @throws \Exception
23      *   When no modules are found.
24      *
25      * @return string
26      */
27     public function moduleQuestion(DrupalStyle $io, $showProfile = true)
28     {
29         $modules = $this->extensionManager->discoverModules()
30             ->showInstalled()
31             ->showUninstalled()
32             ->showNoCore()
33             ->getList(true);
34
35         if ($showProfile) {
36             $profiles = $this->extensionManager->discoverProfiles()
37                 ->showInstalled()
38                 ->showUninstalled()
39                 ->showNoCore()
40                 ->showCore()
41                 ->getList(true);
42
43             $modules = array_merge($modules, $profiles);
44         }
45
46         if (empty($modules)) {
47             throw new \Exception('No extension available, execute the proper generator command to generate one.');
48         }
49
50         $module = $io->choiceNoList(
51             $this->trans('commands.common.questions.module'),
52             $modules
53         );
54
55         return $module;
56     }
57
58     /**
59      * Verify that install requirements for a list of modules are met.
60      *
61      * @param string[]    $module
62      *   List of modules to verify.
63      * @param DrupalStyle $io
64      *   Console interface.
65      *
66      * @throws \Exception
67      *   When one or more requirements are not met.
68      */
69     public function moduleRequirement(array $module, DrupalStyle $io)
70     {
71         // TODO: Module dependencies should also be checked
72         // for unmet requirements recursively.
73         $fail = false;
74         foreach ($module as $module_name) {
75             module_load_install($module_name);
76             if ($requirements = \Drupal::moduleHandler()->invoke($module_name, 'requirements', ['install'])) {
77                 foreach ($requirements as $requirement) {
78                     if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
79                         $io->info("Module '{$module_name}' cannot be installed: " . $requirement['title'] . ' | ' . $requirement['value']);
80                         $fail = true;
81                     }
82                 }
83             }
84         }
85         if ($fail) {
86             throw new \Exception("Some module install requirements are not met.");
87         }
88     }
89 }