@@ -18,22 +18,22 @@ |
||
18 | 18 | */ |
19 | 19 | interface HelperInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * Sets the helper set associated with this helper. |
|
23 | - */ |
|
24 | - public function setHelperSet(HelperSet $helperSet = null); |
|
21 | + /** |
|
22 | + * Sets the helper set associated with this helper. |
|
23 | + */ |
|
24 | + public function setHelperSet(HelperSet $helperSet = null); |
|
25 | 25 | |
26 | - /** |
|
27 | - * Gets the helper set associated with this helper. |
|
28 | - * |
|
29 | - * @return HelperSet A HelperSet instance |
|
30 | - */ |
|
31 | - public function getHelperSet(); |
|
26 | + /** |
|
27 | + * Gets the helper set associated with this helper. |
|
28 | + * |
|
29 | + * @return HelperSet A HelperSet instance |
|
30 | + */ |
|
31 | + public function getHelperSet(); |
|
32 | 32 | |
33 | - /** |
|
34 | - * Returns the canonical name of this helper. |
|
35 | - * |
|
36 | - * @return string The canonical name |
|
37 | - */ |
|
38 | - public function getName(); |
|
33 | + /** |
|
34 | + * Returns the canonical name of this helper. |
|
35 | + * |
|
36 | + * @return string The canonical name |
|
37 | + */ |
|
38 | + public function getName(); |
|
39 | 39 | } |
@@ -18,26 +18,26 @@ |
||
18 | 18 | */ |
19 | 19 | class CommandNotFoundException extends \InvalidArgumentException implements ExceptionInterface |
20 | 20 | { |
21 | - private $alternatives; |
|
21 | + private $alternatives; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param string $message Exception message to throw |
|
25 | - * @param string[] $alternatives List of similar defined names |
|
26 | - * @param int $code Exception code |
|
27 | - * @param \Throwable|null $previous Previous exception used for the exception chaining |
|
28 | - */ |
|
29 | - public function __construct(string $message, array $alternatives = [], int $code = 0, \Throwable $previous = null) |
|
30 | - { |
|
31 | - parent::__construct($message, $code, $previous); |
|
23 | + /** |
|
24 | + * @param string $message Exception message to throw |
|
25 | + * @param string[] $alternatives List of similar defined names |
|
26 | + * @param int $code Exception code |
|
27 | + * @param \Throwable|null $previous Previous exception used for the exception chaining |
|
28 | + */ |
|
29 | + public function __construct(string $message, array $alternatives = [], int $code = 0, \Throwable $previous = null) |
|
30 | + { |
|
31 | + parent::__construct($message, $code, $previous); |
|
32 | 32 | |
33 | - $this->alternatives = $alternatives; |
|
34 | - } |
|
33 | + $this->alternatives = $alternatives; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string[] A list of similar defined names |
|
38 | - */ |
|
39 | - public function getAlternatives() |
|
40 | - { |
|
41 | - return $this->alternatives; |
|
42 | - } |
|
36 | + /** |
|
37 | + * @return string[] A list of similar defined names |
|
38 | + */ |
|
39 | + public function getAlternatives() |
|
40 | + { |
|
41 | + return $this->alternatives; |
|
42 | + } |
|
43 | 43 | } |
@@ -24,24 +24,24 @@ discard block |
||
24 | 24 | */ |
25 | 25 | class HelpCommand extends Command |
26 | 26 | { |
27 | - private $command; |
|
27 | + private $command; |
|
28 | 28 | |
29 | - /** |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - protected function configure() |
|
33 | - { |
|
34 | - $this->ignoreValidationErrors(); |
|
29 | + /** |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + protected function configure() |
|
33 | + { |
|
34 | + $this->ignoreValidationErrors(); |
|
35 | 35 | |
36 | - $this |
|
37 | - ->setName('help') |
|
38 | - ->setDefinition([ |
|
39 | - new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'), |
|
40 | - new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'), |
|
41 | - new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'), |
|
42 | - ]) |
|
43 | - ->setDescription('Display help for a command') |
|
44 | - ->setHelp(<<<'EOF' |
|
36 | + $this |
|
37 | + ->setName('help') |
|
38 | + ->setDefinition([ |
|
39 | + new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'), |
|
40 | + new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'), |
|
41 | + new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command help'), |
|
42 | + ]) |
|
43 | + ->setDescription('Display help for a command') |
|
44 | + ->setHelp(<<<'EOF' |
|
45 | 45 | The <info>%command.name%</info> command displays help for a given command: |
46 | 46 | |
47 | 47 | <info>%command.full_name% list</info> |
@@ -52,32 +52,32 @@ discard block |
||
52 | 52 | |
53 | 53 | To display the list of available commands, please use the <info>list</info> command. |
54 | 54 | EOF |
55 | - ) |
|
56 | - ; |
|
57 | - } |
|
55 | + ) |
|
56 | + ; |
|
57 | + } |
|
58 | 58 | |
59 | - public function setCommand(Command $command) |
|
60 | - { |
|
61 | - $this->command = $command; |
|
62 | - } |
|
59 | + public function setCommand(Command $command) |
|
60 | + { |
|
61 | + $this->command = $command; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
68 | - { |
|
69 | - if (null === $this->command) { |
|
70 | - $this->command = $this->getApplication()->find($input->getArgument('command_name')); |
|
71 | - } |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
68 | + { |
|
69 | + if (null === $this->command) { |
|
70 | + $this->command = $this->getApplication()->find($input->getArgument('command_name')); |
|
71 | + } |
|
72 | 72 | |
73 | - $helper = new DescriptorHelper(); |
|
74 | - $helper->describe($output, $this->command, [ |
|
75 | - 'format' => $input->getOption('format'), |
|
76 | - 'raw_text' => $input->getOption('raw'), |
|
77 | - ]); |
|
73 | + $helper = new DescriptorHelper(); |
|
74 | + $helper->describe($output, $this->command, [ |
|
75 | + 'format' => $input->getOption('format'), |
|
76 | + 'raw_text' => $input->getOption('raw'), |
|
77 | + ]); |
|
78 | 78 | |
79 | - $this->command = null; |
|
79 | + $this->command = null; |
|
80 | 80 | |
81 | - return 0; |
|
82 | - } |
|
81 | + return 0; |
|
82 | + } |
|
83 | 83 | } |
@@ -18,13 +18,13 @@ |
||
18 | 18 | */ |
19 | 19 | interface SignalableCommandInterface |
20 | 20 | { |
21 | - /** |
|
22 | - * Returns the list of signals to subscribe. |
|
23 | - */ |
|
24 | - public function getSubscribedSignals(): array; |
|
21 | + /** |
|
22 | + * Returns the list of signals to subscribe. |
|
23 | + */ |
|
24 | + public function getSubscribedSignals(): array; |
|
25 | 25 | |
26 | - /** |
|
27 | - * The method will be called when the application is signaled. |
|
28 | - */ |
|
29 | - public function handleSignal(int $signal): void; |
|
26 | + /** |
|
27 | + * The method will be called when the application is signaled. |
|
28 | + */ |
|
29 | + public function handleSignal(int $signal): void; |
|
30 | 30 | } |
@@ -24,21 +24,21 @@ discard block |
||
24 | 24 | */ |
25 | 25 | class ListCommand extends Command |
26 | 26 | { |
27 | - /** |
|
28 | - * {@inheritdoc} |
|
29 | - */ |
|
30 | - protected function configure() |
|
31 | - { |
|
32 | - $this |
|
33 | - ->setName('list') |
|
34 | - ->setDefinition([ |
|
35 | - new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'), |
|
36 | - new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'), |
|
37 | - new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'), |
|
38 | - new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'), |
|
39 | - ]) |
|
40 | - ->setDescription('List commands') |
|
41 | - ->setHelp(<<<'EOF' |
|
27 | + /** |
|
28 | + * {@inheritdoc} |
|
29 | + */ |
|
30 | + protected function configure() |
|
31 | + { |
|
32 | + $this |
|
33 | + ->setName('list') |
|
34 | + ->setDefinition([ |
|
35 | + new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'), |
|
36 | + new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'), |
|
37 | + new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt, xml, json, or md)', 'txt'), |
|
38 | + new InputOption('short', null, InputOption::VALUE_NONE, 'To skip describing commands\' arguments'), |
|
39 | + ]) |
|
40 | + ->setDescription('List commands') |
|
41 | + ->setHelp(<<<'EOF' |
|
42 | 42 | The <info>%command.name%</info> command lists all commands: |
43 | 43 | |
44 | 44 | <info>%command.full_name%</info> |
@@ -55,23 +55,23 @@ discard block |
||
55 | 55 | |
56 | 56 | <info>%command.full_name% --raw</info> |
57 | 57 | EOF |
58 | - ) |
|
59 | - ; |
|
60 | - } |
|
58 | + ) |
|
59 | + ; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * {@inheritdoc} |
|
64 | - */ |
|
65 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
66 | - { |
|
67 | - $helper = new DescriptorHelper(); |
|
68 | - $helper->describe($output, $this->getApplication(), [ |
|
69 | - 'format' => $input->getOption('format'), |
|
70 | - 'raw_text' => $input->getOption('raw'), |
|
71 | - 'namespace' => $input->getArgument('namespace'), |
|
72 | - 'short' => $input->getOption('short'), |
|
73 | - ]); |
|
62 | + /** |
|
63 | + * {@inheritdoc} |
|
64 | + */ |
|
65 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
66 | + { |
|
67 | + $helper = new DescriptorHelper(); |
|
68 | + $helper->describe($output, $this->getApplication(), [ |
|
69 | + 'format' => $input->getOption('format'), |
|
70 | + 'raw_text' => $input->getOption('raw'), |
|
71 | + 'namespace' => $input->getArgument('namespace'), |
|
72 | + 'short' => $input->getOption('short'), |
|
73 | + ]); |
|
74 | 74 | |
75 | - return 0; |
|
76 | - } |
|
75 | + return 0; |
|
76 | + } |
|
77 | 77 | } |
@@ -22,190 +22,190 @@ |
||
22 | 22 | */ |
23 | 23 | final class LazyCommand extends Command |
24 | 24 | { |
25 | - private $command; |
|
26 | - private $isEnabled; |
|
27 | - |
|
28 | - public function __construct(string $name, array $aliases, string $description, bool $isHidden, \Closure $commandFactory, ?bool $isEnabled = true) |
|
29 | - { |
|
30 | - $this->setName($name) |
|
31 | - ->setAliases($aliases) |
|
32 | - ->setHidden($isHidden) |
|
33 | - ->setDescription($description); |
|
34 | - |
|
35 | - $this->command = $commandFactory; |
|
36 | - $this->isEnabled = $isEnabled; |
|
37 | - } |
|
38 | - |
|
39 | - public function ignoreValidationErrors(): void |
|
40 | - { |
|
41 | - $this->getCommand()->ignoreValidationErrors(); |
|
42 | - } |
|
43 | - |
|
44 | - public function setApplication(Application $application = null): void |
|
45 | - { |
|
46 | - if ($this->command instanceof parent) { |
|
47 | - $this->command->setApplication($application); |
|
48 | - } |
|
49 | - |
|
50 | - parent::setApplication($application); |
|
51 | - } |
|
52 | - |
|
53 | - public function setHelperSet(HelperSet $helperSet): void |
|
54 | - { |
|
55 | - if ($this->command instanceof parent) { |
|
56 | - $this->command->setHelperSet($helperSet); |
|
57 | - } |
|
58 | - |
|
59 | - parent::setHelperSet($helperSet); |
|
60 | - } |
|
61 | - |
|
62 | - public function isEnabled(): bool |
|
63 | - { |
|
64 | - return $this->isEnabled ?? $this->getCommand()->isEnabled(); |
|
65 | - } |
|
66 | - |
|
67 | - public function run(InputInterface $input, OutputInterface $output): int |
|
68 | - { |
|
69 | - return $this->getCommand()->run($input, $output); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @return $this |
|
74 | - */ |
|
75 | - public function setCode(callable $code): self |
|
76 | - { |
|
77 | - $this->getCommand()->setCode($code); |
|
78 | - |
|
79 | - return $this; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @internal |
|
84 | - */ |
|
85 | - public function mergeApplicationDefinition(bool $mergeArgs = true): void |
|
86 | - { |
|
87 | - $this->getCommand()->mergeApplicationDefinition($mergeArgs); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @return $this |
|
92 | - */ |
|
93 | - public function setDefinition($definition): self |
|
94 | - { |
|
95 | - $this->getCommand()->setDefinition($definition); |
|
96 | - |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - public function getDefinition(): InputDefinition |
|
101 | - { |
|
102 | - return $this->getCommand()->getDefinition(); |
|
103 | - } |
|
104 | - |
|
105 | - public function getNativeDefinition(): InputDefinition |
|
106 | - { |
|
107 | - return $this->getCommand()->getNativeDefinition(); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @return $this |
|
112 | - */ |
|
113 | - public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self |
|
114 | - { |
|
115 | - $this->getCommand()->addArgument($name, $mode, $description, $default); |
|
116 | - |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return $this |
|
122 | - */ |
|
123 | - public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self |
|
124 | - { |
|
125 | - $this->getCommand()->addOption($name, $shortcut, $mode, $description, $default); |
|
126 | - |
|
127 | - return $this; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @return $this |
|
132 | - */ |
|
133 | - public function setProcessTitle(string $title): self |
|
134 | - { |
|
135 | - $this->getCommand()->setProcessTitle($title); |
|
136 | - |
|
137 | - return $this; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @return $this |
|
142 | - */ |
|
143 | - public function setHelp(string $help): self |
|
144 | - { |
|
145 | - $this->getCommand()->setHelp($help); |
|
146 | - |
|
147 | - return $this; |
|
148 | - } |
|
149 | - |
|
150 | - public function getHelp(): string |
|
151 | - { |
|
152 | - return $this->getCommand()->getHelp(); |
|
153 | - } |
|
154 | - |
|
155 | - public function getProcessedHelp(): string |
|
156 | - { |
|
157 | - return $this->getCommand()->getProcessedHelp(); |
|
158 | - } |
|
159 | - |
|
160 | - public function getSynopsis(bool $short = false): string |
|
161 | - { |
|
162 | - return $this->getCommand()->getSynopsis($short); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @return $this |
|
167 | - */ |
|
168 | - public function addUsage(string $usage): self |
|
169 | - { |
|
170 | - $this->getCommand()->addUsage($usage); |
|
171 | - |
|
172 | - return $this; |
|
173 | - } |
|
174 | - |
|
175 | - public function getUsages(): array |
|
176 | - { |
|
177 | - return $this->getCommand()->getUsages(); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return mixed |
|
182 | - */ |
|
183 | - public function getHelper(string $name) |
|
184 | - { |
|
185 | - return $this->getCommand()->getHelper($name); |
|
186 | - } |
|
187 | - |
|
188 | - public function getCommand(): parent |
|
189 | - { |
|
190 | - if (!$this->command instanceof \Closure) { |
|
191 | - return $this->command; |
|
192 | - } |
|
193 | - |
|
194 | - $command = $this->command = ($this->command)(); |
|
195 | - $command->setApplication($this->getApplication()); |
|
196 | - |
|
197 | - if (null !== $this->getHelperSet()) { |
|
198 | - $command->setHelperSet($this->getHelperSet()); |
|
199 | - } |
|
200 | - |
|
201 | - $command->setName($this->getName()) |
|
202 | - ->setAliases($this->getAliases()) |
|
203 | - ->setHidden($this->isHidden()) |
|
204 | - ->setDescription($this->getDescription()); |
|
205 | - |
|
206 | - // Will throw if the command is not correctly initialized. |
|
207 | - $command->getDefinition(); |
|
208 | - |
|
209 | - return $command; |
|
210 | - } |
|
25 | + private $command; |
|
26 | + private $isEnabled; |
|
27 | + |
|
28 | + public function __construct(string $name, array $aliases, string $description, bool $isHidden, \Closure $commandFactory, ?bool $isEnabled = true) |
|
29 | + { |
|
30 | + $this->setName($name) |
|
31 | + ->setAliases($aliases) |
|
32 | + ->setHidden($isHidden) |
|
33 | + ->setDescription($description); |
|
34 | + |
|
35 | + $this->command = $commandFactory; |
|
36 | + $this->isEnabled = $isEnabled; |
|
37 | + } |
|
38 | + |
|
39 | + public function ignoreValidationErrors(): void |
|
40 | + { |
|
41 | + $this->getCommand()->ignoreValidationErrors(); |
|
42 | + } |
|
43 | + |
|
44 | + public function setApplication(Application $application = null): void |
|
45 | + { |
|
46 | + if ($this->command instanceof parent) { |
|
47 | + $this->command->setApplication($application); |
|
48 | + } |
|
49 | + |
|
50 | + parent::setApplication($application); |
|
51 | + } |
|
52 | + |
|
53 | + public function setHelperSet(HelperSet $helperSet): void |
|
54 | + { |
|
55 | + if ($this->command instanceof parent) { |
|
56 | + $this->command->setHelperSet($helperSet); |
|
57 | + } |
|
58 | + |
|
59 | + parent::setHelperSet($helperSet); |
|
60 | + } |
|
61 | + |
|
62 | + public function isEnabled(): bool |
|
63 | + { |
|
64 | + return $this->isEnabled ?? $this->getCommand()->isEnabled(); |
|
65 | + } |
|
66 | + |
|
67 | + public function run(InputInterface $input, OutputInterface $output): int |
|
68 | + { |
|
69 | + return $this->getCommand()->run($input, $output); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @return $this |
|
74 | + */ |
|
75 | + public function setCode(callable $code): self |
|
76 | + { |
|
77 | + $this->getCommand()->setCode($code); |
|
78 | + |
|
79 | + return $this; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @internal |
|
84 | + */ |
|
85 | + public function mergeApplicationDefinition(bool $mergeArgs = true): void |
|
86 | + { |
|
87 | + $this->getCommand()->mergeApplicationDefinition($mergeArgs); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @return $this |
|
92 | + */ |
|
93 | + public function setDefinition($definition): self |
|
94 | + { |
|
95 | + $this->getCommand()->setDefinition($definition); |
|
96 | + |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + public function getDefinition(): InputDefinition |
|
101 | + { |
|
102 | + return $this->getCommand()->getDefinition(); |
|
103 | + } |
|
104 | + |
|
105 | + public function getNativeDefinition(): InputDefinition |
|
106 | + { |
|
107 | + return $this->getCommand()->getNativeDefinition(); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @return $this |
|
112 | + */ |
|
113 | + public function addArgument(string $name, int $mode = null, string $description = '', $default = null): self |
|
114 | + { |
|
115 | + $this->getCommand()->addArgument($name, $mode, $description, $default); |
|
116 | + |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return $this |
|
122 | + */ |
|
123 | + public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null): self |
|
124 | + { |
|
125 | + $this->getCommand()->addOption($name, $shortcut, $mode, $description, $default); |
|
126 | + |
|
127 | + return $this; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @return $this |
|
132 | + */ |
|
133 | + public function setProcessTitle(string $title): self |
|
134 | + { |
|
135 | + $this->getCommand()->setProcessTitle($title); |
|
136 | + |
|
137 | + return $this; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @return $this |
|
142 | + */ |
|
143 | + public function setHelp(string $help): self |
|
144 | + { |
|
145 | + $this->getCommand()->setHelp($help); |
|
146 | + |
|
147 | + return $this; |
|
148 | + } |
|
149 | + |
|
150 | + public function getHelp(): string |
|
151 | + { |
|
152 | + return $this->getCommand()->getHelp(); |
|
153 | + } |
|
154 | + |
|
155 | + public function getProcessedHelp(): string |
|
156 | + { |
|
157 | + return $this->getCommand()->getProcessedHelp(); |
|
158 | + } |
|
159 | + |
|
160 | + public function getSynopsis(bool $short = false): string |
|
161 | + { |
|
162 | + return $this->getCommand()->getSynopsis($short); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @return $this |
|
167 | + */ |
|
168 | + public function addUsage(string $usage): self |
|
169 | + { |
|
170 | + $this->getCommand()->addUsage($usage); |
|
171 | + |
|
172 | + return $this; |
|
173 | + } |
|
174 | + |
|
175 | + public function getUsages(): array |
|
176 | + { |
|
177 | + return $this->getCommand()->getUsages(); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return mixed |
|
182 | + */ |
|
183 | + public function getHelper(string $name) |
|
184 | + { |
|
185 | + return $this->getCommand()->getHelper($name); |
|
186 | + } |
|
187 | + |
|
188 | + public function getCommand(): parent |
|
189 | + { |
|
190 | + if (!$this->command instanceof \Closure) { |
|
191 | + return $this->command; |
|
192 | + } |
|
193 | + |
|
194 | + $command = $this->command = ($this->command)(); |
|
195 | + $command->setApplication($this->getApplication()); |
|
196 | + |
|
197 | + if (null !== $this->getHelperSet()) { |
|
198 | + $command->setHelperSet($this->getHelperSet()); |
|
199 | + } |
|
200 | + |
|
201 | + $command->setName($this->getName()) |
|
202 | + ->setAliases($this->getAliases()) |
|
203 | + ->setHidden($this->isHidden()) |
|
204 | + ->setDescription($this->getDescription()); |
|
205 | + |
|
206 | + // Will throw if the command is not correctly initialized. |
|
207 | + $command->getDefinition(); |
|
208 | + |
|
209 | + return $command; |
|
210 | + } |
|
211 | 211 | } |
@@ -24,46 +24,46 @@ |
||
24 | 24 | */ |
25 | 25 | trait LockableTrait |
26 | 26 | { |
27 | - /** @var Lock */ |
|
28 | - private $lock; |
|
27 | + /** @var Lock */ |
|
28 | + private $lock; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Locks a command. |
|
32 | - */ |
|
33 | - private function lock(string $name = null, bool $blocking = false): bool |
|
34 | - { |
|
35 | - if (!class_exists(SemaphoreStore::class)) { |
|
36 | - throw new LogicException('To enable the locking feature you must install the symfony/lock component.'); |
|
37 | - } |
|
30 | + /** |
|
31 | + * Locks a command. |
|
32 | + */ |
|
33 | + private function lock(string $name = null, bool $blocking = false): bool |
|
34 | + { |
|
35 | + if (!class_exists(SemaphoreStore::class)) { |
|
36 | + throw new LogicException('To enable the locking feature you must install the symfony/lock component.'); |
|
37 | + } |
|
38 | 38 | |
39 | - if (null !== $this->lock) { |
|
40 | - throw new LogicException('A lock is already in place.'); |
|
41 | - } |
|
39 | + if (null !== $this->lock) { |
|
40 | + throw new LogicException('A lock is already in place.'); |
|
41 | + } |
|
42 | 42 | |
43 | - if (SemaphoreStore::isSupported()) { |
|
44 | - $store = new SemaphoreStore(); |
|
45 | - } else { |
|
46 | - $store = new FlockStore(); |
|
47 | - } |
|
43 | + if (SemaphoreStore::isSupported()) { |
|
44 | + $store = new SemaphoreStore(); |
|
45 | + } else { |
|
46 | + $store = new FlockStore(); |
|
47 | + } |
|
48 | 48 | |
49 | - $this->lock = (new LockFactory($store))->createLock($name ?: $this->getName()); |
|
50 | - if (!$this->lock->acquire($blocking)) { |
|
51 | - $this->lock = null; |
|
49 | + $this->lock = (new LockFactory($store))->createLock($name ?: $this->getName()); |
|
50 | + if (!$this->lock->acquire($blocking)) { |
|
51 | + $this->lock = null; |
|
52 | 52 | |
53 | - return false; |
|
54 | - } |
|
53 | + return false; |
|
54 | + } |
|
55 | 55 | |
56 | - return true; |
|
57 | - } |
|
56 | + return true; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Releases the command lock if there is one. |
|
61 | - */ |
|
62 | - private function release() |
|
63 | - { |
|
64 | - if ($this->lock) { |
|
65 | - $this->lock->release(); |
|
66 | - $this->lock = null; |
|
67 | - } |
|
68 | - } |
|
59 | + /** |
|
60 | + * Releases the command lock if there is one. |
|
61 | + */ |
|
62 | + private function release() |
|
63 | + { |
|
64 | + if ($this->lock) { |
|
65 | + $this->lock->release(); |
|
66 | + $this->lock = null; |
|
67 | + } |
|
68 | + } |
|
69 | 69 | } |
@@ -30,675 +30,675 @@ |
||
30 | 30 | */ |
31 | 31 | class Command |
32 | 32 | { |
33 | - // see https://tldp.org/LDP/abs/html/exitcodes.html |
|
34 | - public const SUCCESS = 0; |
|
35 | - public const FAILURE = 1; |
|
36 | - public const INVALID = 2; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var string|null The default command name |
|
40 | - */ |
|
41 | - protected static $defaultName; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var string|null The default command description |
|
45 | - */ |
|
46 | - protected static $defaultDescription; |
|
47 | - |
|
48 | - private $application; |
|
49 | - private $name; |
|
50 | - private $processTitle; |
|
51 | - private $aliases = []; |
|
52 | - private $definition; |
|
53 | - private $hidden = false; |
|
54 | - private $help = ''; |
|
55 | - private $description = ''; |
|
56 | - private $fullDefinition; |
|
57 | - private $ignoreValidationErrors = false; |
|
58 | - private $code; |
|
59 | - private $synopsis = []; |
|
60 | - private $usages = []; |
|
61 | - private $helperSet; |
|
62 | - |
|
63 | - /** |
|
64 | - * @return string|null The default command name or null when no default name is set |
|
65 | - */ |
|
66 | - public static function getDefaultName() |
|
67 | - { |
|
68 | - $class = static::class; |
|
69 | - |
|
70 | - if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) { |
|
71 | - return $attribute[0]->newInstance()->name; |
|
72 | - } |
|
73 | - |
|
74 | - $r = new \ReflectionProperty($class, 'defaultName'); |
|
75 | - |
|
76 | - return $class === $r->class ? static::$defaultName : null; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return string|null The default command description or null when no default description is set |
|
81 | - */ |
|
82 | - public static function getDefaultDescription(): ?string |
|
83 | - { |
|
84 | - $class = static::class; |
|
85 | - |
|
86 | - if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) { |
|
87 | - return $attribute[0]->newInstance()->description; |
|
88 | - } |
|
89 | - |
|
90 | - $r = new \ReflectionProperty($class, 'defaultDescription'); |
|
91 | - |
|
92 | - return $class === $r->class ? static::$defaultDescription : null; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param string|null $name The name of the command; passing null means it must be set in configure() |
|
97 | - * |
|
98 | - * @throws LogicException When the command name is empty |
|
99 | - */ |
|
100 | - public function __construct(string $name = null) |
|
101 | - { |
|
102 | - $this->definition = new InputDefinition(); |
|
103 | - |
|
104 | - if (null === $name && null !== $name = static::getDefaultName()) { |
|
105 | - $aliases = explode('|', $name); |
|
106 | - |
|
107 | - if ('' === $name = array_shift($aliases)) { |
|
108 | - $this->setHidden(true); |
|
109 | - $name = array_shift($aliases); |
|
110 | - } |
|
111 | - |
|
112 | - $this->setAliases($aliases); |
|
113 | - } |
|
114 | - |
|
115 | - if (null !== $name) { |
|
116 | - $this->setName($name); |
|
117 | - } |
|
118 | - |
|
119 | - if ('' === $this->description) { |
|
120 | - $this->setDescription(static::getDefaultDescription() ?? ''); |
|
121 | - } |
|
122 | - |
|
123 | - $this->configure(); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Ignores validation errors. |
|
128 | - * |
|
129 | - * This is mainly useful for the help command. |
|
130 | - */ |
|
131 | - public function ignoreValidationErrors() |
|
132 | - { |
|
133 | - $this->ignoreValidationErrors = true; |
|
134 | - } |
|
135 | - |
|
136 | - public function setApplication(Application $application = null) |
|
137 | - { |
|
138 | - $this->application = $application; |
|
139 | - if ($application) { |
|
140 | - $this->setHelperSet($application->getHelperSet()); |
|
141 | - } else { |
|
142 | - $this->helperSet = null; |
|
143 | - } |
|
144 | - |
|
145 | - $this->fullDefinition = null; |
|
146 | - } |
|
147 | - |
|
148 | - public function setHelperSet(HelperSet $helperSet) |
|
149 | - { |
|
150 | - $this->helperSet = $helperSet; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Gets the helper set. |
|
155 | - * |
|
156 | - * @return HelperSet|null A HelperSet instance |
|
157 | - */ |
|
158 | - public function getHelperSet() |
|
159 | - { |
|
160 | - return $this->helperSet; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Gets the application instance for this command. |
|
165 | - * |
|
166 | - * @return Application|null An Application instance |
|
167 | - */ |
|
168 | - public function getApplication() |
|
169 | - { |
|
170 | - return $this->application; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Checks whether the command is enabled or not in the current environment. |
|
175 | - * |
|
176 | - * Override this to check for x or y and return false if the command can not |
|
177 | - * run properly under the current conditions. |
|
178 | - * |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function isEnabled() |
|
182 | - { |
|
183 | - return true; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Configures the current command. |
|
188 | - */ |
|
189 | - protected function configure() |
|
190 | - { |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Executes the current command. |
|
195 | - * |
|
196 | - * This method is not abstract because you can use this class |
|
197 | - * as a concrete class. In this case, instead of defining the |
|
198 | - * execute() method, you set the code to execute by passing |
|
199 | - * a Closure to the setCode() method. |
|
200 | - * |
|
201 | - * @return int 0 if everything went fine, or an exit code |
|
202 | - * |
|
203 | - * @throws LogicException When this abstract method is not implemented |
|
204 | - * |
|
205 | - * @see setCode() |
|
206 | - */ |
|
207 | - protected function execute(InputInterface $input, OutputInterface $output) |
|
208 | - { |
|
209 | - throw new LogicException('You must override the execute() method in the concrete command class.'); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Interacts with the user. |
|
214 | - * |
|
215 | - * This method is executed before the InputDefinition is validated. |
|
216 | - * This means that this is the only place where the command can |
|
217 | - * interactively ask for values of missing required arguments. |
|
218 | - */ |
|
219 | - protected function interact(InputInterface $input, OutputInterface $output) |
|
220 | - { |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Initializes the command after the input has been bound and before the input |
|
225 | - * is validated. |
|
226 | - * |
|
227 | - * This is mainly useful when a lot of commands extends one main command |
|
228 | - * where some things need to be initialized based on the input arguments and options. |
|
229 | - * |
|
230 | - * @see InputInterface::bind() |
|
231 | - * @see InputInterface::validate() |
|
232 | - */ |
|
233 | - protected function initialize(InputInterface $input, OutputInterface $output) |
|
234 | - { |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Runs the command. |
|
239 | - * |
|
240 | - * The code to execute is either defined directly with the |
|
241 | - * setCode() method or by overriding the execute() method |
|
242 | - * in a sub-class. |
|
243 | - * |
|
244 | - * @return int The command exit code |
|
245 | - * |
|
246 | - * @throws \Exception When binding input fails. Bypass this by calling {@link ignoreValidationErrors()}. |
|
247 | - * |
|
248 | - * @see setCode() |
|
249 | - * @see execute() |
|
250 | - */ |
|
251 | - public function run(InputInterface $input, OutputInterface $output) |
|
252 | - { |
|
253 | - // add the application arguments and options |
|
254 | - $this->mergeApplicationDefinition(); |
|
255 | - |
|
256 | - // bind the input against the command specific arguments/options |
|
257 | - try { |
|
258 | - $input->bind($this->getDefinition()); |
|
259 | - } catch (ExceptionInterface $e) { |
|
260 | - if (!$this->ignoreValidationErrors) { |
|
261 | - throw $e; |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - $this->initialize($input, $output); |
|
266 | - |
|
267 | - if (null !== $this->processTitle) { |
|
268 | - if (\function_exists('cli_set_process_title')) { |
|
269 | - if (!@cli_set_process_title($this->processTitle)) { |
|
270 | - if ('Darwin' === \PHP_OS) { |
|
271 | - $output->writeln('<comment>Running "cli_set_process_title" as an unprivileged user is not supported on MacOS.</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE); |
|
272 | - } else { |
|
273 | - cli_set_process_title($this->processTitle); |
|
274 | - } |
|
275 | - } |
|
276 | - } elseif (\function_exists('setproctitle')) { |
|
277 | - setproctitle($this->processTitle); |
|
278 | - } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) { |
|
279 | - $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>'); |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - if ($input->isInteractive()) { |
|
284 | - $this->interact($input, $output); |
|
285 | - } |
|
286 | - |
|
287 | - // The command name argument is often omitted when a command is executed directly with its run() method. |
|
288 | - // It would fail the validation if we didn't make sure the command argument is present, |
|
289 | - // since it's required by the application. |
|
290 | - if ($input->hasArgument('command') && null === $input->getArgument('command')) { |
|
291 | - $input->setArgument('command', $this->getName()); |
|
292 | - } |
|
293 | - |
|
294 | - $input->validate(); |
|
295 | - |
|
296 | - if ($this->code) { |
|
297 | - $statusCode = ($this->code)($input, $output); |
|
298 | - } else { |
|
299 | - $statusCode = $this->execute($input, $output); |
|
300 | - |
|
301 | - if (!\is_int($statusCode)) { |
|
302 | - throw new \TypeError(sprintf('Return value of "%s::execute()" must be of the type int, "%s" returned.', static::class, get_debug_type($statusCode))); |
|
303 | - } |
|
304 | - } |
|
305 | - |
|
306 | - return is_numeric($statusCode) ? (int) $statusCode : 0; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Sets the code to execute when running this command. |
|
311 | - * |
|
312 | - * If this method is used, it overrides the code defined |
|
313 | - * in the execute() method. |
|
314 | - * |
|
315 | - * @param callable $code A callable(InputInterface $input, OutputInterface $output) |
|
316 | - * |
|
317 | - * @return $this |
|
318 | - * |
|
319 | - * @throws InvalidArgumentException |
|
320 | - * |
|
321 | - * @see execute() |
|
322 | - */ |
|
323 | - public function setCode(callable $code) |
|
324 | - { |
|
325 | - if ($code instanceof \Closure) { |
|
326 | - $r = new \ReflectionFunction($code); |
|
327 | - if (null === $r->getClosureThis()) { |
|
328 | - set_error_handler(static function () {}); |
|
329 | - try { |
|
330 | - if ($c = \Closure::bind($code, $this)) { |
|
331 | - $code = $c; |
|
332 | - } |
|
333 | - } finally { |
|
334 | - restore_error_handler(); |
|
335 | - } |
|
336 | - } |
|
337 | - } |
|
338 | - |
|
339 | - $this->code = $code; |
|
340 | - |
|
341 | - return $this; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Merges the application definition with the command definition. |
|
346 | - * |
|
347 | - * This method is not part of public API and should not be used directly. |
|
348 | - * |
|
349 | - * @param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments |
|
350 | - * |
|
351 | - * @internal |
|
352 | - */ |
|
353 | - public function mergeApplicationDefinition(bool $mergeArgs = true) |
|
354 | - { |
|
355 | - if (null === $this->application) { |
|
356 | - return; |
|
357 | - } |
|
358 | - |
|
359 | - $this->fullDefinition = new InputDefinition(); |
|
360 | - $this->fullDefinition->setOptions($this->definition->getOptions()); |
|
361 | - $this->fullDefinition->addOptions($this->application->getDefinition()->getOptions()); |
|
362 | - |
|
363 | - if ($mergeArgs) { |
|
364 | - $this->fullDefinition->setArguments($this->application->getDefinition()->getArguments()); |
|
365 | - $this->fullDefinition->addArguments($this->definition->getArguments()); |
|
366 | - } else { |
|
367 | - $this->fullDefinition->setArguments($this->definition->getArguments()); |
|
368 | - } |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Sets an array of argument and option instances. |
|
373 | - * |
|
374 | - * @param array|InputDefinition $definition An array of argument and option instances or a definition instance |
|
375 | - * |
|
376 | - * @return $this |
|
377 | - */ |
|
378 | - public function setDefinition($definition) |
|
379 | - { |
|
380 | - if ($definition instanceof InputDefinition) { |
|
381 | - $this->definition = $definition; |
|
382 | - } else { |
|
383 | - $this->definition->setDefinition($definition); |
|
384 | - } |
|
385 | - |
|
386 | - $this->fullDefinition = null; |
|
387 | - |
|
388 | - return $this; |
|
389 | - } |
|
390 | - |
|
391 | - /** |
|
392 | - * Gets the InputDefinition attached to this Command. |
|
393 | - * |
|
394 | - * @return InputDefinition An InputDefinition instance |
|
395 | - */ |
|
396 | - public function getDefinition() |
|
397 | - { |
|
398 | - return $this->fullDefinition ?? $this->getNativeDefinition(); |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Gets the InputDefinition to be used to create representations of this Command. |
|
403 | - * |
|
404 | - * Can be overridden to provide the original command representation when it would otherwise |
|
405 | - * be changed by merging with the application InputDefinition. |
|
406 | - * |
|
407 | - * This method is not part of public API and should not be used directly. |
|
408 | - * |
|
409 | - * @return InputDefinition An InputDefinition instance |
|
410 | - */ |
|
411 | - public function getNativeDefinition() |
|
412 | - { |
|
413 | - if (null === $this->definition) { |
|
414 | - throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', static::class)); |
|
415 | - } |
|
416 | - |
|
417 | - return $this->definition; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Adds an argument. |
|
422 | - * |
|
423 | - * @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL |
|
424 | - * @param mixed $default The default value (for InputArgument::OPTIONAL mode only) |
|
425 | - * |
|
426 | - * @throws InvalidArgumentException When argument mode is not valid |
|
427 | - * |
|
428 | - * @return $this |
|
429 | - */ |
|
430 | - public function addArgument(string $name, int $mode = null, string $description = '', $default = null) |
|
431 | - { |
|
432 | - $this->definition->addArgument(new InputArgument($name, $mode, $description, $default)); |
|
433 | - if (null !== $this->fullDefinition) { |
|
434 | - $this->fullDefinition->addArgument(new InputArgument($name, $mode, $description, $default)); |
|
435 | - } |
|
436 | - |
|
437 | - return $this; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Adds an option. |
|
442 | - * |
|
443 | - * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
|
444 | - * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants |
|
445 | - * @param mixed $default The default value (must be null for InputOption::VALUE_NONE) |
|
446 | - * |
|
447 | - * @throws InvalidArgumentException If option mode is invalid or incompatible |
|
448 | - * |
|
449 | - * @return $this |
|
450 | - */ |
|
451 | - public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) |
|
452 | - { |
|
453 | - $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default)); |
|
454 | - if (null !== $this->fullDefinition) { |
|
455 | - $this->fullDefinition->addOption(new InputOption($name, $shortcut, $mode, $description, $default)); |
|
456 | - } |
|
457 | - |
|
458 | - return $this; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Sets the name of the command. |
|
463 | - * |
|
464 | - * This method can set both the namespace and the name if |
|
465 | - * you separate them by a colon (:) |
|
466 | - * |
|
467 | - * $command->setName('foo:bar'); |
|
468 | - * |
|
469 | - * @return $this |
|
470 | - * |
|
471 | - * @throws InvalidArgumentException When the name is invalid |
|
472 | - */ |
|
473 | - public function setName(string $name) |
|
474 | - { |
|
475 | - $this->validateName($name); |
|
476 | - |
|
477 | - $this->name = $name; |
|
478 | - |
|
479 | - return $this; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Sets the process title of the command. |
|
484 | - * |
|
485 | - * This feature should be used only when creating a long process command, |
|
486 | - * like a daemon. |
|
487 | - * |
|
488 | - * @return $this |
|
489 | - */ |
|
490 | - public function setProcessTitle(string $title) |
|
491 | - { |
|
492 | - $this->processTitle = $title; |
|
493 | - |
|
494 | - return $this; |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Returns the command name. |
|
499 | - * |
|
500 | - * @return string|null |
|
501 | - */ |
|
502 | - public function getName() |
|
503 | - { |
|
504 | - return $this->name; |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * @param bool $hidden Whether or not the command should be hidden from the list of commands |
|
509 | - * The default value will be true in Symfony 6.0 |
|
510 | - * |
|
511 | - * @return $this |
|
512 | - * |
|
513 | - * @final since Symfony 5.1 |
|
514 | - */ |
|
515 | - public function setHidden(bool $hidden /*= true*/) |
|
516 | - { |
|
517 | - $this->hidden = $hidden; |
|
518 | - |
|
519 | - return $this; |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * @return bool whether the command should be publicly shown or not |
|
524 | - */ |
|
525 | - public function isHidden() |
|
526 | - { |
|
527 | - return $this->hidden; |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Sets the description for the command. |
|
532 | - * |
|
533 | - * @return $this |
|
534 | - */ |
|
535 | - public function setDescription(string $description) |
|
536 | - { |
|
537 | - $this->description = $description; |
|
538 | - |
|
539 | - return $this; |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * Returns the description for the command. |
|
544 | - * |
|
545 | - * @return string The description for the command |
|
546 | - */ |
|
547 | - public function getDescription() |
|
548 | - { |
|
549 | - return $this->description; |
|
550 | - } |
|
551 | - |
|
552 | - /** |
|
553 | - * Sets the help for the command. |
|
554 | - * |
|
555 | - * @return $this |
|
556 | - */ |
|
557 | - public function setHelp(string $help) |
|
558 | - { |
|
559 | - $this->help = $help; |
|
560 | - |
|
561 | - return $this; |
|
562 | - } |
|
563 | - |
|
564 | - /** |
|
565 | - * Returns the help for the command. |
|
566 | - * |
|
567 | - * @return string The help for the command |
|
568 | - */ |
|
569 | - public function getHelp() |
|
570 | - { |
|
571 | - return $this->help; |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * Returns the processed help for the command replacing the %command.name% and |
|
576 | - * %command.full_name% patterns with the real values dynamically. |
|
577 | - * |
|
578 | - * @return string The processed help for the command |
|
579 | - */ |
|
580 | - public function getProcessedHelp() |
|
581 | - { |
|
582 | - $name = $this->name; |
|
583 | - $isSingleCommand = $this->application && $this->application->isSingleCommand(); |
|
584 | - |
|
585 | - $placeholders = [ |
|
586 | - '%command.name%', |
|
587 | - '%command.full_name%', |
|
588 | - ]; |
|
589 | - $replacements = [ |
|
590 | - $name, |
|
591 | - $isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name, |
|
592 | - ]; |
|
593 | - |
|
594 | - return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription()); |
|
595 | - } |
|
596 | - |
|
597 | - /** |
|
598 | - * Sets the aliases for the command. |
|
599 | - * |
|
600 | - * @param string[] $aliases An array of aliases for the command |
|
601 | - * |
|
602 | - * @return $this |
|
603 | - * |
|
604 | - * @throws InvalidArgumentException When an alias is invalid |
|
605 | - */ |
|
606 | - public function setAliases(iterable $aliases) |
|
607 | - { |
|
608 | - $list = []; |
|
609 | - |
|
610 | - foreach ($aliases as $alias) { |
|
611 | - $this->validateName($alias); |
|
612 | - $list[] = $alias; |
|
613 | - } |
|
614 | - |
|
615 | - $this->aliases = \is_array($aliases) ? $aliases : $list; |
|
616 | - |
|
617 | - return $this; |
|
618 | - } |
|
619 | - |
|
620 | - /** |
|
621 | - * Returns the aliases for the command. |
|
622 | - * |
|
623 | - * @return array An array of aliases for the command |
|
624 | - */ |
|
625 | - public function getAliases() |
|
626 | - { |
|
627 | - return $this->aliases; |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Returns the synopsis for the command. |
|
632 | - * |
|
633 | - * @param bool $short Whether to show the short version of the synopsis (with options folded) or not |
|
634 | - * |
|
635 | - * @return string The synopsis |
|
636 | - */ |
|
637 | - public function getSynopsis(bool $short = false) |
|
638 | - { |
|
639 | - $key = $short ? 'short' : 'long'; |
|
640 | - |
|
641 | - if (!isset($this->synopsis[$key])) { |
|
642 | - $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short))); |
|
643 | - } |
|
644 | - |
|
645 | - return $this->synopsis[$key]; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Add a command usage example, it'll be prefixed with the command name. |
|
650 | - * |
|
651 | - * @return $this |
|
652 | - */ |
|
653 | - public function addUsage(string $usage) |
|
654 | - { |
|
655 | - if (!str_starts_with($usage, $this->name)) { |
|
656 | - $usage = sprintf('%s %s', $this->name, $usage); |
|
657 | - } |
|
658 | - |
|
659 | - $this->usages[] = $usage; |
|
660 | - |
|
661 | - return $this; |
|
662 | - } |
|
663 | - |
|
664 | - /** |
|
665 | - * Returns alternative usages of the command. |
|
666 | - * |
|
667 | - * @return array |
|
668 | - */ |
|
669 | - public function getUsages() |
|
670 | - { |
|
671 | - return $this->usages; |
|
672 | - } |
|
673 | - |
|
674 | - /** |
|
675 | - * Gets a helper instance by name. |
|
676 | - * |
|
677 | - * @return mixed The helper value |
|
678 | - * |
|
679 | - * @throws LogicException if no HelperSet is defined |
|
680 | - * @throws InvalidArgumentException if the helper is not defined |
|
681 | - */ |
|
682 | - public function getHelper(string $name) |
|
683 | - { |
|
684 | - if (null === $this->helperSet) { |
|
685 | - throw new LogicException(sprintf('Cannot retrieve helper "%s" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.', $name)); |
|
686 | - } |
|
687 | - |
|
688 | - return $this->helperSet->get($name); |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * Validates a command name. |
|
693 | - * |
|
694 | - * It must be non-empty and parts can optionally be separated by ":". |
|
695 | - * |
|
696 | - * @throws InvalidArgumentException When the name is invalid |
|
697 | - */ |
|
698 | - private function validateName(string $name) |
|
699 | - { |
|
700 | - if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) { |
|
701 | - throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name)); |
|
702 | - } |
|
703 | - } |
|
33 | + // see https://tldp.org/LDP/abs/html/exitcodes.html |
|
34 | + public const SUCCESS = 0; |
|
35 | + public const FAILURE = 1; |
|
36 | + public const INVALID = 2; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var string|null The default command name |
|
40 | + */ |
|
41 | + protected static $defaultName; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var string|null The default command description |
|
45 | + */ |
|
46 | + protected static $defaultDescription; |
|
47 | + |
|
48 | + private $application; |
|
49 | + private $name; |
|
50 | + private $processTitle; |
|
51 | + private $aliases = []; |
|
52 | + private $definition; |
|
53 | + private $hidden = false; |
|
54 | + private $help = ''; |
|
55 | + private $description = ''; |
|
56 | + private $fullDefinition; |
|
57 | + private $ignoreValidationErrors = false; |
|
58 | + private $code; |
|
59 | + private $synopsis = []; |
|
60 | + private $usages = []; |
|
61 | + private $helperSet; |
|
62 | + |
|
63 | + /** |
|
64 | + * @return string|null The default command name or null when no default name is set |
|
65 | + */ |
|
66 | + public static function getDefaultName() |
|
67 | + { |
|
68 | + $class = static::class; |
|
69 | + |
|
70 | + if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) { |
|
71 | + return $attribute[0]->newInstance()->name; |
|
72 | + } |
|
73 | + |
|
74 | + $r = new \ReflectionProperty($class, 'defaultName'); |
|
75 | + |
|
76 | + return $class === $r->class ? static::$defaultName : null; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return string|null The default command description or null when no default description is set |
|
81 | + */ |
|
82 | + public static function getDefaultDescription(): ?string |
|
83 | + { |
|
84 | + $class = static::class; |
|
85 | + |
|
86 | + if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) { |
|
87 | + return $attribute[0]->newInstance()->description; |
|
88 | + } |
|
89 | + |
|
90 | + $r = new \ReflectionProperty($class, 'defaultDescription'); |
|
91 | + |
|
92 | + return $class === $r->class ? static::$defaultDescription : null; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param string|null $name The name of the command; passing null means it must be set in configure() |
|
97 | + * |
|
98 | + * @throws LogicException When the command name is empty |
|
99 | + */ |
|
100 | + public function __construct(string $name = null) |
|
101 | + { |
|
102 | + $this->definition = new InputDefinition(); |
|
103 | + |
|
104 | + if (null === $name && null !== $name = static::getDefaultName()) { |
|
105 | + $aliases = explode('|', $name); |
|
106 | + |
|
107 | + if ('' === $name = array_shift($aliases)) { |
|
108 | + $this->setHidden(true); |
|
109 | + $name = array_shift($aliases); |
|
110 | + } |
|
111 | + |
|
112 | + $this->setAliases($aliases); |
|
113 | + } |
|
114 | + |
|
115 | + if (null !== $name) { |
|
116 | + $this->setName($name); |
|
117 | + } |
|
118 | + |
|
119 | + if ('' === $this->description) { |
|
120 | + $this->setDescription(static::getDefaultDescription() ?? ''); |
|
121 | + } |
|
122 | + |
|
123 | + $this->configure(); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Ignores validation errors. |
|
128 | + * |
|
129 | + * This is mainly useful for the help command. |
|
130 | + */ |
|
131 | + public function ignoreValidationErrors() |
|
132 | + { |
|
133 | + $this->ignoreValidationErrors = true; |
|
134 | + } |
|
135 | + |
|
136 | + public function setApplication(Application $application = null) |
|
137 | + { |
|
138 | + $this->application = $application; |
|
139 | + if ($application) { |
|
140 | + $this->setHelperSet($application->getHelperSet()); |
|
141 | + } else { |
|
142 | + $this->helperSet = null; |
|
143 | + } |
|
144 | + |
|
145 | + $this->fullDefinition = null; |
|
146 | + } |
|
147 | + |
|
148 | + public function setHelperSet(HelperSet $helperSet) |
|
149 | + { |
|
150 | + $this->helperSet = $helperSet; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Gets the helper set. |
|
155 | + * |
|
156 | + * @return HelperSet|null A HelperSet instance |
|
157 | + */ |
|
158 | + public function getHelperSet() |
|
159 | + { |
|
160 | + return $this->helperSet; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Gets the application instance for this command. |
|
165 | + * |
|
166 | + * @return Application|null An Application instance |
|
167 | + */ |
|
168 | + public function getApplication() |
|
169 | + { |
|
170 | + return $this->application; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Checks whether the command is enabled or not in the current environment. |
|
175 | + * |
|
176 | + * Override this to check for x or y and return false if the command can not |
|
177 | + * run properly under the current conditions. |
|
178 | + * |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function isEnabled() |
|
182 | + { |
|
183 | + return true; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Configures the current command. |
|
188 | + */ |
|
189 | + protected function configure() |
|
190 | + { |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Executes the current command. |
|
195 | + * |
|
196 | + * This method is not abstract because you can use this class |
|
197 | + * as a concrete class. In this case, instead of defining the |
|
198 | + * execute() method, you set the code to execute by passing |
|
199 | + * a Closure to the setCode() method. |
|
200 | + * |
|
201 | + * @return int 0 if everything went fine, or an exit code |
|
202 | + * |
|
203 | + * @throws LogicException When this abstract method is not implemented |
|
204 | + * |
|
205 | + * @see setCode() |
|
206 | + */ |
|
207 | + protected function execute(InputInterface $input, OutputInterface $output) |
|
208 | + { |
|
209 | + throw new LogicException('You must override the execute() method in the concrete command class.'); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Interacts with the user. |
|
214 | + * |
|
215 | + * This method is executed before the InputDefinition is validated. |
|
216 | + * This means that this is the only place where the command can |
|
217 | + * interactively ask for values of missing required arguments. |
|
218 | + */ |
|
219 | + protected function interact(InputInterface $input, OutputInterface $output) |
|
220 | + { |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Initializes the command after the input has been bound and before the input |
|
225 | + * is validated. |
|
226 | + * |
|
227 | + * This is mainly useful when a lot of commands extends one main command |
|
228 | + * where some things need to be initialized based on the input arguments and options. |
|
229 | + * |
|
230 | + * @see InputInterface::bind() |
|
231 | + * @see InputInterface::validate() |
|
232 | + */ |
|
233 | + protected function initialize(InputInterface $input, OutputInterface $output) |
|
234 | + { |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Runs the command. |
|
239 | + * |
|
240 | + * The code to execute is either defined directly with the |
|
241 | + * setCode() method or by overriding the execute() method |
|
242 | + * in a sub-class. |
|
243 | + * |
|
244 | + * @return int The command exit code |
|
245 | + * |
|
246 | + * @throws \Exception When binding input fails. Bypass this by calling {@link ignoreValidationErrors()}. |
|
247 | + * |
|
248 | + * @see setCode() |
|
249 | + * @see execute() |
|
250 | + */ |
|
251 | + public function run(InputInterface $input, OutputInterface $output) |
|
252 | + { |
|
253 | + // add the application arguments and options |
|
254 | + $this->mergeApplicationDefinition(); |
|
255 | + |
|
256 | + // bind the input against the command specific arguments/options |
|
257 | + try { |
|
258 | + $input->bind($this->getDefinition()); |
|
259 | + } catch (ExceptionInterface $e) { |
|
260 | + if (!$this->ignoreValidationErrors) { |
|
261 | + throw $e; |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + $this->initialize($input, $output); |
|
266 | + |
|
267 | + if (null !== $this->processTitle) { |
|
268 | + if (\function_exists('cli_set_process_title')) { |
|
269 | + if (!@cli_set_process_title($this->processTitle)) { |
|
270 | + if ('Darwin' === \PHP_OS) { |
|
271 | + $output->writeln('<comment>Running "cli_set_process_title" as an unprivileged user is not supported on MacOS.</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE); |
|
272 | + } else { |
|
273 | + cli_set_process_title($this->processTitle); |
|
274 | + } |
|
275 | + } |
|
276 | + } elseif (\function_exists('setproctitle')) { |
|
277 | + setproctitle($this->processTitle); |
|
278 | + } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) { |
|
279 | + $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>'); |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + if ($input->isInteractive()) { |
|
284 | + $this->interact($input, $output); |
|
285 | + } |
|
286 | + |
|
287 | + // The command name argument is often omitted when a command is executed directly with its run() method. |
|
288 | + // It would fail the validation if we didn't make sure the command argument is present, |
|
289 | + // since it's required by the application. |
|
290 | + if ($input->hasArgument('command') && null === $input->getArgument('command')) { |
|
291 | + $input->setArgument('command', $this->getName()); |
|
292 | + } |
|
293 | + |
|
294 | + $input->validate(); |
|
295 | + |
|
296 | + if ($this->code) { |
|
297 | + $statusCode = ($this->code)($input, $output); |
|
298 | + } else { |
|
299 | + $statusCode = $this->execute($input, $output); |
|
300 | + |
|
301 | + if (!\is_int($statusCode)) { |
|
302 | + throw new \TypeError(sprintf('Return value of "%s::execute()" must be of the type int, "%s" returned.', static::class, get_debug_type($statusCode))); |
|
303 | + } |
|
304 | + } |
|
305 | + |
|
306 | + return is_numeric($statusCode) ? (int) $statusCode : 0; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Sets the code to execute when running this command. |
|
311 | + * |
|
312 | + * If this method is used, it overrides the code defined |
|
313 | + * in the execute() method. |
|
314 | + * |
|
315 | + * @param callable $code A callable(InputInterface $input, OutputInterface $output) |
|
316 | + * |
|
317 | + * @return $this |
|
318 | + * |
|
319 | + * @throws InvalidArgumentException |
|
320 | + * |
|
321 | + * @see execute() |
|
322 | + */ |
|
323 | + public function setCode(callable $code) |
|
324 | + { |
|
325 | + if ($code instanceof \Closure) { |
|
326 | + $r = new \ReflectionFunction($code); |
|
327 | + if (null === $r->getClosureThis()) { |
|
328 | + set_error_handler(static function () {}); |
|
329 | + try { |
|
330 | + if ($c = \Closure::bind($code, $this)) { |
|
331 | + $code = $c; |
|
332 | + } |
|
333 | + } finally { |
|
334 | + restore_error_handler(); |
|
335 | + } |
|
336 | + } |
|
337 | + } |
|
338 | + |
|
339 | + $this->code = $code; |
|
340 | + |
|
341 | + return $this; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Merges the application definition with the command definition. |
|
346 | + * |
|
347 | + * This method is not part of public API and should not be used directly. |
|
348 | + * |
|
349 | + * @param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments |
|
350 | + * |
|
351 | + * @internal |
|
352 | + */ |
|
353 | + public function mergeApplicationDefinition(bool $mergeArgs = true) |
|
354 | + { |
|
355 | + if (null === $this->application) { |
|
356 | + return; |
|
357 | + } |
|
358 | + |
|
359 | + $this->fullDefinition = new InputDefinition(); |
|
360 | + $this->fullDefinition->setOptions($this->definition->getOptions()); |
|
361 | + $this->fullDefinition->addOptions($this->application->getDefinition()->getOptions()); |
|
362 | + |
|
363 | + if ($mergeArgs) { |
|
364 | + $this->fullDefinition->setArguments($this->application->getDefinition()->getArguments()); |
|
365 | + $this->fullDefinition->addArguments($this->definition->getArguments()); |
|
366 | + } else { |
|
367 | + $this->fullDefinition->setArguments($this->definition->getArguments()); |
|
368 | + } |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Sets an array of argument and option instances. |
|
373 | + * |
|
374 | + * @param array|InputDefinition $definition An array of argument and option instances or a definition instance |
|
375 | + * |
|
376 | + * @return $this |
|
377 | + */ |
|
378 | + public function setDefinition($definition) |
|
379 | + { |
|
380 | + if ($definition instanceof InputDefinition) { |
|
381 | + $this->definition = $definition; |
|
382 | + } else { |
|
383 | + $this->definition->setDefinition($definition); |
|
384 | + } |
|
385 | + |
|
386 | + $this->fullDefinition = null; |
|
387 | + |
|
388 | + return $this; |
|
389 | + } |
|
390 | + |
|
391 | + /** |
|
392 | + * Gets the InputDefinition attached to this Command. |
|
393 | + * |
|
394 | + * @return InputDefinition An InputDefinition instance |
|
395 | + */ |
|
396 | + public function getDefinition() |
|
397 | + { |
|
398 | + return $this->fullDefinition ?? $this->getNativeDefinition(); |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Gets the InputDefinition to be used to create representations of this Command. |
|
403 | + * |
|
404 | + * Can be overridden to provide the original command representation when it would otherwise |
|
405 | + * be changed by merging with the application InputDefinition. |
|
406 | + * |
|
407 | + * This method is not part of public API and should not be used directly. |
|
408 | + * |
|
409 | + * @return InputDefinition An InputDefinition instance |
|
410 | + */ |
|
411 | + public function getNativeDefinition() |
|
412 | + { |
|
413 | + if (null === $this->definition) { |
|
414 | + throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', static::class)); |
|
415 | + } |
|
416 | + |
|
417 | + return $this->definition; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Adds an argument. |
|
422 | + * |
|
423 | + * @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL |
|
424 | + * @param mixed $default The default value (for InputArgument::OPTIONAL mode only) |
|
425 | + * |
|
426 | + * @throws InvalidArgumentException When argument mode is not valid |
|
427 | + * |
|
428 | + * @return $this |
|
429 | + */ |
|
430 | + public function addArgument(string $name, int $mode = null, string $description = '', $default = null) |
|
431 | + { |
|
432 | + $this->definition->addArgument(new InputArgument($name, $mode, $description, $default)); |
|
433 | + if (null !== $this->fullDefinition) { |
|
434 | + $this->fullDefinition->addArgument(new InputArgument($name, $mode, $description, $default)); |
|
435 | + } |
|
436 | + |
|
437 | + return $this; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Adds an option. |
|
442 | + * |
|
443 | + * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
|
444 | + * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants |
|
445 | + * @param mixed $default The default value (must be null for InputOption::VALUE_NONE) |
|
446 | + * |
|
447 | + * @throws InvalidArgumentException If option mode is invalid or incompatible |
|
448 | + * |
|
449 | + * @return $this |
|
450 | + */ |
|
451 | + public function addOption(string $name, $shortcut = null, int $mode = null, string $description = '', $default = null) |
|
452 | + { |
|
453 | + $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default)); |
|
454 | + if (null !== $this->fullDefinition) { |
|
455 | + $this->fullDefinition->addOption(new InputOption($name, $shortcut, $mode, $description, $default)); |
|
456 | + } |
|
457 | + |
|
458 | + return $this; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Sets the name of the command. |
|
463 | + * |
|
464 | + * This method can set both the namespace and the name if |
|
465 | + * you separate them by a colon (:) |
|
466 | + * |
|
467 | + * $command->setName('foo:bar'); |
|
468 | + * |
|
469 | + * @return $this |
|
470 | + * |
|
471 | + * @throws InvalidArgumentException When the name is invalid |
|
472 | + */ |
|
473 | + public function setName(string $name) |
|
474 | + { |
|
475 | + $this->validateName($name); |
|
476 | + |
|
477 | + $this->name = $name; |
|
478 | + |
|
479 | + return $this; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Sets the process title of the command. |
|
484 | + * |
|
485 | + * This feature should be used only when creating a long process command, |
|
486 | + * like a daemon. |
|
487 | + * |
|
488 | + * @return $this |
|
489 | + */ |
|
490 | + public function setProcessTitle(string $title) |
|
491 | + { |
|
492 | + $this->processTitle = $title; |
|
493 | + |
|
494 | + return $this; |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Returns the command name. |
|
499 | + * |
|
500 | + * @return string|null |
|
501 | + */ |
|
502 | + public function getName() |
|
503 | + { |
|
504 | + return $this->name; |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * @param bool $hidden Whether or not the command should be hidden from the list of commands |
|
509 | + * The default value will be true in Symfony 6.0 |
|
510 | + * |
|
511 | + * @return $this |
|
512 | + * |
|
513 | + * @final since Symfony 5.1 |
|
514 | + */ |
|
515 | + public function setHidden(bool $hidden /*= true*/) |
|
516 | + { |
|
517 | + $this->hidden = $hidden; |
|
518 | + |
|
519 | + return $this; |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * @return bool whether the command should be publicly shown or not |
|
524 | + */ |
|
525 | + public function isHidden() |
|
526 | + { |
|
527 | + return $this->hidden; |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Sets the description for the command. |
|
532 | + * |
|
533 | + * @return $this |
|
534 | + */ |
|
535 | + public function setDescription(string $description) |
|
536 | + { |
|
537 | + $this->description = $description; |
|
538 | + |
|
539 | + return $this; |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * Returns the description for the command. |
|
544 | + * |
|
545 | + * @return string The description for the command |
|
546 | + */ |
|
547 | + public function getDescription() |
|
548 | + { |
|
549 | + return $this->description; |
|
550 | + } |
|
551 | + |
|
552 | + /** |
|
553 | + * Sets the help for the command. |
|
554 | + * |
|
555 | + * @return $this |
|
556 | + */ |
|
557 | + public function setHelp(string $help) |
|
558 | + { |
|
559 | + $this->help = $help; |
|
560 | + |
|
561 | + return $this; |
|
562 | + } |
|
563 | + |
|
564 | + /** |
|
565 | + * Returns the help for the command. |
|
566 | + * |
|
567 | + * @return string The help for the command |
|
568 | + */ |
|
569 | + public function getHelp() |
|
570 | + { |
|
571 | + return $this->help; |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * Returns the processed help for the command replacing the %command.name% and |
|
576 | + * %command.full_name% patterns with the real values dynamically. |
|
577 | + * |
|
578 | + * @return string The processed help for the command |
|
579 | + */ |
|
580 | + public function getProcessedHelp() |
|
581 | + { |
|
582 | + $name = $this->name; |
|
583 | + $isSingleCommand = $this->application && $this->application->isSingleCommand(); |
|
584 | + |
|
585 | + $placeholders = [ |
|
586 | + '%command.name%', |
|
587 | + '%command.full_name%', |
|
588 | + ]; |
|
589 | + $replacements = [ |
|
590 | + $name, |
|
591 | + $isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name, |
|
592 | + ]; |
|
593 | + |
|
594 | + return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription()); |
|
595 | + } |
|
596 | + |
|
597 | + /** |
|
598 | + * Sets the aliases for the command. |
|
599 | + * |
|
600 | + * @param string[] $aliases An array of aliases for the command |
|
601 | + * |
|
602 | + * @return $this |
|
603 | + * |
|
604 | + * @throws InvalidArgumentException When an alias is invalid |
|
605 | + */ |
|
606 | + public function setAliases(iterable $aliases) |
|
607 | + { |
|
608 | + $list = []; |
|
609 | + |
|
610 | + foreach ($aliases as $alias) { |
|
611 | + $this->validateName($alias); |
|
612 | + $list[] = $alias; |
|
613 | + } |
|
614 | + |
|
615 | + $this->aliases = \is_array($aliases) ? $aliases : $list; |
|
616 | + |
|
617 | + return $this; |
|
618 | + } |
|
619 | + |
|
620 | + /** |
|
621 | + * Returns the aliases for the command. |
|
622 | + * |
|
623 | + * @return array An array of aliases for the command |
|
624 | + */ |
|
625 | + public function getAliases() |
|
626 | + { |
|
627 | + return $this->aliases; |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Returns the synopsis for the command. |
|
632 | + * |
|
633 | + * @param bool $short Whether to show the short version of the synopsis (with options folded) or not |
|
634 | + * |
|
635 | + * @return string The synopsis |
|
636 | + */ |
|
637 | + public function getSynopsis(bool $short = false) |
|
638 | + { |
|
639 | + $key = $short ? 'short' : 'long'; |
|
640 | + |
|
641 | + if (!isset($this->synopsis[$key])) { |
|
642 | + $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short))); |
|
643 | + } |
|
644 | + |
|
645 | + return $this->synopsis[$key]; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Add a command usage example, it'll be prefixed with the command name. |
|
650 | + * |
|
651 | + * @return $this |
|
652 | + */ |
|
653 | + public function addUsage(string $usage) |
|
654 | + { |
|
655 | + if (!str_starts_with($usage, $this->name)) { |
|
656 | + $usage = sprintf('%s %s', $this->name, $usage); |
|
657 | + } |
|
658 | + |
|
659 | + $this->usages[] = $usage; |
|
660 | + |
|
661 | + return $this; |
|
662 | + } |
|
663 | + |
|
664 | + /** |
|
665 | + * Returns alternative usages of the command. |
|
666 | + * |
|
667 | + * @return array |
|
668 | + */ |
|
669 | + public function getUsages() |
|
670 | + { |
|
671 | + return $this->usages; |
|
672 | + } |
|
673 | + |
|
674 | + /** |
|
675 | + * Gets a helper instance by name. |
|
676 | + * |
|
677 | + * @return mixed The helper value |
|
678 | + * |
|
679 | + * @throws LogicException if no HelperSet is defined |
|
680 | + * @throws InvalidArgumentException if the helper is not defined |
|
681 | + */ |
|
682 | + public function getHelper(string $name) |
|
683 | + { |
|
684 | + if (null === $this->helperSet) { |
|
685 | + throw new LogicException(sprintf('Cannot retrieve helper "%s" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.', $name)); |
|
686 | + } |
|
687 | + |
|
688 | + return $this->helperSet->get($name); |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * Validates a command name. |
|
693 | + * |
|
694 | + * It must be non-empty and parts can optionally be separated by ":". |
|
695 | + * |
|
696 | + * @throws InvalidArgumentException When the name is invalid |
|
697 | + */ |
|
698 | + private function validateName(string $name) |
|
699 | + { |
|
700 | + if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) { |
|
701 | + throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name)); |
|
702 | + } |
|
703 | + } |
|
704 | 704 | } |
@@ -16,30 +16,30 @@ |
||
16 | 16 | */ |
17 | 17 | class BufferedOutput extends Output |
18 | 18 | { |
19 | - private $buffer = ''; |
|
19 | + private $buffer = ''; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Empties buffer and returns its content. |
|
23 | - * |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - public function fetch() |
|
27 | - { |
|
28 | - $content = $this->buffer; |
|
29 | - $this->buffer = ''; |
|
21 | + /** |
|
22 | + * Empties buffer and returns its content. |
|
23 | + * |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + public function fetch() |
|
27 | + { |
|
28 | + $content = $this->buffer; |
|
29 | + $this->buffer = ''; |
|
30 | 30 | |
31 | - return $content; |
|
32 | - } |
|
31 | + return $content; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - protected function doWrite(string $message, bool $newline) |
|
38 | - { |
|
39 | - $this->buffer .= $message; |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + protected function doWrite(string $message, bool $newline) |
|
38 | + { |
|
39 | + $this->buffer .= $message; |
|
40 | 40 | |
41 | - if ($newline) { |
|
42 | - $this->buffer .= \PHP_EOL; |
|
43 | - } |
|
44 | - } |
|
41 | + if ($newline) { |
|
42 | + $this->buffer .= \PHP_EOL; |
|
43 | + } |
|
44 | + } |
|
45 | 45 | } |