|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/bluzman |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Bluzman\Command; |
|
8
|
|
|
|
|
9
|
|
|
use Bluz\Proxy\Config; |
|
10
|
|
|
use Bluz\Validator\Validator; |
|
11
|
|
|
use Bluzman\Application\Application; |
|
12
|
|
|
use Bluzman\Input\InputArgument; |
|
13
|
|
|
use Bluzman\Input\InputException; |
|
14
|
|
|
use Symfony\Component\Console; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AbstractCommand |
|
21
|
|
|
* @package Bluzman\Command |
|
22
|
|
|
* |
|
23
|
|
|
* @method Application getApplication() |
|
24
|
|
|
* |
|
25
|
|
|
* @author Pavel Machekhin |
|
26
|
|
|
* @created 2013-11-28 15:47 |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class AbstractCommand extends Console\Command\Command |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var InputInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $input; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var OutputInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $output; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Filesystem |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $fs; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param InputInterface $input |
|
47
|
|
|
*/ |
|
48
|
10 |
|
public function setInput(InputInterface $input) : void |
|
49
|
|
|
{ |
|
50
|
10 |
|
$this->input = $input; |
|
51
|
10 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return InputInterface |
|
55
|
|
|
*/ |
|
56
|
10 |
|
public function getInput() |
|
57
|
|
|
{ |
|
58
|
10 |
|
return $this->input; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param OutputInterface $output |
|
63
|
|
|
*/ |
|
64
|
10 |
|
public function setOutput(OutputInterface $output) : void |
|
65
|
|
|
{ |
|
66
|
10 |
|
$this->output = $output; |
|
67
|
10 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return OutputInterface |
|
71
|
|
|
*/ |
|
72
|
10 |
|
public function getOutput() |
|
73
|
|
|
{ |
|
74
|
10 |
|
return $this->output; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param \Symfony\Component\Filesystem\Filesystem $fs |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setFs($fs) : void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->fs = $fs; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return \Symfony\Component\Filesystem\Filesystem |
|
87
|
|
|
*/ |
|
88
|
6 |
|
public function getFs() |
|
89
|
|
|
{ |
|
90
|
6 |
|
if (!$this->fs) { |
|
91
|
6 |
|
$this->fs = new Filesystem(); |
|
92
|
|
|
} |
|
93
|
6 |
|
return $this->fs; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param InputInterface $input |
|
98
|
|
|
* @param OutputInterface $output |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
* @throws \Bluz\Config\ConfigException |
|
102
|
|
|
*/ |
|
103
|
10 |
|
final public function initialize(InputInterface $input, OutputInterface $output) |
|
104
|
|
|
{ |
|
105
|
10 |
|
parent::initialize($input, $output); |
|
106
|
|
|
|
|
107
|
10 |
|
$this->setInput($input); |
|
108
|
10 |
|
$this->setOutput($output); |
|
109
|
|
|
|
|
110
|
10 |
|
putenv('BLUZ_ENV=' . ($input->getOption('env') ?: getenv('BLUZ_ENV'))); |
|
111
|
|
|
|
|
112
|
10 |
|
$loader = new \Bluz\Config\ConfigLoader(); |
|
113
|
10 |
|
$loader->setPath(PATH_APPLICATION); |
|
114
|
10 |
|
$loader->setEnvironment($input->getOption('env')); |
|
115
|
10 |
|
$loader->load(); |
|
116
|
|
|
|
|
117
|
10 |
|
$config = new \Bluz\Config\Config(); |
|
118
|
10 |
|
$config->setFromArray($loader->getConfig()); |
|
119
|
|
|
|
|
120
|
10 |
|
Config::setInstance($config); |
|
121
|
10 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $message |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
10 |
|
public function write($message) : void |
|
128
|
|
|
{ |
|
129
|
10 |
|
$this->getOutput()->writeln($message); |
|
130
|
10 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $message |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
|
|
public function info($message) : void |
|
137
|
|
|
{ |
|
138
|
|
|
$this->write("<info>$message</info>"); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param $message |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
|
public function comment($message) : void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->write("<comment>$message</comment>"); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param $message |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
public function question($message) : void |
|
155
|
|
|
{ |
|
156
|
|
|
$this->write("<question>$message</question>:"); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param $message |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
4 |
|
public function error($message) : void |
|
164
|
|
|
{ |
|
165
|
4 |
|
$this->write("<error>$message</error>"); |
|
166
|
4 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @internal param $output |
|
170
|
|
|
*/ |
|
171
|
|
|
public function callForContribute() |
|
172
|
|
|
{ |
|
173
|
|
|
$this->info( |
|
174
|
|
|
' This command is not implemented yet. Don\'t be indifferent - you can contribute!' . |
|
175
|
|
|
' https://github.com/bluzphp/bluzman. ' |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Add Module Argument |
|
181
|
|
|
* |
|
182
|
|
|
* @param int $required |
|
183
|
|
|
* @return void |
|
184
|
|
|
*/ |
|
185
|
14 |
|
protected function addModuleArgument($required = InputArgument::REQUIRED) : void |
|
186
|
|
|
{ |
|
187
|
14 |
|
$module = new InputArgument('module', $required, 'Module name is required'); |
|
188
|
14 |
|
$this->getDefinition()->addArgument($module); |
|
189
|
14 |
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Validate Module Argument |
|
193
|
|
|
* |
|
194
|
|
|
* @return void |
|
195
|
|
|
* @throws \Bluzman\Input\InputException |
|
196
|
|
|
*/ |
|
197
|
5 |
View Code Duplication |
protected function validateModuleArgument() : void |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
5 |
|
$module = $this->getInput()->getArgument('module'); |
|
200
|
|
|
|
|
201
|
5 |
|
$validator = Validator::create() |
|
202
|
5 |
|
->string() |
|
203
|
5 |
|
->alphaNumeric('-_') |
|
204
|
5 |
|
->noWhitespace(); |
|
205
|
|
|
|
|
206
|
5 |
|
if ($this->getDefinition()->getArgument('module')->isRequired() |
|
207
|
5 |
|
&& !$validator->validate($module)) { |
|
208
|
|
|
throw new InputException($validator->getError()); |
|
209
|
|
|
} |
|
210
|
5 |
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Add Controller Argument |
|
214
|
|
|
* |
|
215
|
|
|
* @param int $required |
|
216
|
|
|
* @return void |
|
217
|
|
|
*/ |
|
218
|
14 |
|
protected function addControllerArgument($required = InputArgument::REQUIRED) : void |
|
219
|
|
|
{ |
|
220
|
14 |
|
$controller = new InputArgument('controller', $required, 'Controller name is required'); |
|
221
|
14 |
|
$this->getDefinition()->addArgument($controller); |
|
222
|
14 |
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Validate Module Argument |
|
226
|
|
|
* |
|
227
|
|
|
* @return void |
|
228
|
|
|
* @throws \Bluzman\Input\InputException |
|
229
|
|
|
*/ |
|
230
|
1 |
View Code Duplication |
protected function validateControllerArgument() : void |
|
|
|
|
|
|
231
|
|
|
{ |
|
232
|
1 |
|
$controller = $this->getInput()->getArgument('controller'); |
|
233
|
|
|
|
|
234
|
1 |
|
$validator = Validator::create() |
|
235
|
1 |
|
->string() |
|
236
|
1 |
|
->alphaNumeric('-_') |
|
237
|
1 |
|
->noWhitespace(); |
|
238
|
|
|
|
|
239
|
1 |
|
if ($this->getDefinition()->getArgument('controller')->isRequired() |
|
240
|
1 |
|
&& !$validator->validate($controller)) { |
|
241
|
|
|
throw new InputException($validator->getError()); |
|
242
|
|
|
} |
|
243
|
1 |
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
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.