1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace PhpTaskman\Core\Robo\Plugin\Commands; |
6
|
|
|
|
7
|
|
|
use Consolidation\AnnotatedCommand\AnnotatedCommand; |
8
|
|
|
use PhpTaskman\Core\Robo\Task\CollectionFactory\LoadCollectionFactoryTasks; |
9
|
|
|
use Robo\Collection\CollectionBuilder; |
10
|
|
|
use Robo\Exception\TaskException; |
11
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DynamicCommands. |
15
|
|
|
*/ |
16
|
|
|
final class YamlCommands extends AbstractCommands |
17
|
|
|
{ |
18
|
|
|
use LoadCollectionFactoryTasks; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Bind input values of custom command options to config entries. |
22
|
|
|
* |
23
|
|
|
* @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event |
24
|
|
|
* |
25
|
|
|
* @hook pre-command-event * |
26
|
|
|
*/ |
27
|
|
|
public function bindInputOptionsToConfig(ConsoleCommandEvent $event): void |
28
|
|
|
{ |
29
|
|
|
$command = $event->getCommand(); |
30
|
|
|
|
31
|
|
|
if (null === $command) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (AnnotatedCommand::class !== \get_class($command)) { |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!\is_subclass_of($command, AnnotatedCommand::class)) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @var \Consolidation\AnnotatedCommand\AnnotatedCommand $command */ |
44
|
|
|
/** @var \Consolidation\AnnotatedCommand\AnnotationData $annotatedData */ |
45
|
|
|
$annotatedData = $command->getAnnotationData(); |
|
|
|
|
46
|
|
|
if (!$annotatedData->get('dynamic-command')) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Dynamic commands may define their own options bound to specific configuration. Dynamically set the |
51
|
|
|
// configuration from command options. |
52
|
|
|
$config = $this->getConfig(); |
53
|
|
|
$commands = $config->get('commands'); |
54
|
|
|
|
55
|
|
|
if (empty($commands[$command->getName()]['options'])) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach ($commands[$command->getName()]['options'] as $optionName => $option) { |
60
|
|
|
if (empty($option['config']) && $event->getInput()->hasOption($optionName)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$inputValue = $event->getInput()->getOption($optionName); |
65
|
|
|
|
66
|
|
|
if (null === $inputValue) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$config->set( |
71
|
|
|
$option['config'], |
72
|
|
|
$event->getInput()->getOption($optionName) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getConfigurationFile(): string |
81
|
|
|
{ |
82
|
|
|
return __DIR__ . '/../../../../config/commands/default.yml'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getDefaultConfigurationFile(): string |
89
|
|
|
{ |
90
|
|
|
return __DIR__ . '/../../../../config/default.yml'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Run a task. |
95
|
|
|
* |
96
|
|
|
* @dynamic-command true |
97
|
|
|
* |
98
|
|
|
* @throws \Robo\Exception\TaskException |
99
|
|
|
* |
100
|
|
|
* @return \Robo\Collection\CollectionBuilder |
101
|
|
|
*/ |
102
|
|
|
public function runTasks(): CollectionBuilder |
103
|
|
|
{ |
104
|
|
|
$command = $this->input()->getArgument('command'); |
105
|
|
|
|
106
|
|
|
if (!\is_string($command)) { |
107
|
|
|
throw new TaskException($this, 'The command must be a string.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$tasks = $this->getConfig()->get('commands.' . $command); |
111
|
|
|
|
112
|
|
|
$inputOptions = []; |
113
|
|
|
foreach ($this->input()->getOptions() as $name => $value) { |
114
|
|
|
if ($this->input()->hasParameterOption('--' . $name)) { |
115
|
|
|
$inputOptions[$name] = $value; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->taskCollectionFactory($tasks, $inputOptions); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|