1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Console\Command\Task; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
13
|
|
|
use Slick\Mvc\Console\Command\TaskInterface; |
14
|
|
|
use Slick\Mvc\Console\MetaDataGenerator\Composer; |
15
|
|
|
use Slick\Mvc\Console\MetaDataGenerator\ConsoleAwareMethods; |
16
|
|
|
use Slick\Mvc\Console\MetaDataGenerator\Controller; |
17
|
|
|
use Slick\Template\Template; |
18
|
|
|
use Slick\Template\TemplateEngineInterface; |
19
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
20
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class CreateController |
24
|
|
|
* |
25
|
|
|
* @package Slick\Mvc\Console\Command\Task |
26
|
|
|
* @author Filipe Silva <[email protected]> |
27
|
|
|
* |
28
|
|
|
* @property string $controllerName |
29
|
|
|
* @property string $namespace |
30
|
|
|
* @property string $sourcePath |
31
|
|
|
* @property string $basePath |
32
|
|
|
*/ |
33
|
|
|
class CreateController extends Base implements TaskInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @readwrite |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $entityName; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @readwrite |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $controllerName; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @readwrite |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $namespace; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @readwrite |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $sourcePath; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $basePath; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $controllerFile; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* For input/output getters and setters |
71
|
|
|
*/ |
72
|
|
|
use ConsoleAwareMethods; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var Controller |
76
|
|
|
*/ |
77
|
|
|
protected $controllerMetaData; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var TemplateEngineInterface |
81
|
|
|
*/ |
82
|
|
|
protected $templateEngine; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var QuestionHelper |
86
|
|
|
*/ |
87
|
|
|
protected $questionHelper; |
88
|
|
|
|
89
|
|
|
protected $template = 'controller.twig'; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Runs this task |
93
|
|
|
* |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
8 |
|
public function run() |
97
|
|
|
{ |
98
|
8 |
|
if (!$this->overrideFile()) { |
99
|
2 |
|
$this->output->writeln("<info>File was skipped. The controller was not created.</info>"); |
100
|
2 |
|
return true; |
101
|
|
|
} |
102
|
6 |
|
$data = $this->getControllerMetaData()->getData(); |
103
|
6 |
|
$data = array_merge( |
104
|
3 |
|
$data, |
105
|
|
|
[ |
106
|
6 |
|
'controllerName' => $this->controllerName, |
107
|
6 |
|
'namespace' => $this->namespace, |
108
|
6 |
|
'basePath' => $this->getBasePath() |
109
|
3 |
|
] |
110
|
3 |
|
); |
111
|
6 |
|
$content = $this->getTemplateEngine() |
112
|
6 |
|
->parse($this->template) |
113
|
6 |
|
->process($data); |
114
|
6 |
|
file_put_contents($this->getControllerFile(), $content); |
115
|
6 |
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Gets controllerMetaData property |
120
|
|
|
* |
121
|
|
|
* @return Controller |
122
|
|
|
*/ |
123
|
8 |
|
public function getControllerMetaData() |
124
|
|
|
{ |
125
|
8 |
|
if (null == $this->controllerMetaData) { |
126
|
8 |
|
$controller = new Controller(); |
127
|
8 |
|
$this->configureController($controller); |
128
|
8 |
|
$this->setControllerMetaData($controller); |
129
|
4 |
|
} |
130
|
8 |
|
return $this->controllerMetaData; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets controllerMetaData property |
135
|
|
|
* |
136
|
|
|
* @param Controller $controllerMetaData |
137
|
|
|
* |
138
|
|
|
* @return CreateController |
139
|
|
|
*/ |
140
|
8 |
|
public function setControllerMetaData(Controller $controllerMetaData) |
141
|
|
|
{ |
142
|
8 |
|
$this->controllerMetaData = $controllerMetaData; |
143
|
8 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Gets templateEngine property |
148
|
|
|
* |
149
|
|
|
* @return TemplateEngineInterface |
150
|
|
|
*/ |
151
|
8 |
|
public function getTemplateEngine() |
152
|
|
|
{ |
153
|
8 |
|
if (null == $this->templateEngine) { |
154
|
8 |
|
Template::addPath(dirname(dirname(__DIR__)).'/templates'); |
155
|
8 |
|
$template = (new Template())->initialize(); |
156
|
8 |
|
$this->setTemplateEngine($template); |
157
|
4 |
|
} |
158
|
8 |
|
return $this->templateEngine; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sets templateEngine property |
163
|
|
|
* |
164
|
|
|
* @param TemplateEngineInterface $templateEngine |
165
|
|
|
* |
166
|
|
|
* @return CreateController |
167
|
|
|
*/ |
168
|
8 |
|
public function setTemplateEngine(TemplateEngineInterface $templateEngine) |
169
|
|
|
{ |
170
|
8 |
|
$this->templateEngine = $templateEngine; |
171
|
8 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Gets basePath property |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
8 |
|
public function getBasePath() |
180
|
|
|
{ |
181
|
8 |
|
if (null == $this->basePath) { |
182
|
8 |
|
$this->basePath = strtolower($this->controllerName); |
183
|
4 |
|
} |
184
|
8 |
|
return $this->basePath; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Gets controllerFile property |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
20 |
|
public function getControllerFile() |
193
|
|
|
{ |
194
|
20 |
|
if (null == $this->controllerFile) { |
195
|
20 |
|
$this->setControllerFile( |
196
|
20 |
|
"{$this->sourcePath}/{$this->namespace}/" . |
197
|
20 |
|
"{$this->controllerName}.php" |
198
|
10 |
|
); |
199
|
10 |
|
} |
200
|
20 |
|
return $this->controllerFile; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets controllerFile property |
205
|
|
|
* |
206
|
|
|
* @param string $controllerFile |
207
|
|
|
* |
208
|
|
|
* @return CreateController |
209
|
|
|
*/ |
210
|
20 |
|
public function setControllerFile($controllerFile) |
211
|
|
|
{ |
212
|
20 |
|
$this->controllerFile = $controllerFile; |
213
|
20 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Gets questionHelper property |
218
|
|
|
* |
219
|
|
|
* @return QuestionHelper |
220
|
|
|
*/ |
221
|
6 |
|
public function getQuestionHelper() |
222
|
|
|
{ |
223
|
6 |
|
if (null == $this->questionHelper) { |
224
|
6 |
|
$this->setQuestionHelper($this->getCommand()->getHelper('question')); |
|
|
|
|
225
|
3 |
|
} |
226
|
6 |
|
return $this->questionHelper; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Sets questionHelper property |
231
|
|
|
* |
232
|
|
|
* @param QuestionHelper $questionHelper |
233
|
|
|
* |
234
|
|
|
* @return CreateController |
235
|
|
|
*/ |
236
|
6 |
|
public function setQuestionHelper(QuestionHelper $questionHelper) |
237
|
|
|
{ |
238
|
6 |
|
$this->questionHelper = $questionHelper; |
239
|
6 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Configures the controller generator |
244
|
|
|
* |
245
|
|
|
* @param Controller $controller |
246
|
|
|
*/ |
247
|
8 |
|
protected function configureController(Controller $controller) |
248
|
|
|
{ |
249
|
8 |
|
$controller->setInput($this->getInput()) |
250
|
8 |
|
->setOutput($this->getOutput()) |
251
|
8 |
|
->setCommand($this->getCommand()); |
252
|
8 |
|
$composer = new Composer(); |
253
|
8 |
|
$controller->add($composer); |
254
|
8 |
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Check controller file existence and ask if can be overridden |
258
|
|
|
* |
259
|
|
|
* @return bool |
260
|
|
|
*/ |
261
|
8 |
|
protected function overrideFile() |
262
|
|
|
{ |
263
|
8 |
|
if (!is_file($this->getControllerFile())) { |
264
|
4 |
|
return true; |
265
|
|
|
} |
266
|
|
|
|
267
|
4 |
|
$question = new ConfirmationQuestion( |
268
|
4 |
|
"\nThe file <comment>{$this->controllerFile}</comment> already " . |
269
|
4 |
|
"exists. Override it (y,N)? ", |
270
|
4 |
|
false, |
271
|
2 |
|
'/^(y|yes)/i' |
272
|
2 |
|
); |
273
|
4 |
|
return (boolean) $this->getQuestionHelper()->ask( |
274
|
4 |
|
$this->input, |
275
|
4 |
|
$this->output, |
276
|
|
|
$question |
277
|
2 |
|
); |
278
|
|
|
} |
279
|
|
|
} |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.