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\Validator\Validator as v; |
10
|
|
|
use Bluzman\Application\Application; |
11
|
|
|
use Bluzman\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AbstractCommand |
19
|
|
|
* @package Bluzman\Command |
20
|
|
|
* |
21
|
|
|
* @method Application getApplication() |
22
|
|
|
* |
23
|
|
|
* @author Pavel Machekhin |
24
|
|
|
* @created 2013-11-28 15:47 |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractCommand extends Console\Command\Command |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var InputInterface |
30
|
|
|
*/ |
31
|
|
|
protected $input; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var OutputInterface |
35
|
|
|
*/ |
36
|
|
|
protected $output; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Filesystem |
40
|
|
|
*/ |
41
|
|
|
protected $fs; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param InputInterface $input |
45
|
|
|
*/ |
46
|
10 |
|
public function setInput(InputInterface $input) |
47
|
|
|
{ |
48
|
10 |
|
$this->input = $input; |
49
|
10 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return InputInterface |
53
|
|
|
*/ |
54
|
|
|
public function getInput() |
55
|
|
|
{ |
56
|
|
|
return $this->input; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param OutputInterface $output |
61
|
|
|
*/ |
62
|
10 |
|
public function setOutput(OutputInterface $output) |
63
|
|
|
{ |
64
|
10 |
|
$this->output = $output; |
65
|
10 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return OutputInterface |
69
|
|
|
*/ |
70
|
10 |
|
public function getOutput() |
71
|
|
|
{ |
72
|
10 |
|
return $this->output; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \Symfony\Component\Filesystem\Filesystem $fs |
77
|
|
|
*/ |
78
|
|
|
public function setFs($fs) |
79
|
|
|
{ |
80
|
|
|
$this->fs = $fs; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \Symfony\Component\Filesystem\Filesystem |
85
|
|
|
*/ |
86
|
6 |
|
public function getFs() |
87
|
|
|
{ |
88
|
6 |
|
if (!$this->fs) { |
89
|
6 |
|
$this->fs = new Filesystem(); |
90
|
|
|
} |
91
|
6 |
|
return $this->fs; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param InputInterface $input |
96
|
|
|
* @param OutputInterface $output |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
10 |
|
final public function initialize(InputInterface $input, OutputInterface $output) |
100
|
|
|
{ |
101
|
10 |
|
parent::initialize($input, $output); |
102
|
|
|
|
103
|
10 |
|
$this->setInput($input); |
104
|
10 |
|
$this->setOutput($output); |
105
|
|
|
|
106
|
10 |
|
putenv('BLUZ_ENV=' . $input->getOption('env')); |
107
|
10 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $message |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
10 |
|
public function write($message) |
114
|
|
|
{ |
115
|
10 |
|
$this->getOutput()->writeln($message); |
116
|
10 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $message |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function info($message) |
123
|
|
|
{ |
124
|
|
|
$this->write("<info>$message</info>"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $message |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
public function comment($message) |
132
|
|
|
{ |
133
|
|
|
$this->write("<comment>$message</comment>"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $message |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public function question($message) |
141
|
|
|
{ |
142
|
|
|
$this->write("<question>$message</question>:"); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $message |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
4 |
|
public function error($message) |
150
|
|
|
{ |
151
|
4 |
|
$this->write("<error>$message</error>"); |
152
|
4 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @internal param $output |
156
|
|
|
*/ |
157
|
|
|
public function callForContribute() |
158
|
|
|
{ |
159
|
|
|
$this->info( |
160
|
|
|
' This command is not implemented yet. Don\'t be indifferent - you can contribute!' . |
161
|
|
|
' https://github.com/bluzphp/bluzman. ' |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* addModuleArgument |
167
|
|
|
* |
168
|
|
|
* @param int $required |
169
|
|
|
* @return AbstractCommand |
170
|
|
|
*/ |
171
|
14 |
View Code Duplication |
protected function addModuleArgument($required = InputArgument::REQUIRED) |
|
|
|
|
172
|
|
|
{ |
173
|
14 |
|
$module = new InputArgument('module', $required, 'Module name is required'); |
174
|
14 |
|
$module->setValidator( |
175
|
14 |
|
v::string()->alphaNumeric('-_')->noWhitespace() |
176
|
|
|
); |
177
|
|
|
|
178
|
14 |
|
$this->getDefinition()->addArgument($module); |
179
|
|
|
|
180
|
14 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* addControllerArgument |
185
|
|
|
* |
186
|
|
|
* @param int $required |
187
|
|
|
* @return AbstractCommand |
188
|
|
|
*/ |
189
|
14 |
View Code Duplication |
protected function addControllerArgument($required = InputArgument::REQUIRED) |
|
|
|
|
190
|
|
|
{ |
191
|
14 |
|
$controller = new InputArgument( |
192
|
14 |
|
'controller', |
193
|
|
|
$required, |
194
|
14 |
|
'Controller name is required' |
195
|
|
|
); |
196
|
14 |
|
$controller->setValidator( |
197
|
14 |
|
v::string()->alphaNumeric('-_')->noWhitespace() |
198
|
|
|
); |
199
|
|
|
|
200
|
14 |
|
$this->getDefinition()->addArgument($controller); |
201
|
|
|
|
202
|
14 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
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.