1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Console; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Command\Command as Symfony; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Command |
18
|
|
|
*/ |
19
|
|
|
abstract class Command extends Symfony |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $signature = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var InputInterface |
33
|
|
|
*/ |
34
|
|
|
protected $input; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var OutputInterface |
38
|
|
|
*/ |
39
|
|
|
protected $output; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
43
|
|
|
*/ |
44
|
|
|
public function configure(): void |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
|
|
$parser = new SignatureParser($this->signature); |
48
|
|
|
|
49
|
|
|
$this->setName($parser->getName()); |
50
|
|
|
$this->setDescription($this->description ?: \get_class($this) . ' command'); |
51
|
|
|
|
52
|
|
|
foreach ($parser->getArguments() as $argument) { |
53
|
|
|
$this->getDefinition()->addArgument($argument); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($parser->getOptions() as $option) { |
57
|
|
|
$this->getDefinition()->addOption($option); |
58
|
|
|
} |
59
|
|
|
} catch (\Throwable $e) { |
60
|
|
|
$this->throw($e); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \Throwable $e |
66
|
|
|
* @param OutputInterface|null $output |
67
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
private function throw(\Throwable $e, OutputInterface $output = null): void |
70
|
|
|
{ |
71
|
|
|
$this->getApplication()->throw($e, $output ?? $this->output); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Application|\Symfony\Component\Console\Application |
76
|
|
|
*/ |
77
|
|
|
public function getApplication(): Application |
78
|
|
|
{ |
79
|
|
|
return parent::getApplication(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param InputInterface $input |
84
|
|
|
* @param OutputInterface $output |
85
|
|
|
* @return int|null|void |
86
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
87
|
|
|
* @throws \Throwable |
88
|
|
|
*/ |
89
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
90
|
|
|
{ |
91
|
|
|
$this->input = $input; |
92
|
|
|
$this->output = $output; |
93
|
|
|
|
94
|
|
|
try { |
95
|
|
|
$this->handle(); |
96
|
|
|
} catch (\Throwable $e) { |
97
|
|
|
$this->throw($e, $output); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
abstract public function handle(); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $message |
108
|
|
|
*/ |
109
|
|
|
protected function write(string $message): void |
110
|
|
|
{ |
111
|
|
|
$this->output->write($message); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $message |
116
|
|
|
*/ |
117
|
|
|
protected function writeln(string $message): void |
118
|
|
|
{ |
119
|
|
|
$this->output->writeln($message); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $message |
124
|
|
|
*/ |
125
|
|
|
protected function info(string $message): void |
126
|
|
|
{ |
127
|
|
|
$this->writeln('<info>' . $message . '</info>'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $message |
132
|
|
|
*/ |
133
|
|
|
protected function error(string $message): void |
134
|
|
|
{ |
135
|
|
|
$this->writeln('<error>' . $message . '</error>'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $message |
140
|
|
|
*/ |
141
|
|
|
protected function comment(string $message): void |
142
|
|
|
{ |
143
|
|
|
$this->writeln('<comment>' . $message . '</comment>'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $name |
148
|
|
|
* @return mixed |
149
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
150
|
|
|
*/ |
151
|
|
|
protected function argument(string $name) |
152
|
|
|
{ |
153
|
|
|
return $this->input->getArgument($name); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $name |
158
|
|
|
* @return mixed |
159
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
160
|
|
|
*/ |
161
|
|
|
protected function option(string $name) |
162
|
|
|
{ |
163
|
|
|
return $this->input->getOption($name); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: