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