|
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
|
6 |
|
public function run() |
|
97
|
|
|
{ |
|
98
|
6 |
|
if (!$this->overrideFile()) { |
|
99
|
2 |
|
$this->output->writeln("<info>File was skipped. The controller was not created.</info>"); |
|
100
|
2 |
|
return true; |
|
101
|
|
|
} |
|
102
|
4 |
|
$data = $this->getControllerMetaData()->getData(); |
|
103
|
4 |
|
$data = array_merge( |
|
104
|
2 |
|
$data, |
|
105
|
|
|
[ |
|
106
|
4 |
|
'controllerName' => $this->controllerName, |
|
107
|
4 |
|
'namespace' => $this->namespace, |
|
108
|
4 |
|
'basePath' => $this->getBasePath() |
|
109
|
2 |
|
] |
|
110
|
2 |
|
); |
|
111
|
4 |
|
$content = $this->getTemplateEngine() |
|
112
|
4 |
|
->parse($this->template) |
|
113
|
4 |
|
->process($data); |
|
114
|
4 |
|
file_put_contents($this->getControllerFile(), $content); |
|
115
|
4 |
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Gets controllerMetaData property |
|
120
|
|
|
* |
|
121
|
|
|
* @return Controller |
|
122
|
|
|
*/ |
|
123
|
6 |
|
public function getControllerMetaData() |
|
124
|
|
|
{ |
|
125
|
6 |
|
if (null == $this->controllerMetaData) { |
|
126
|
6 |
|
$controller = new Controller(); |
|
127
|
6 |
|
$this->configureController($controller); |
|
128
|
6 |
|
$this->setControllerMetaData($controller); |
|
129
|
3 |
|
} |
|
130
|
6 |
|
return $this->controllerMetaData; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets controllerMetaData property |
|
135
|
|
|
* |
|
136
|
|
|
* @param Controller $controllerMetaData |
|
137
|
|
|
* |
|
138
|
|
|
* @return CreateController |
|
139
|
|
|
*/ |
|
140
|
6 |
|
public function setControllerMetaData(Controller $controllerMetaData) |
|
141
|
|
|
{ |
|
142
|
6 |
|
$this->controllerMetaData = $controllerMetaData; |
|
143
|
6 |
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Gets templateEngine property |
|
148
|
|
|
* |
|
149
|
|
|
* @return TemplateEngineInterface |
|
150
|
|
|
*/ |
|
151
|
6 |
|
public function getTemplateEngine() |
|
152
|
|
|
{ |
|
153
|
6 |
|
if (null == $this->templateEngine) { |
|
154
|
6 |
|
Template::addPath(dirname(dirname(__DIR__)).'/templates'); |
|
155
|
6 |
|
$template = (new Template())->initialize(); |
|
156
|
6 |
|
$this->setTemplateEngine($template); |
|
157
|
3 |
|
} |
|
158
|
6 |
|
return $this->templateEngine; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Sets templateEngine property |
|
163
|
|
|
* |
|
164
|
|
|
* @param TemplateEngineInterface $templateEngine |
|
165
|
|
|
* |
|
166
|
|
|
* @return CreateController |
|
167
|
|
|
*/ |
|
168
|
6 |
|
public function setTemplateEngine(TemplateEngineInterface $templateEngine) |
|
169
|
|
|
{ |
|
170
|
6 |
|
$this->templateEngine = $templateEngine; |
|
171
|
6 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Gets basePath property |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
6 |
|
public function getBasePath() |
|
180
|
|
|
{ |
|
181
|
6 |
|
if (null == $this->basePath) { |
|
182
|
6 |
|
$this->basePath = strtolower($this->controllerName); |
|
183
|
3 |
|
} |
|
184
|
6 |
|
return $this->basePath; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Gets controllerFile property |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
16 |
|
public function getControllerFile() |
|
193
|
|
|
{ |
|
194
|
16 |
|
if (null == $this->controllerFile) { |
|
195
|
16 |
|
$this->setControllerFile( |
|
196
|
16 |
|
"{$this->sourcePath}/{$this->namespace}/" . |
|
197
|
16 |
|
"{$this->controllerName}.php" |
|
198
|
8 |
|
); |
|
199
|
8 |
|
} |
|
200
|
16 |
|
return $this->controllerFile; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Sets controllerFile property |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $controllerFile |
|
207
|
|
|
* |
|
208
|
|
|
* @return CreateController |
|
209
|
|
|
*/ |
|
210
|
16 |
|
public function setControllerFile($controllerFile) |
|
211
|
|
|
{ |
|
212
|
16 |
|
$this->controllerFile = $controllerFile; |
|
213
|
16 |
|
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
|
6 |
|
protected function configureController(Controller $controller) |
|
248
|
|
|
{ |
|
249
|
6 |
|
$controller->setInput($this->getInput()) |
|
250
|
6 |
|
->setOutput($this->getOutput()) |
|
251
|
6 |
|
->setCommand($this->getCommand()); |
|
252
|
6 |
|
$composer = new Composer(); |
|
253
|
6 |
|
$controller->add($composer); |
|
254
|
6 |
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Check controller file existence and ask if can be overridden |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool |
|
260
|
|
|
*/ |
|
261
|
6 |
|
protected function overrideFile() |
|
262
|
|
|
{ |
|
263
|
6 |
|
if (!is_file($this->getControllerFile())) { |
|
264
|
2 |
|
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.