|
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 MakeResourceActionCommand.php |
|
34
|
|
|
* |
|
35
|
|
|
* The Command to generate new resource 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\Console\Input\Reader; |
|
51
|
|
|
use Platine\Console\Output\Writer; |
|
52
|
|
|
use Platine\Filesystem\Filesystem; |
|
53
|
|
|
use Platine\Framework\App\Application; |
|
54
|
|
|
use Platine\Framework\Console\BaseMakeActionCommand; |
|
55
|
|
|
use Platine\Framework\Helper\Flash; |
|
56
|
|
|
use Platine\Framework\Http\RouteHelper; |
|
57
|
|
|
use Platine\Lang\Lang; |
|
58
|
|
|
use Platine\Logger\LoggerInterface; |
|
59
|
|
|
use Platine\Pagination\Pagination; |
|
60
|
|
|
use Platine\Stdlib\Helper\Json; |
|
61
|
|
|
use Platine\Stdlib\Helper\Str; |
|
62
|
|
|
use Platine\Template\Template; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @class MakeResourceActionCommand |
|
66
|
|
|
* @package Platine\Framework\Console\Command |
|
67
|
|
|
*/ |
|
68
|
|
|
class MakeResourceActionCommand extends BaseMakeActionCommand |
|
69
|
|
|
{ |
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
protected string $type = 'resource'; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create new instance |
|
77
|
|
|
* @param Application $application |
|
78
|
|
|
* @param Filesystem $filesystem |
|
79
|
|
|
*/ |
|
80
|
|
|
public function __construct( |
|
81
|
|
|
Application $application, |
|
82
|
|
|
Filesystem $filesystem |
|
83
|
|
|
) { |
|
84
|
|
|
parent::__construct($application, $filesystem); |
|
85
|
|
|
$this->setName('make:resource') |
|
86
|
|
|
->setDescription('Command to generate platine resource action'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function interact(Reader $reader, Writer $writer): void |
|
93
|
|
|
{ |
|
94
|
|
|
parent::interact($reader, $writer); |
|
95
|
|
|
|
|
96
|
|
|
$baseClasses = $this->getBaseClasses(); |
|
97
|
|
|
|
|
98
|
|
|
foreach ($baseClasses as $value) { |
|
99
|
|
|
$this->addProperty($value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->recordProperties(); |
|
103
|
|
|
|
|
104
|
|
|
$this->addProperty($this->repositoryClass); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getClassTemplate(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return <<<EOF |
|
113
|
|
|
<?php |
|
114
|
|
|
|
|
115
|
|
|
declare(strict_types=1); |
|
116
|
|
|
|
|
117
|
|
|
namespace %namespace%; |
|
118
|
|
|
|
|
119
|
|
|
use Exception; |
|
120
|
|
|
use Platine\Http\ResponseInterface; |
|
121
|
|
|
use Platine\Http\ServerRequestInterface; |
|
122
|
|
|
use Platine\Framework\Http\RequestData; |
|
123
|
|
|
use Platine\Framework\Http\Response\TemplateResponse; |
|
124
|
|
|
use Platine\Framework\Http\Response\RedirectResponse; |
|
125
|
|
|
%uses% |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @class %classname% |
|
129
|
|
|
* @package %namespace% |
|
130
|
|
|
*/ |
|
131
|
|
|
class %classname% |
|
132
|
|
|
{ |
|
133
|
|
|
|
|
134
|
|
|
%properties% |
|
135
|
|
|
|
|
136
|
|
|
%constructor% |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* List all entities |
|
140
|
|
|
* @param ServerRequestInterface \$request |
|
141
|
|
|
* @return ResponseInterface |
|
142
|
|
|
*/ |
|
143
|
|
|
public function index(ServerRequestInterface \$request): ResponseInterface |
|
144
|
|
|
{ |
|
145
|
|
|
%method_body_index% |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* List entity detail |
|
150
|
|
|
* @param ServerRequestInterface \$request |
|
151
|
|
|
* @return ResponseInterface |
|
152
|
|
|
*/ |
|
153
|
|
|
public function detail(ServerRequestInterface \$request): ResponseInterface |
|
154
|
|
|
{ |
|
155
|
|
|
%method_body_detail% |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Create new entity |
|
160
|
|
|
* @param ServerRequestInterface \$request |
|
161
|
|
|
* @return ResponseInterface |
|
162
|
|
|
*/ |
|
163
|
|
|
public function create(ServerRequestInterface \$request): ResponseInterface |
|
164
|
|
|
{ |
|
165
|
|
|
%method_body_create% |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Update existing entity |
|
170
|
|
|
* @param ServerRequestInterface \$request |
|
171
|
|
|
* @return ResponseInterface |
|
172
|
|
|
*/ |
|
173
|
|
|
public function update(ServerRequestInterface \$request): ResponseInterface |
|
174
|
|
|
{ |
|
175
|
|
|
%method_body_update% |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Delete the entity |
|
180
|
|
|
* @param ServerRequestInterface \$request |
|
181
|
|
|
* @return ResponseInterface |
|
182
|
|
|
*/ |
|
183
|
|
|
public function delete(ServerRequestInterface \$request): ResponseInterface |
|
184
|
|
|
{ |
|
185
|
|
|
%method_body_delete% |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
EOF; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* {@inheritdoc} |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function createClass(): string |
|
196
|
|
|
{ |
|
197
|
|
|
$content = parent::createClass(); |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
$contentIndex = $this->getIndexMethodBody($content); |
|
201
|
|
|
$contentDetail = $this->getDetailMethodBody($contentIndex); |
|
202
|
|
|
$contentCreate = $this->getCreateMethodBody($contentDetail); |
|
203
|
|
|
$contentUpdate = $this->getUpdateMethodBody($contentCreate); |
|
204
|
|
|
$contentDelete = $this->getDeleteMethodBody($contentUpdate); |
|
205
|
|
|
|
|
206
|
|
|
return $contentDelete; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Return the index method body |
|
211
|
|
|
* @param string $content |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function getIndexMethodBody(string $content): string |
|
215
|
|
|
{ |
|
216
|
|
|
$repositoryName = $this->getPropertyName($this->repositoryClass); |
|
217
|
|
|
$templatePrefix = $this->getTemplatePrefix(); |
|
218
|
|
|
|
|
219
|
|
|
$orderByTemplate = $this->getOrderByTemplate(); |
|
220
|
|
|
|
|
221
|
|
|
$result = <<<EOF |
|
222
|
|
|
\$context = []; |
|
223
|
|
|
\$param = new RequestData(\$request); |
|
224
|
|
|
\$totalItems = \$this->{$repositoryName}->query() |
|
225
|
|
|
->count('id'); |
|
226
|
|
|
|
|
227
|
|
|
\$currentPage = (int) \$param->get('page', 1); |
|
228
|
|
|
|
|
229
|
|
|
\$this->pagination->setTotalItems(\$totalItems) |
|
230
|
|
|
->setCurrentPage(\$currentPage); |
|
231
|
|
|
|
|
232
|
|
|
\$limit = \$this->pagination->getItemsPerPage(); |
|
233
|
|
|
\$offset = \$this->pagination->getOffset(); |
|
234
|
|
|
|
|
235
|
|
|
\$results = \$this->{$repositoryName}->query() |
|
236
|
|
|
->offset(\$offset) |
|
237
|
|
|
->limit(\$limit) |
|
238
|
|
|
$orderByTemplate |
|
239
|
|
|
->all(); |
|
240
|
|
|
|
|
241
|
|
|
\$context['list'] = \$results; |
|
242
|
|
|
\$context['pagination'] = \$this->pagination->render(); |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
return new TemplateResponse( |
|
246
|
|
|
\$this->template, |
|
247
|
|
|
'$templatePrefix/list', |
|
248
|
|
|
\$context |
|
249
|
|
|
); |
|
250
|
|
|
EOF; |
|
251
|
|
|
|
|
252
|
|
|
return str_replace('%method_body_index%', $result, $content); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Return the detail method body |
|
257
|
|
|
* @param string $content |
|
258
|
|
|
* @return string |
|
259
|
|
|
*/ |
|
260
|
|
|
protected function getDetailMethodBody(string $content): string |
|
261
|
|
|
{ |
|
262
|
|
|
$repositoryName = $this->getPropertyName($this->repositoryClass); |
|
263
|
|
|
$entityBaseClass = $this->getClassBaseName($this->entityClass); |
|
264
|
|
|
$templatePrefix = $this->getTemplatePrefix(); |
|
265
|
|
|
$notFoundMessage = $this->getMessage('messageNotFound'); |
|
266
|
|
|
|
|
267
|
|
|
$listRoute = $this->getRouteName('list'); |
|
268
|
|
|
|
|
269
|
|
|
$entityContextKey = $this->getEntityContextKey(true); |
|
270
|
|
|
$entityContextName = $this->getEntityContextKey(false); |
|
271
|
|
|
|
|
272
|
|
|
$result = <<<EOF |
|
273
|
|
|
\$context = []; |
|
274
|
|
|
\$id = (int) \$request->getAttribute('id'); |
|
275
|
|
|
|
|
276
|
|
|
/** @var $entityBaseClass|null \$$entityContextName */ |
|
277
|
|
|
\$$entityContextName = \$this->{$repositoryName}->find(\$id); |
|
278
|
|
|
|
|
279
|
|
|
if (\$$entityContextName === null) { |
|
280
|
|
|
\$this->flash->setError(\$this->lang->tr('$notFoundMessage')); |
|
281
|
|
|
|
|
282
|
|
|
return new RedirectResponse( |
|
283
|
|
|
\$this->routeHelper->generateUrl('$listRoute') |
|
284
|
|
|
); |
|
285
|
|
|
} |
|
286
|
|
|
\$context['$entityContextKey'] = \$$entityContextName; |
|
287
|
|
|
|
|
288
|
|
|
return new TemplateResponse( |
|
289
|
|
|
\$this->template, |
|
290
|
|
|
'$templatePrefix/detail', |
|
291
|
|
|
\$context |
|
292
|
|
|
); |
|
293
|
|
|
EOF; |
|
294
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
return str_replace('%method_body_detail%', $result, $content); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Return the create method body |
|
301
|
|
|
* @param string $content |
|
302
|
|
|
* @return string |
|
303
|
|
|
*/ |
|
304
|
|
|
protected function getCreateMethodBody(string $content): string |
|
305
|
|
|
{ |
|
306
|
|
|
$repositoryName = $this->getPropertyName($this->repositoryClass); |
|
307
|
|
|
$formParamBaseClass = $this->getClassBaseName($this->paramClass); |
|
308
|
|
|
$validatorBaseClass = $this->getClassBaseName($this->validatorClass); |
|
309
|
|
|
$entityBaseClass = $this->getClassBaseName($this->entityClass); |
|
310
|
|
|
$templatePrefix = $this->getTemplatePrefix(); |
|
311
|
|
|
$listRoute = $this->getRouteName('list'); |
|
312
|
|
|
$createMessage = $this->getMessage('messageCreate'); |
|
313
|
|
|
$processErrorMessage = $this->getMessage('messageProcessError'); |
|
314
|
|
|
|
|
315
|
|
|
$uniqueCheckStr = $this->getUniqueFieldCheckTemplate(true); |
|
316
|
|
|
$fieldTemplates = $this->getEntityFieldsTemplate(true); |
|
317
|
|
|
|
|
318
|
|
|
$entityContextName = $this->getEntityContextKey(false); |
|
319
|
|
|
|
|
320
|
|
|
$result = <<<EOF |
|
321
|
|
|
\$context = []; |
|
322
|
|
|
\$param = new RequestData(\$request); |
|
323
|
|
|
|
|
324
|
|
|
\$formParam = new $formParamBaseClass(\$param->posts()); |
|
325
|
|
|
\$context['param'] = \$formParam; |
|
326
|
|
|
|
|
327
|
|
|
if (\$request->getMethod() === 'GET') { |
|
328
|
|
|
return new TemplateResponse( |
|
329
|
|
|
\$this->template, |
|
330
|
|
|
'$templatePrefix/create', |
|
331
|
|
|
\$context |
|
332
|
|
|
); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
\$validator = new $validatorBaseClass(\$formParam, \$this->lang); |
|
336
|
|
|
if (\$validator->validate() === false) { |
|
337
|
|
|
\$context['errors'] = \$validator->getErrors(); |
|
338
|
|
|
|
|
339
|
|
|
return new TemplateResponse( |
|
340
|
|
|
\$this->template, |
|
341
|
|
|
'$templatePrefix/create', |
|
342
|
|
|
\$context |
|
343
|
|
|
); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
$uniqueCheckStr |
|
347
|
|
|
|
|
348
|
|
|
/** @var $entityBaseClass \$$entityContextName */ |
|
349
|
|
|
\$$entityContextName = \$this->{$repositoryName}->create([ |
|
350
|
|
|
$fieldTemplates |
|
351
|
|
|
]); |
|
352
|
|
|
|
|
353
|
|
|
try { |
|
354
|
|
|
\$this->{$repositoryName}->save(\$$entityContextName); |
|
355
|
|
|
|
|
356
|
|
|
\$this->flash->setSuccess(\$this->lang->tr('$createMessage')); |
|
357
|
|
|
|
|
358
|
|
|
return new RedirectResponse( |
|
359
|
|
|
\$this->routeHelper->generateUrl('$listRoute') |
|
360
|
|
|
); |
|
361
|
|
|
} catch (Exception \$ex) { |
|
362
|
|
|
\$this->logger->error('Error when saved the data {error}', ['error' => \$ex->getMessage()]); |
|
363
|
|
|
|
|
364
|
|
|
\$this->flash->setError(\$this->lang->tr('$processErrorMessage')); |
|
365
|
|
|
|
|
366
|
|
|
return new TemplateResponse( |
|
367
|
|
|
\$this->template, |
|
368
|
|
|
'$templatePrefix/create', |
|
369
|
|
|
\$context |
|
370
|
|
|
); |
|
371
|
|
|
} |
|
372
|
|
|
EOF; |
|
373
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
return str_replace('%method_body_create%', $result, $content); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Return the update method body |
|
380
|
|
|
* @param string $content |
|
381
|
|
|
* @return string |
|
382
|
|
|
*/ |
|
383
|
|
|
protected function getUpdateMethodBody(string $content): string |
|
384
|
|
|
{ |
|
385
|
|
|
$repositoryName = $this->getPropertyName($this->repositoryClass); |
|
386
|
|
|
$formParamBaseClass = $this->getClassBaseName($this->paramClass); |
|
387
|
|
|
$validatorBaseClass = $this->getClassBaseName($this->validatorClass); |
|
388
|
|
|
$entityBaseClass = $this->getClassBaseName($this->entityClass); |
|
389
|
|
|
$templatePrefix = $this->getTemplatePrefix(); |
|
390
|
|
|
$listRoute = $this->getRouteName('list'); |
|
391
|
|
|
$detailRoute = $this->getRouteName('detail'); |
|
392
|
|
|
$notFoundMessage = $this->getMessage('messageNotFound'); |
|
393
|
|
|
$updateMessage = $this->getMessage('messageUpdate'); |
|
394
|
|
|
$processErrorMessage = $this->getMessage('messageProcessError'); |
|
395
|
|
|
|
|
396
|
|
|
$uniqueCheckStr = $this->getUniqueFieldCheckTemplate(false); |
|
397
|
|
|
$fieldTemplates = $this->getEntityFieldsTemplate(false); |
|
398
|
|
|
|
|
399
|
|
|
$entityContextKey = $this->getEntityContextKey(true); |
|
400
|
|
|
$entityContextName = $this->getEntityContextKey(false); |
|
401
|
|
|
|
|
402
|
|
|
$result = <<<EOF |
|
403
|
|
|
\$context = []; |
|
404
|
|
|
\$param = new RequestData(\$request); |
|
405
|
|
|
|
|
406
|
|
|
\$id = (int) \$request->getAttribute('id'); |
|
407
|
|
|
|
|
408
|
|
|
/** @var $entityBaseClass|null \$$entityContextName */ |
|
409
|
|
|
\$$entityContextName = \$this->{$repositoryName}->find(\$id); |
|
410
|
|
|
|
|
411
|
|
|
if (\$$entityContextName === null) { |
|
412
|
|
|
\$this->flash->setError(\$this->lang->tr('$notFoundMessage')); |
|
413
|
|
|
|
|
414
|
|
|
return new RedirectResponse( |
|
415
|
|
|
\$this->routeHelper->generateUrl('$listRoute') |
|
416
|
|
|
); |
|
417
|
|
|
} |
|
418
|
|
|
\$context['$entityContextKey'] = \$$entityContextName; |
|
419
|
|
|
\$context['param'] = (new $formParamBaseClass())->fromEntity(\$$entityContextName); |
|
420
|
|
|
if (\$request->getMethod() === 'GET') { |
|
421
|
|
|
return new TemplateResponse( |
|
422
|
|
|
\$this->template, |
|
423
|
|
|
'$templatePrefix/update', |
|
424
|
|
|
\$context |
|
425
|
|
|
); |
|
426
|
|
|
} |
|
427
|
|
|
\$formParam = new $formParamBaseClass(\$param->posts()); |
|
428
|
|
|
\$context['param'] = \$formParam; |
|
429
|
|
|
|
|
430
|
|
|
\$validator = new $validatorBaseClass(\$formParam, \$this->lang); |
|
431
|
|
|
if (\$validator->validate() === false) { |
|
432
|
|
|
\$context['errors'] = \$validator->getErrors(); |
|
433
|
|
|
|
|
434
|
|
|
return new TemplateResponse( |
|
435
|
|
|
\$this->template, |
|
436
|
|
|
'$templatePrefix/update', |
|
437
|
|
|
\$context |
|
438
|
|
|
); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
$uniqueCheckStr |
|
442
|
|
|
|
|
443
|
|
|
$fieldTemplates |
|
444
|
|
|
|
|
445
|
|
|
try { |
|
446
|
|
|
\$this->{$repositoryName}->save(\$$entityContextName); |
|
447
|
|
|
|
|
448
|
|
|
\$this->flash->setSuccess(\$this->lang->tr('$updateMessage')); |
|
449
|
|
|
|
|
450
|
|
|
return new RedirectResponse( |
|
451
|
|
|
\$this->routeHelper->generateUrl('$detailRoute', ['id' => \$id]) |
|
452
|
|
|
); |
|
453
|
|
|
} catch (Exception \$ex) { |
|
454
|
|
|
\$this->logger->error('Error when saved the data {error}', ['error' => \$ex->getMessage()]); |
|
455
|
|
|
|
|
456
|
|
|
\$this->flash->setError(\$this->lang->tr('$processErrorMessage')); |
|
457
|
|
|
|
|
458
|
|
|
return new TemplateResponse( |
|
459
|
|
|
\$this->template, |
|
460
|
|
|
'$templatePrefix/update', |
|
461
|
|
|
\$context |
|
462
|
|
|
); |
|
463
|
|
|
} |
|
464
|
|
|
EOF; |
|
465
|
|
|
|
|
466
|
|
|
|
|
467
|
|
|
return str_replace('%method_body_update%', $result, $content); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* Return the delete method body |
|
472
|
|
|
* @param string $content |
|
473
|
|
|
* @return string |
|
474
|
|
|
*/ |
|
475
|
|
|
protected function getDeleteMethodBody(string $content): string |
|
476
|
|
|
{ |
|
477
|
|
|
$repositoryName = $this->getPropertyName($this->repositoryClass); |
|
478
|
|
|
$entityBaseClass = $this->getClassBaseName($this->entityClass); |
|
479
|
|
|
$notFoundMessage = $this->getMessage('messageNotFound'); |
|
480
|
|
|
$deleteMessage = $this->getMessage('messageDelete'); |
|
481
|
|
|
$processErrorMessage = $this->getMessage('messageProcessError'); |
|
482
|
|
|
|
|
483
|
|
|
$listRoute = $this->getRouteName('list'); |
|
484
|
|
|
|
|
485
|
|
|
$entityContextName = $this->getEntityContextKey(false); |
|
486
|
|
|
|
|
487
|
|
|
$result = <<<EOF |
|
488
|
|
|
\$id = (int) \$request->getAttribute('id'); |
|
489
|
|
|
|
|
490
|
|
|
/** @var $entityBaseClass|null \$$entityContextName */ |
|
491
|
|
|
\$$entityContextName = \$this->{$repositoryName}->find(\$id); |
|
492
|
|
|
|
|
493
|
|
|
if (\$$entityContextName === null) { |
|
494
|
|
|
\$this->flash->setError(\$this->lang->tr('$notFoundMessage')); |
|
495
|
|
|
|
|
496
|
|
|
return new RedirectResponse( |
|
497
|
|
|
\$this->routeHelper->generateUrl('$listRoute') |
|
498
|
|
|
); |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
try { |
|
502
|
|
|
\$this->{$repositoryName}->delete(\$$entityContextName); |
|
503
|
|
|
|
|
504
|
|
|
\$this->flash->setSuccess(\$this->lang->tr('$deleteMessage')); |
|
505
|
|
|
|
|
506
|
|
|
return new RedirectResponse( |
|
507
|
|
|
\$this->routeHelper->generateUrl('$listRoute') |
|
508
|
|
|
); |
|
509
|
|
|
} catch (Exception \$ex) { |
|
510
|
|
|
\$this->logger->error('Error when delete the data {error}', ['error' => \$ex->getMessage()]); |
|
511
|
|
|
|
|
512
|
|
|
\$this->flash->setError(\$this->lang->tr('$processErrorMessage')); |
|
513
|
|
|
|
|
514
|
|
|
return new RedirectResponse( |
|
515
|
|
|
\$this->routeHelper->generateUrl('$listRoute') |
|
516
|
|
|
); |
|
517
|
|
|
} |
|
518
|
|
|
EOF; |
|
519
|
|
|
|
|
520
|
|
|
|
|
521
|
|
|
return str_replace('%method_body_delete%', $result, $content); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* Return the template for unique field check |
|
526
|
|
|
* @param bool $create |
|
527
|
|
|
* @return string |
|
528
|
|
|
*/ |
|
529
|
|
|
protected function getUniqueFieldCheckTemplate(bool $create = true): string |
|
530
|
|
|
{ |
|
531
|
|
|
$repositoryName = $this->getPropertyName($this->repositoryClass); |
|
532
|
|
|
$templatePrefix = $this->getTemplatePrefix(); |
|
533
|
|
|
$uniqueFields = $this->getOptionValue('fieldsUnique'); |
|
534
|
|
|
$uniqueCheckStr = ''; |
|
535
|
|
|
if ($uniqueFields !== null) { |
|
536
|
|
|
$duplicateMessage = $this->getMessage('messageDuplicate'); |
|
537
|
|
|
|
|
538
|
|
|
$fields = (array) explode(',', $uniqueFields); |
|
539
|
|
|
$i = 1; |
|
540
|
|
|
$result = ''; |
|
541
|
|
|
foreach ($fields as $field) { |
|
542
|
|
|
$column = $field; |
|
543
|
|
|
$param = $field; |
|
544
|
|
|
$uniqueField = (array) explode(':', $field); |
|
545
|
|
|
if (isset($uniqueField[0])) { |
|
546
|
|
|
$column = $uniqueField[0]; |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
if (isset($uniqueField[1])) { |
|
550
|
|
|
$param = $uniqueField[1]; |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
|
|
$result .= ($i > 1 ? "\t\t\t\t\t " : '') . |
|
554
|
|
|
$this->getFormParamEntityFieldTemplate($column, $param, count($fields) > $i); |
|
555
|
|
|
$i++; |
|
556
|
|
|
} |
|
557
|
|
|
|
|
558
|
|
|
$updateStr = $create ? '' : ' && $entityExist->id !== $id'; |
|
559
|
|
|
$templateName = $create ? 'create' : 'update'; |
|
560
|
|
|
|
|
561
|
|
|
$uniqueCheckStr = <<<EOF |
|
562
|
|
|
\$entityExist = \$this->{$repositoryName}->findBy([ |
|
563
|
|
|
$result |
|
564
|
|
|
]); |
|
565
|
|
|
|
|
566
|
|
|
if(\$entityExist !== null$updateStr){ |
|
567
|
|
|
\$this->flash->setError(\$this->lang->tr('$duplicateMessage')); |
|
568
|
|
|
|
|
569
|
|
|
return new TemplateResponse( |
|
570
|
|
|
\$this->template, |
|
571
|
|
|
'$templatePrefix/$templateName', |
|
572
|
|
|
\$context |
|
573
|
|
|
); |
|
574
|
|
|
} |
|
575
|
|
|
EOF; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
return $uniqueCheckStr; |
|
579
|
|
|
} |
|
580
|
|
|
|
|
581
|
|
|
/** |
|
582
|
|
|
* Return the template for order by |
|
583
|
|
|
* @return string |
|
584
|
|
|
*/ |
|
585
|
|
|
protected function getOrderByTemplate(): string |
|
586
|
|
|
{ |
|
587
|
|
|
$result = ''; |
|
588
|
|
|
$orderFields = $this->getOptionValue('fieldsOrder'); |
|
589
|
|
|
|
|
590
|
|
|
if ($orderFields !== null) { |
|
591
|
|
|
$fields = (array) explode(',', $orderFields); |
|
592
|
|
|
$i = 1; |
|
593
|
|
|
foreach ($fields as $field) { |
|
594
|
|
|
$column = $field; |
|
595
|
|
|
$dir = 'ASC'; |
|
596
|
|
|
$orderField = (array) explode(':', $field); |
|
597
|
|
|
if (isset($orderField[0])) { |
|
598
|
|
|
$column = $orderField[0]; |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
if (isset($orderField[1]) && in_array(strtolower($orderField[1]), ['asc', 'desc'])) { |
|
602
|
|
|
$dir = $orderField[1]; |
|
603
|
|
|
} |
|
604
|
|
|
|
|
605
|
|
|
$result .= ($i > 1 ? "\t\t\t\t\t " : '') . |
|
606
|
|
|
sprintf('->orderBy(\'%s\', \'%s\')', $column, Str::upper($dir)) . |
|
607
|
|
|
(count($fields) > $i ? PHP_EOL : ''); |
|
608
|
|
|
$i++; |
|
609
|
|
|
} |
|
610
|
|
|
} |
|
611
|
|
|
|
|
612
|
|
|
return $result; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
/** |
|
616
|
|
|
* Return the template for entity field for saving |
|
617
|
|
|
* @param bool $create |
|
618
|
|
|
* @return string |
|
619
|
|
|
*/ |
|
620
|
|
|
protected function getEntityFieldsTemplate(bool $create = true): string |
|
621
|
|
|
{ |
|
622
|
|
|
$fields = $this->getOptionValue('fields'); |
|
623
|
|
|
$result = ''; |
|
624
|
|
|
if ($fields !== null) { |
|
625
|
|
|
$fields = (array) explode(',', $fields); |
|
626
|
|
|
$i = 1; |
|
627
|
|
|
|
|
628
|
|
|
foreach ($fields as $field) { |
|
629
|
|
|
$column = $field; |
|
630
|
|
|
$param = $field; |
|
631
|
|
|
$entityField = (array) explode(':', $field); |
|
632
|
|
|
if (isset($entityField[0])) { |
|
633
|
|
|
$column = $entityField[0]; |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
if (isset($entityField[1])) { |
|
637
|
|
|
$param = $entityField[1]; |
|
638
|
|
|
} |
|
639
|
|
|
|
|
640
|
|
|
$result .= ($i > 1 ? "\t " : '') . |
|
641
|
|
|
$this->getEntityRecordFieldTemplate($column, $param, count($fields) > $i, $create); |
|
642
|
|
|
$i++; |
|
643
|
|
|
} |
|
644
|
|
|
} |
|
645
|
|
|
|
|
646
|
|
|
return $result; |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* {@inheritdoc} |
|
651
|
|
|
*/ |
|
652
|
|
|
protected function getUsesContent(): string |
|
653
|
|
|
{ |
|
654
|
|
|
$uses = parent::getUsesContent(); |
|
655
|
|
|
|
|
656
|
|
|
$uses .= $this->getUsesTemplate($this->entityClass); |
|
657
|
|
|
$uses .= $this->getUsesTemplate($this->paramClass); |
|
658
|
|
|
$uses .= $this->getUsesTemplate($this->validatorClass); |
|
659
|
|
|
|
|
660
|
|
|
return <<<EOF |
|
661
|
|
|
$uses |
|
662
|
|
|
EOF; |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
/** |
|
666
|
|
|
* Return the base classes |
|
667
|
|
|
* @return array<class-string> |
|
|
|
|
|
|
668
|
|
|
*/ |
|
669
|
|
|
protected function getBaseClasses(): array |
|
670
|
|
|
{ |
|
671
|
|
|
return [ |
|
672
|
|
|
Lang::class, |
|
673
|
|
|
Pagination::class, |
|
674
|
|
|
Template::class, |
|
675
|
|
|
Flash::class, |
|
676
|
|
|
RouteHelper::class, |
|
677
|
|
|
LoggerInterface::class, |
|
678
|
|
|
]; |
|
679
|
|
|
} |
|
680
|
|
|
|
|
681
|
|
|
/** |
|
682
|
|
|
* Return the template for entity record fields |
|
683
|
|
|
* @param string $field |
|
684
|
|
|
* @param string $param |
|
685
|
|
|
* @param bool $isLast |
|
686
|
|
|
* @param bool $create |
|
687
|
|
|
* @return string |
|
688
|
|
|
*/ |
|
689
|
|
|
protected function getEntityRecordFieldTemplate( |
|
690
|
|
|
string $field, |
|
691
|
|
|
string $param, |
|
692
|
|
|
bool $isLast = false, |
|
693
|
|
|
$create = true |
|
694
|
|
|
): string { |
|
695
|
|
|
$fieldMethodName = $this->getFormParamMethodName($param); |
|
696
|
|
|
if ($create) { |
|
697
|
|
|
return sprintf('\'%s\' => $formParam->%s(),', $field, $fieldMethodName) . ($isLast ? PHP_EOL : ''); |
|
698
|
|
|
} |
|
699
|
|
|
$entityContextName = $this->getEntityContextKey(false); |
|
700
|
|
|
return sprintf( |
|
701
|
|
|
'$%s->%s = $formParam->%s();', |
|
702
|
|
|
$entityContextName, |
|
703
|
|
|
$field, |
|
704
|
|
|
$fieldMethodName |
|
705
|
|
|
) . ($isLast ? PHP_EOL : ''); |
|
706
|
|
|
} |
|
707
|
|
|
} |
|
708
|
|
|
|