2 namespace Drush\Commands;
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;
11 * Common validation providers. Use them by adding an annotation to your method.
13 class ValidatorsCommands
17 * Validate that passed entity names are valid.
18 * @see \Drush\Commands\core\ViewsCommands::execute for an example.
20 * @hook validate @validate-entity-load
21 * @param \Consolidation\AnnotatedCommand\CommandData $commandData
22 * @return \Consolidation\AnnotatedCommand\CommandError|null
24 public function validateEntityLoad(CommandData $commandData)
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);
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).
39 * @see \Drush\Commands\core\WatchdogCommands::show for an example.
41 * @hook pre-init @validate-module-enabled
42 * @param \Consolidation\AnnotatedCommand\CommandData $commandData
43 * @return \Consolidation\AnnotatedCommand\CommandError|null
45 public function validateModuleEnabled(Input $input, AnnotationData $annotationData)
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);
56 * Validate that the file path exists.
58 * Annotation value should be the name of the argument containing the path.
60 * @hook validate @validate-file-exists
61 * @param \Consolidation\AnnotatedCommand\CommandData $commandData
62 * @return \Consolidation\AnnotatedCommand\CommandError|null
64 public function validateFileExists(CommandData $commandData)
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);
74 if (!empty($path) && !file_exists($path)) {
81 $msg = dt('File(s) not found: !paths', ['!paths' => implode(', ', $missing)]);
82 return new CommandError($msg);
87 * Validate that required PHP extension exists.
89 * Annotation value should be extension name. If multiple, delimit by a comma.
91 * @hook validate @validate-php-extension
92 * @param \Consolidation\AnnotatedCommand\CommandData $commandData
93 * @return \Consolidation\AnnotatedCommand\CommandError|null
95 public function validatePHPExtension(CommandData $commandData)
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;
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));
112 * Validate that the permission exists.
114 * Annotation value should be the name of the argument/option containing the permission(s).
116 * @hook validate @validate-permissions
117 * @param \Consolidation\AnnotatedCommand\CommandData $commandData
118 * @return \Consolidation\AnnotatedCommand\CommandError|null
120 public function validatePermissions(CommandData $commandData)
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));
127 $permissions = StringUtils::csvToArray($commandData->input()->getOption($arg_or_option_name));
129 $all_permissions = array_keys(\Drupal::service('user.permissions')->getPermissions());
130 $missing = array_diff($permissions, $all_permissions);
132 $msg = dt('Permission(s) not found: !perms', ['!perms' => implode(', ', $missing)]);
133 return new CommandError($msg);