1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PhpTaskman\Core; |
6
|
|
|
|
7
|
|
|
use Composer\Autoload\ClassLoader; |
8
|
|
|
use Consolidation\AnnotatedCommand\AnnotatedCommand; |
9
|
|
|
use League\Container\Inflector\Inflector; |
10
|
|
|
use PhpTaskman\Core\Plugin\Task\YamlTask; |
11
|
|
|
use PhpTaskman\Core\Robo\Plugin\Commands\YamlCommands; |
12
|
|
|
use League\Container\ContainerAwareTrait; |
13
|
|
|
use Robo\Application; |
14
|
|
|
use Robo\Collection\CollectionBuilder; |
15
|
|
|
use Robo\Common\ConfigAwareTrait; |
16
|
|
|
use Robo\Contract\BuilderAwareInterface; |
17
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Runner. |
25
|
|
|
*/ |
26
|
|
|
final class Runner |
27
|
|
|
{ |
28
|
|
|
use ConfigAwareTrait; |
29
|
|
|
use ContainerAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Application |
33
|
|
|
*/ |
34
|
|
|
private $application; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \Composer\Autoload\ClassLoader |
38
|
|
|
*/ |
39
|
|
|
private $classLoader; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var InputInterface |
43
|
|
|
*/ |
44
|
|
|
private $input; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var OutputInterface |
48
|
|
|
*/ |
49
|
|
|
private $output; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \Robo\Runner |
53
|
|
|
*/ |
54
|
|
|
private $runner; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $workingDir; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Runner constructor. |
63
|
|
|
* |
64
|
|
|
* @param InputInterface $input |
65
|
|
|
* @param OutputInterface $output |
66
|
|
|
* @param ClassLoader $classLoader |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
InputInterface $input = null, |
70
|
|
|
OutputInterface $output = null, |
71
|
|
|
ClassLoader $classLoader = null |
72
|
|
|
) { |
73
|
|
|
$this->input = $input ?? new ArgvInput(); |
74
|
|
|
$this->output = $output ?? new ConsoleOutput(); |
75
|
|
|
$this->classLoader = $classLoader ?? new ClassLoader(); |
76
|
|
|
|
77
|
|
|
$this->workingDir = $this->getWorkingDir($this->input); |
78
|
|
|
\chdir($this->workingDir); |
79
|
|
|
|
80
|
|
|
$this->config = Taskman::createConfiguration( |
81
|
|
|
[], |
82
|
|
|
$this->workingDir |
83
|
|
|
); |
84
|
|
|
$this->application = Taskman::createDefaultApplication( |
85
|
|
|
null, |
86
|
|
|
null, |
87
|
|
|
$this->workingDir |
88
|
|
|
); |
89
|
|
|
$this->container = Taskman::createContainer( |
90
|
|
|
$this->input, |
91
|
|
|
$this->output, |
92
|
|
|
$this->application, |
93
|
|
|
$this->config, |
94
|
|
|
$this->classLoader |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->runner = Taskman::createDefaultRunner($this->container); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param mixed $args |
102
|
|
|
* |
103
|
|
|
* @throws \ReflectionException |
104
|
|
|
* |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
|
|
public function run($args) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
// Register command classes. |
110
|
|
|
$this->runner->registerCommandClasses($this->application, [YamlCommands::class]); |
111
|
|
|
|
112
|
|
|
// Register commands defined in task.yml file. |
113
|
|
|
$this->registerDynamicCommands($this->application); |
114
|
|
|
|
115
|
|
|
// Register tasks |
116
|
|
|
$this->registerDynamicTasks($this->application); |
117
|
|
|
|
118
|
|
|
// Run command. |
119
|
|
|
return $this->runner->run($this->input, $this->output, $this->application); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param bool $hasDefault |
124
|
|
|
* @param mixed $defaultValue |
125
|
|
|
* |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
|
|
protected function getCommandArgumentMode(bool $hasDefault, $defaultValue) |
129
|
|
|
{ |
130
|
|
|
if (!$hasDefault) { |
131
|
|
|
return InputArgument::REQUIRED; |
132
|
|
|
} |
133
|
|
|
if (\is_array($defaultValue)) { |
134
|
|
|
return InputArgument::IS_ARRAY; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return InputArgument::OPTIONAL; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \Consolidation\AnnotatedCommand\AnnotatedCommand $command |
142
|
|
|
* @param array $commandDefinition |
143
|
|
|
*/ |
144
|
|
|
private function addOptions(AnnotatedCommand $command, array $commandDefinition) |
145
|
|
|
{ |
146
|
|
|
// This command doesn't define any option. |
147
|
|
|
if (empty($commandDefinition['options'])) { |
148
|
|
|
return; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$defaults = \array_fill_keys(['shortcut', 'mode', 'description', 'default'], null); |
152
|
|
|
foreach ($commandDefinition['options'] as $optionName => $optionDefinition) { |
153
|
|
|
$optionDefinition += $defaults; |
154
|
|
|
$command->addOption( |
155
|
|
|
'--' . $optionName, |
156
|
|
|
$optionDefinition['shortcut'], |
157
|
|
|
$optionDefinition['mode'], |
158
|
|
|
$optionDefinition['description'], |
159
|
|
|
$optionDefinition['default'] |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $command |
166
|
|
|
* |
167
|
|
|
* @throws \InvalidArgumentException |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
private function getTasks($command) |
172
|
|
|
{ |
173
|
|
|
$commands = $this->getConfig()->get('commands', []); |
174
|
|
|
|
175
|
|
|
if (!isset($commands[$command])) { |
176
|
|
|
throw new \InvalidArgumentException("Custom command '${command}' not defined."); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return !empty($commands[$command]['tasks']) ? $commands[$command]['tasks'] : $commands[$command]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
184
|
|
|
* |
185
|
|
|
* @return mixed |
186
|
|
|
*/ |
187
|
|
|
private function getWorkingDir(InputInterface $input) |
188
|
|
|
{ |
189
|
|
|
return $input->getParameterOption('--working-dir', \getcwd()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param \Robo\Application $application |
194
|
|
|
*/ |
195
|
|
|
private function registerDynamicCommands(Application $application) |
196
|
|
|
{ |
197
|
|
|
$customCommands = $this->getConfig()->get('commands', []); |
198
|
|
|
foreach ($customCommands as $name => $commandDefinition) { |
199
|
|
|
/** @var \Consolidation\AnnotatedCommand\AnnotatedCommandFactory $commandFactory */ |
200
|
|
|
$commandFileName = YamlCommands::class . 'Commands'; |
201
|
|
|
$commandClass = $this->container->get($commandFileName); |
202
|
|
|
$commandFactory = $this->container->get('commandFactory'); |
203
|
|
|
$commandInfo = $commandFactory->createCommandInfo($commandClass, 'runTasks'); |
204
|
|
|
|
205
|
|
|
$commandDefinition += ['options' => []]; |
206
|
|
|
foreach ($commandDefinition['options'] as &$option) { |
207
|
|
|
if (isset($option['mode'])) { |
208
|
|
|
continue; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$option['mode'] = $this->getCommandArgumentMode( |
212
|
|
|
isset($option['default']), |
213
|
|
|
$option['default'] ?? null |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$command = $commandFactory->createCommand($commandInfo, $commandClass)->setName($name); |
218
|
|
|
|
219
|
|
|
// Dynamic commands may define their own options. |
220
|
|
|
$this->addOptions($command, $commandDefinition); |
221
|
|
|
|
222
|
|
|
// Append also options of subsequent tasks. |
223
|
|
|
foreach ($this->getTasks($name) as $taskEntry) { |
224
|
|
|
if (!\is_array($taskEntry)) { |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if (!isset($taskEntry['task'])) { |
229
|
|
|
continue; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if ('run' !== $taskEntry['task']) { |
233
|
|
|
continue; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (empty($taskEntry['command'])) { |
237
|
|
|
continue; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// This is a 'run' task. |
241
|
|
|
if (!empty($customCommands[$taskEntry['command']])) { |
242
|
|
|
// Add the options of another custom command. |
243
|
|
|
$this->addOptions($command, $customCommands[$taskEntry['command']]); |
244
|
|
|
} else { |
245
|
|
|
// Add the options of an already registered command. |
246
|
|
|
if ($this->application->has($taskEntry['command'])) { |
247
|
|
|
$subCommand = $this->application->get($taskEntry['command']); |
248
|
|
|
$command->addOptions($subCommand->getDefinition()->getOptions()); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$application->add($command); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param \Robo\Application $application |
259
|
|
|
* |
260
|
|
|
* @throws \ReflectionException |
261
|
|
|
*/ |
262
|
|
|
private function registerDynamicTasks(Application $application) |
|
|
|
|
263
|
|
|
{ |
264
|
|
|
$classes = Taskman::discoverTasksClasses('Plugin'); |
265
|
|
|
|
266
|
|
|
/** @var \ReflectionClass[] $tasks */ |
267
|
|
|
$tasks = []; |
268
|
|
|
foreach ($classes as $className) { |
269
|
|
|
$class = new \ReflectionClass($className); |
270
|
|
|
if (!$class->isInstantiable()) { |
271
|
|
|
continue; |
272
|
|
|
} |
273
|
|
|
$tasks[] = $class; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$builder = CollectionBuilder::create($this->container, ''); |
277
|
|
|
|
278
|
|
|
$inflector = $this->container->inflector(BuilderAwareInterface::class); |
279
|
|
|
if ($inflector instanceof Inflector) { |
|
|
|
|
280
|
|
|
$inflector->invokeMethod('setBuilder', [$builder]); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// Register custom Task classes. |
284
|
|
|
foreach ($tasks as $taskReflectionClass) { |
285
|
|
|
$this->container->add( |
286
|
|
|
'task.' . $taskReflectionClass->getConstant('NAME'), |
287
|
|
|
$taskReflectionClass->getName() |
288
|
|
|
); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// Register custom YAML tasks. |
292
|
|
|
$customTasks = $this->getConfig()->get('tasks', []); |
293
|
|
|
|
294
|
|
|
foreach ($customTasks as $name => $tasks) { |
295
|
|
|
$this->container->add( |
296
|
|
|
'task.' . $name, |
297
|
|
|
YamlTask::class |
298
|
|
|
)->withArgument($tasks); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.