6fef9e227f4e676a4d148e58ae631f8124f3959d
[yaffs-website] / vendor / drush / drush / src / Commands / ValidatorsCommands.php
1 <?php
2 namespace Drush\Commands;
3
4 use Consolidation\AnnotatedCommand\AnnotationData;
5 use Consolidation\AnnotatedCommand\CommandError;
6 use Consolidation\AnnotatedCommand\CommandData;
7 use Drush\Utils\StringUtils;
8 use Symfony\Component\Console\Input\Input;
9
10 /*
11  * Common validation providers. Use them by adding an annotation to your method.
12  */
13 class ValidatorsCommands
14 {
15
16     /**
17      * Validate that passed entity names are valid.
18      * @see \Drush\Commands\core\ViewsCommands::execute for an example.
19      *
20      * @hook validate @validate-entity-load
21      * @param \Consolidation\AnnotatedCommand\CommandData $commandData
22      * @return \Consolidation\AnnotatedCommand\CommandError|null
23      */
24     public function validateEntityLoad(CommandData $commandData)
25     {
26         list($entity_type, $arg_name) = explode(' ', $commandData->annotationData()->get('validate-entity-load', null));
27         $names = StringUtils::csvToArray($commandData->input()->getArgument($arg_name));
28         $loaded = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple($names);
29         if ($missing = array_diff($names, array_keys($loaded))) {
30             $msg = dt('Unable to load the !type: !str', ['!type' => $entity_type, '!str' => implode(', ', $missing)]);
31             return new CommandError($msg);
32         }
33     }
34
35     /**
36      * Validate that passed module names are enabled. We use pre-init phase because interact() methods run early and they
37      * need to know that their module is enabled (e.g. image-flush).
38      *
39      * @see \Drush\Commands\core\WatchdogCommands::show for an example.
40      *
41      * @hook pre-init @validate-module-enabled
42      * @param \Consolidation\AnnotatedCommand\CommandData $commandData
43      * @return \Consolidation\AnnotatedCommand\CommandError|null
44      */
45     public function validateModuleEnabled(Input $input, AnnotationData $annotationData)
46     {
47         $names = StringUtils::csvToArray($annotationData->get('validate-module-enabled'));
48         $loaded = \Drupal::moduleHandler()->getModuleList();
49         if ($missing = array_diff($names, array_keys($loaded))) {
50             $msg = dt('Missing module: !str', ['!str' => implode(', ', $missing)]);
51             throw new \Exception($msg);
52         }
53     }
54
55     /**
56      * Validate that the file path exists.
57      *
58      * Annotation value should be the name of the argument containing the path.
59      *
60      * @hook validate @validate-file-exists
61      * @param \Consolidation\AnnotatedCommand\CommandData $commandData
62      * @return \Consolidation\AnnotatedCommand\CommandError|null
63      */
64     public function validateFileExists(CommandData $commandData)
65     {
66         $missing = [];
67         $arg_names = _convert_csv_to_array($commandData->annotationData()->get('validate-file-exists', null));
68         foreach ($arg_names as $arg_name) {
69             if ($commandData->input()->hasArgument($arg_name)) {
70                 $path = $commandData->input()->getArgument($arg_name);
71             } elseif ($commandData->input()->hasOption($arg_name)) {
72                 $path = $commandData->input()->getOption($arg_name);
73             }
74             if (!empty($path) && !file_exists($path)) {
75                 $missing[] = $path;
76             }
77             unset($path);
78         }
79
80         if ($missing) {
81             $msg = dt('File(s) not found: !paths', ['!paths' => implode(', ', $missing)]);
82             return new CommandError($msg);
83         }
84     }
85
86     /**
87      * Validate that required PHP extension exists.
88      *
89      * Annotation value should be extension name. If multiple, delimit by a comma.
90      *
91      * @hook validate @validate-php-extension
92      * @param \Consolidation\AnnotatedCommand\CommandData $commandData
93      * @return \Consolidation\AnnotatedCommand\CommandError|null
94      */
95     public function validatePHPExtension(CommandData $commandData)
96     {
97         $missing = [];
98         $arg_names = _convert_csv_to_array($commandData->annotationData()->get('validate-php-extension', null));
99         foreach ($arg_names as $arg_name) {
100             if (!extension_loaded($arg_name)) {
101                 $missing[] = $arg_name;
102             }
103         }
104
105         if ($missing) {
106             $args = ['!command' => $commandData->input(), '!dependencies' => implode(', ', $missing)];
107             return new CommandError(dt('Command !command needs the following PHP extensions installed and enabled: !dependencies.', $args));
108         }
109     }
110
111     /**
112      * Validate that the permission exists.
113      *
114      * Annotation value should be the name of the argument/option containing the permission(s).
115      *
116      * @hook validate @validate-permissions
117      * @param \Consolidation\AnnotatedCommand\CommandData $commandData
118      * @return \Consolidation\AnnotatedCommand\CommandError|null
119      */
120     public function validatePermissions(CommandData $commandData)
121     {
122         $missing = [];
123         $arg_or_option_name = $commandData->annotationData()->get('validate-permissions', null);
124         if ($commandData->input()->hasArgument($arg_or_option_name)) {
125             $permissions = StringUtils::csvToArray($commandData->input()->getArgument($arg_or_option_name));
126         } else {
127             $permissions = StringUtils::csvToArray($commandData->input()->getOption($arg_or_option_name));
128         }
129         $all_permissions = array_keys(\Drupal::service('user.permissions')->getPermissions());
130         $missing = array_diff($permissions, $all_permissions);
131         if ($missing) {
132             $msg = dt('Permission(s) not found: !perms', ['!perms' => implode(', ', $missing)]);
133             return new CommandError($msg);
134         }
135     }
136 }