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