1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine PHP |
5
|
|
|
* |
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
7
|
|
|
* PHP Web framework |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine PHP |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file MakeCrudActionCommand.php |
34
|
|
|
* |
35
|
|
|
* The Command to generate new CRUD action class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Framework\Console\Command |
38
|
|
|
* @author Platine Developers team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Framework\Console\Command; |
49
|
|
|
|
50
|
|
|
use Platine\Filesystem\Filesystem; |
51
|
|
|
use Platine\Framework\App\Application; |
52
|
|
|
use Platine\Framework\Console\BaseMakeActionCommand; |
53
|
|
|
use Platine\Stdlib\Helper\Str; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @class MakeCrudActionCommand |
57
|
|
|
* @package Platine\Framework\Console\Command |
58
|
|
|
*/ |
59
|
|
|
class MakeCrudActionCommand extends BaseMakeActionCommand |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected string $type = 'crud'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create new instance |
68
|
|
|
* @param Application $application |
69
|
|
|
* @param Filesystem $filesystem |
70
|
|
|
*/ |
71
|
|
|
public function __construct( |
72
|
|
|
Application $application, |
73
|
|
|
Filesystem $filesystem |
74
|
|
|
) { |
75
|
|
|
parent::__construct($application, $filesystem); |
76
|
|
|
|
77
|
|
|
$this->setName('make:crud') |
78
|
|
|
->setDescription('Command to generate platine CRUD action'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getClassTemplate(): string |
85
|
|
|
{ |
86
|
|
|
return <<<EOF |
87
|
|
|
<?php |
88
|
|
|
|
89
|
|
|
declare(strict_types=1); |
90
|
|
|
|
91
|
|
|
namespace %namespace%; |
92
|
|
|
|
93
|
|
|
use Platine\Framework\Helper\Flash; |
94
|
|
|
use Platine\Framework\Http\Action\CrudAction; |
95
|
|
|
use Platine\Framework\Http\RouteHelper; |
96
|
|
|
use Platine\Lang\Lang; |
97
|
|
|
use Platine\Logger\LoggerInterface; |
98
|
|
|
use Platine\Pagination\Pagination; |
99
|
|
|
use Platine\Template\Template; |
100
|
|
|
%uses% |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @class %classname% |
104
|
|
|
* @package %namespace% |
105
|
|
|
* @extends CrudAction<%base_entity%> |
106
|
|
|
*/ |
107
|
|
|
class %classname% extends CrudAction |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
%attributes% |
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
protected string \$paramClass = %base_param%::class; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
protected string \$validatorClass = %base_validator%::class; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create new instance |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
* @param %base_repository%<%base_entity%> \$repository |
125
|
|
|
*/ |
126
|
|
|
public function __construct( |
127
|
|
|
Lang \$lang, |
128
|
|
|
Pagination \$pagination, |
129
|
|
|
Template \$template, |
130
|
|
|
Flash \$flash, |
131
|
|
|
RouteHelper \$routeHelper, |
132
|
|
|
LoggerInterface \$logger, |
133
|
|
|
%base_repository% \$repository |
134
|
|
|
) { |
135
|
|
|
parent::__construct( |
136
|
|
|
\$lang, |
137
|
|
|
\$pagination, |
138
|
|
|
\$template, |
139
|
|
|
\$flash, |
140
|
|
|
\$routeHelper, |
141
|
|
|
\$logger |
142
|
|
|
); |
143
|
|
|
\$this->repository = \$repository; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
EOF; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
protected function createClass(): string |
154
|
|
|
{ |
155
|
|
|
$content = parent::createClass(); |
156
|
|
|
$entityBaseClass = $this->getClassBaseName($this->entityClass); |
157
|
|
|
$repositoryBaseClass = $this->getClassBaseName($this->repositoryClass); |
158
|
|
|
$paramBaseClass = $this->getClassBaseName($this->paramClass); |
159
|
|
|
$validatorBaseClass = $this->getClassBaseName($this->validatorClass); |
160
|
|
|
|
161
|
|
|
$replace = ''; |
162
|
|
|
$fields = $this->getFieldsPropertyBody(); |
163
|
|
|
if (!empty($fields)) { |
164
|
|
|
$replace .= $fields . PHP_EOL . PHP_EOL; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$orderFields = $this->getOrderByFieldsPropertyBody(); |
168
|
|
|
if (!empty($orderFields)) { |
169
|
|
|
$replace .= $orderFields . PHP_EOL . PHP_EOL; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$uniqueFields = $this->getUniqueFieldsPropertyBody(); |
173
|
|
|
if (!empty($uniqueFields)) { |
174
|
|
|
$replace .= $uniqueFields . PHP_EOL . PHP_EOL; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$templatePrefix = $this->getTemplatePrefix(); |
178
|
|
|
if (!empty($templatePrefix)) { |
179
|
|
|
$replace .= <<<EOF |
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
protected string \$templatePrefix = '$templatePrefix'; |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
EOF; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$routePrefix = $this->getRoutePrefix(); |
190
|
|
|
if (!empty($routePrefix)) { |
191
|
|
|
$replace .= <<<EOF |
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
protected string \$routePrefix = '$routePrefix'; |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
EOF; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$entityContextName = $this->getEntityContextKey(true); |
202
|
|
|
$optionEntityContext = $this->getOptionForArgument('--entity-context-key'); |
203
|
|
|
if ($optionEntityContext !== null && $optionEntityContext->getDefault() !== $entityContextName) { |
204
|
|
|
$replace .= <<<EOF |
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
protected string \$entityContextName = '$entityContextName'; |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
EOF; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$messages = $this->getMessageTemplates(); |
215
|
|
|
if (!empty($messages)) { |
216
|
|
|
$replace .= $messages; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
$contentBaseEntity = str_replace('%base_entity%', $entityBaseClass, $content); |
221
|
|
|
$contentBaseRepository = str_replace('%base_repository%', $repositoryBaseClass, $contentBaseEntity); |
222
|
|
|
$contentBaseParam = str_replace('%base_param%', $paramBaseClass, $contentBaseRepository); |
223
|
|
|
$contentBaseValidator = str_replace('%base_validator%', $validatorBaseClass, $contentBaseParam); |
224
|
|
|
|
225
|
|
|
return str_replace('%attributes%', $replace, $contentBaseValidator); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return the messages template |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
protected function getMessageTemplates(): string |
233
|
|
|
{ |
234
|
|
|
$replace = ''; |
235
|
|
|
|
236
|
|
|
$messages = [ |
237
|
|
|
'create', |
238
|
|
|
'update', |
239
|
|
|
'delete', |
240
|
|
|
'duplicate', |
241
|
|
|
'not-found', |
242
|
|
|
'process-error', |
243
|
|
|
]; |
244
|
|
|
|
245
|
|
|
foreach ($messages as $val) { |
246
|
|
|
$optionName = sprintf('--message-%s', $val); |
247
|
|
|
$optionKey = sprintf('message%s', Str::camel($val, false)); |
248
|
|
|
|
249
|
|
|
$message = $this->getMessage($optionKey); |
250
|
|
|
$option = $this->getOptionForArgument($optionName); |
251
|
|
|
if ($option !== null && addslashes($option->getDefault()) !== $message) { |
|
|
|
|
252
|
|
|
$replace .= <<<EOF |
253
|
|
|
/** |
254
|
|
|
* {@inheritdoc} |
255
|
|
|
*/ |
256
|
|
|
protected string \${$optionKey} = '$message'; |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
EOF; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $replace; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Return the fields property body |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
protected function getFieldsPropertyBody(): string |
271
|
|
|
{ |
272
|
|
|
$fields = $this->getOptionValue('fields'); |
273
|
|
|
if ($fields === null) { |
274
|
|
|
return ''; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
$columns = $this->formatFields($fields); |
278
|
|
|
$str = $this->formatFieldStr($columns); |
279
|
|
|
|
280
|
|
|
$result = <<<EOF |
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
*/ |
284
|
|
|
protected array \$fields = [$str]; |
285
|
|
|
EOF; |
286
|
|
|
|
287
|
|
|
return $result; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Return the order by fields property body |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
protected function getOrderByFieldsPropertyBody(): string |
295
|
|
|
{ |
296
|
|
|
$fields = $this->getOptionValue('fieldsOrder'); |
297
|
|
|
|
298
|
|
|
if ($fields === null) { |
299
|
|
|
return ''; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$columns = $this->formatFields($fields); |
303
|
|
|
$str = $this->formatFieldStr($columns, true); |
304
|
|
|
|
305
|
|
|
$result = <<<EOF |
306
|
|
|
/** |
307
|
|
|
* {@inheritdoc} |
308
|
|
|
*/ |
309
|
|
|
protected array \$orderFields = [$str]; |
310
|
|
|
EOF; |
311
|
|
|
|
312
|
|
|
return $result; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Return the unique fields property body |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
|
|
protected function getUniqueFieldsPropertyBody(): string |
320
|
|
|
{ |
321
|
|
|
$fields = $this->getOptionValue('fieldsUnique'); |
322
|
|
|
|
323
|
|
|
if ($fields === null) { |
324
|
|
|
return ''; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$columns = $this->formatFields($fields); |
328
|
|
|
$str = $this->formatFieldStr($columns); |
329
|
|
|
|
330
|
|
|
$result = <<<EOF |
331
|
|
|
/** |
332
|
|
|
* {@inheritdoc} |
333
|
|
|
*/ |
334
|
|
|
protected array \$uniqueFields = [$str]; |
335
|
|
|
EOF; |
336
|
|
|
|
337
|
|
|
return $result; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* {@inheritdoc} |
342
|
|
|
*/ |
343
|
|
|
protected function getUsesContent(): string |
344
|
|
|
{ |
345
|
|
|
$uses = parent::getUsesContent(); |
346
|
|
|
|
347
|
|
|
$uses .= $this->getUsesTemplate($this->entityClass); |
348
|
|
|
$uses .= $this->getUsesTemplate($this->paramClass); |
349
|
|
|
$uses .= $this->getUsesTemplate($this->validatorClass); |
350
|
|
|
$uses .= $this->getUsesTemplate($this->repositoryClass); |
351
|
|
|
|
352
|
|
|
return <<<EOF |
353
|
|
|
$uses |
354
|
|
|
EOF; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|