Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Shared / ModuleTrait.php
1 <?php
2
3 namespace Drupal\Console\Command\Shared;
4
5 /**
6  * Class ModuleTrait
7  *
8  * @package Drupal\Console\Command
9  */
10 trait ModuleTrait
11 {
12     /**
13      * Ask the user to choose a module or profile.
14      *
15      * @param bool        $showProfile
16      *   If profiles should be discovered.
17      *
18      * @throws \Exception
19      *   When no modules are found.
20      *
21      * @return string
22      */
23     public function moduleQuestion($showProfile = true)
24     {
25         $modules = $this->extensionManager->discoverModules()
26             ->showInstalled()
27             ->showUninstalled()
28             ->showNoCore()
29             ->getList(true);
30
31         if ($showProfile) {
32             $profiles = $this->extensionManager->discoverProfiles()
33                 ->showInstalled()
34                 ->showUninstalled()
35                 ->showNoCore()
36                 ->showCore()
37                 ->getList(true);
38
39             $modules = array_merge($modules, $profiles);
40         }
41
42         if (empty($modules)) {
43             throw new \Exception('No extension available, execute the proper generator command to generate one.');
44         }
45
46         $module = $this->getIo()->choiceNoList(
47             $this->trans('commands.common.questions.module'),
48             $modules
49         );
50
51         return $module;
52     }
53
54     /**
55      * Verify that install requirements for a list of modules are met.
56      *
57      * @param string[]    $module
58      *   List of modules to verify.
59      *
60      * @throws \Exception
61      *   When one or more requirements are not met.
62      */
63     public function moduleRequirement(array $module)
64     {
65         // TODO: Module dependencies should also be checked
66         // for unmet requirements recursively.
67         $fail = false;
68         foreach ($module as $module_name) {
69             module_load_install($module_name);
70             if ($requirements = \Drupal::moduleHandler()->invoke($module_name, 'requirements', ['install'])) {
71                 foreach ($requirements as $requirement) {
72                     if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
73                         $this->getIo()->info("Module '{$module_name}' cannot be installed: " . $requirement['title'] . ' | ' . $requirement['value']);
74                         $fail = true;
75                     }
76                 }
77             }
78         }
79         if ($fail) {
80             throw new \Exception("Some module install requirements are not met.");
81         }
82     }
83
84     /**
85      * Get module name from user.
86      *
87      * @return mixed|string
88      *   Module name.
89      * @throws \Exception
90      *   When module is not found.
91      */
92     public function getModuleOption()
93     {
94         $input = $this->getIo()->getInput();
95         $module = $input->getOption('module');
96         if (!$module) {
97             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
98             $module = $this->moduleQuestion();
99             $input->setOption('module', $module);
100         } else {
101             $missing_modules = $this->validator->getMissingModules([$module]);
102             if ($missing_modules) {
103                 throw new \Exception(
104                     sprintf(
105                         $this->trans(
106                             'commands.module.download.messages.no-releases'
107                         ),
108                         $module
109                     )
110                 );
111             }
112         }
113
114         return $module;
115     }
116 }