|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Venta\Console\Command\SignatureParser; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Command |
|
14
|
|
|
* |
|
15
|
|
|
* @package Venta\Console |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class Command extends BaseCommand |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Input instance passed to handle method |
|
22
|
|
|
* |
|
23
|
|
|
* @var InputInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $input; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Output instance passed to handle method |
|
29
|
|
|
* |
|
30
|
|
|
* @var OutputInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $output; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Helper method to get input argument |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function arg(string $name) |
|
41
|
|
|
{ |
|
42
|
2 |
|
return $this->input->getArgument($name); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function configure() |
|
49
|
|
|
{ |
|
50
|
5 |
|
$signature = (new SignatureParser())->parse($this->signature()); |
|
51
|
|
|
|
|
52
|
5 |
|
$this->setName($signature['name']); |
|
53
|
5 |
|
$this->setDescription($this->description()); |
|
54
|
|
|
|
|
55
|
5 |
|
if (is_array($signature['arguments']) && count($signature['arguments']) > 0) { |
|
56
|
1 |
|
foreach ($signature['arguments'] as $argument) { |
|
57
|
1 |
|
$this->addArgument( |
|
58
|
1 |
|
$argument['name'], |
|
59
|
1 |
|
$argument['type'], |
|
60
|
1 |
|
$argument['description'], |
|
61
|
1 |
|
$argument['default'] |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} else { |
|
65
|
4 |
|
$this->getDefinition()->addArguments($this->returnArguments()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
if (is_array($signature['options']) && count($signature['options']) > 0) { |
|
69
|
1 |
|
foreach ($signature['options'] as $option) { |
|
70
|
1 |
|
$this->addOption($option['name'], null, $option['type'], $option['description'], $option['default']); |
|
71
|
|
|
} |
|
72
|
|
|
} else { |
|
73
|
4 |
|
$this->getDefinition()->addOptions($this->returnOptions()); |
|
74
|
|
|
} |
|
75
|
5 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function description(): string |
|
81
|
|
|
{ |
|
82
|
3 |
|
return ''; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Main command function, which is executed on command run |
|
87
|
|
|
* |
|
88
|
|
|
* @param InputInterface $input |
|
89
|
|
|
* @param OutputInterface $output |
|
90
|
|
|
* @return null|int |
|
91
|
|
|
*/ |
|
92
|
|
|
abstract public function handle(InputInterface $input, OutputInterface $output); |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Helper method to get input option |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $name |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function opt(string $name) |
|
101
|
|
|
{ |
|
102
|
2 |
|
return $this->input->getOption($name); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns command arguments array |
|
107
|
|
|
* Values must be instances of InputArgument |
|
108
|
|
|
* |
|
109
|
|
|
* @return InputArgument[] |
|
110
|
|
|
*/ |
|
111
|
3 |
|
public function returnArguments(): array |
|
112
|
|
|
{ |
|
113
|
3 |
|
return []; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns command options array |
|
118
|
|
|
* Values must be instances of InputOption |
|
119
|
|
|
* |
|
120
|
|
|
* @return InputOption[] |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function returnOptions(): array |
|
123
|
|
|
{ |
|
124
|
3 |
|
return []; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Making method final to restrict overwrite |
|
129
|
|
|
* |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
4 |
|
final public function run(InputInterface $input, OutputInterface $output): int |
|
133
|
|
|
{ |
|
134
|
4 |
|
return parent::run($input, $output); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Should return string with command signature |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
abstract public function signature(): string; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Helper method to write string to output |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $string |
|
148
|
|
|
* @param bool $newline |
|
149
|
|
|
* @param int $options |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
3 |
|
public function write(string $string, bool $newline = false, int $options = 0) |
|
153
|
|
|
{ |
|
154
|
3 |
|
$this->output->write($string, $newline, $options); |
|
155
|
3 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Helper method to write string with new line to output |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $string |
|
161
|
|
|
* @param int $options |
|
162
|
|
|
* @return void |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function writeln(string $string, int $options = 0) |
|
165
|
|
|
{ |
|
166
|
2 |
|
$this->output->writeln($string, $options); |
|
167
|
2 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* {@inheritdoc} |
|
171
|
|
|
*/ |
|
172
|
4 |
|
final protected function execute(InputInterface $input, OutputInterface $output) |
|
173
|
|
|
{ |
|
174
|
4 |
|
$this->input = $input; |
|
175
|
4 |
|
$this->output = $output; |
|
176
|
|
|
|
|
177
|
4 |
|
return $this->handle($input, $output); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
} |