-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathCommandsValidator.php
191 lines (155 loc) · 4.64 KB
/
CommandsValidator.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Created by valantic CX Austria GmbH
*
*/
namespace Elements\Bundle\ProcessManagerBundle\Service;
use Elements\Bundle\ProcessManagerBundle\ExecutionTrait;
use Elements\Bundle\ProcessManagerBundle\Model\Configuration;
use Pimcore\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
class CommandsValidator
{
protected string $strategy;
/**
* @var array<string>
*/
protected array $whiteList = [];
/**
* @var array<string>
*/
protected array $blackList = [];
/**
* @param string $strategy
* @param array<string> $whiteList
* @param array<string> $blackList
*/
public function __construct(string $strategy = 'default', array $whiteList = [], array $blackList = [])
{
$this->setStrategy($strategy);
$this->setWhiteList($whiteList);
//add 'debug:translation', 'translation:extract' as they cause issues on Pimcore 11.1.4
$blackList = array_merge($blackList, ['debug:translation', 'translation:extract']);
$this->setBlackList($blackList);
}
public function validateCommandConfiguration(LazyCommand|Command $command, Configuration $configuration): void
{
$settings = $configuration->getExecutorSettingsAsArray();
$values = $settings['values'];
$commandOptions = $values['commandOptions'] ?? '';
if (is_callable([$command, 'validatedCommandOptions'])) {
$command->validatedCommandOptions($commandOptions, $configuration);
}
}
/**
* @return array<mixed>
*/
public function getValidCommands(): array
{
$application = new Application(\Pimcore::getKernel());
$commands = $this->{'getCommands' . ucfirst($this->getStrategy())}($application->all());
ksort($commands);
return $commands;
}
/**
* @param array<mixed> $commands
*
* @return array<mixed>
*/
protected function getCommandsAll(array $commands): array
{
return $commands;
}
/**
* @param array<mixed> $commands
*
* @return array<mixed>
*/
protected function getCommandsDefault(array $commands): array
{
$validCommands = [];
/**
* @var Command $command
*/
foreach ($commands as $name => $command) {
if (in_array($name, $this->getBlackList())) {
continue;
}
if (in_array($name, $this->getWhiteList())) {
$validCommands[$name] = $command;
continue;
}
$useTrait = in_array(ExecutionTrait::class, $this->classUsesTraits($command));
if ($useTrait) {
$validCommands[$name] = $command;
}
}
return $validCommands;
}
/**
* @return array<string>
*/
protected function classUsesTraits(LazyCommand|Command $class, bool $autoload = true): array
{
if ($class instanceof LazyCommand) {
$class = $class->getCommand();
}
$traits = [];
// Get traits of all parent classes
do {
$traits = array_merge(class_uses($class, $autoload), $traits);
// @phpstan-ignore-next-line
} while ($class = get_parent_class($class));
// Get traits of all parent traits
$traitsToSearch = $traits;
while ($traitsToSearch !== []) {
$newTraits = class_uses(array_pop($traitsToSearch), $autoload);
$traits = array_merge($newTraits, $traits);
$traitsToSearch = array_merge($newTraits, $traitsToSearch);
}
foreach (array_keys($traits) as $trait) {
$traits = array_merge(class_uses($trait, $autoload), $traits);
}
return array_unique($traits);
}
public function getStrategy(): string
{
return $this->strategy;
}
public function setStrategy(string $strategy): static
{
$this->strategy = $strategy;
return $this;
}
/**
* @return array<string>
*/
public function getWhiteList(): array
{
return $this->whiteList;
}
/**
* @param array<string> $whiteList
*/
public function setWhiteList(array $whiteList): static
{
$this->whiteList = $whiteList;
return $this;
}
/**
* @return array<string>
*/
public function getBlackList(): array
{
return $this->blackList;
}
/**
* @param array<string> $blackList
*/
public function setBlackList(array $blackList): static
{
$this->blackList = $blackList;
return $this;
}
}