|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\Generator\Domain\UseCases; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Generator\Domain\Builders\Inflector; |
|
13
|
|
|
use FlexPHP\Generator\Domain\Builders\Template\TemplateBuilder; |
|
14
|
|
|
use FlexPHP\Generator\Domain\Messages\Requests\CreateTemplateFileRequest; |
|
15
|
|
|
use FlexPHP\Generator\Domain\Messages\Responses\CreateTemplateFileResponse; |
|
16
|
|
|
use FlexPHP\Generator\Domain\Writers\TemplateWriter; |
|
17
|
|
|
|
|
18
|
|
|
final class CreateTemplateFileUseCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function execute(CreateTemplateFileRequest $request): CreateTemplateFileResponse |
|
21
|
|
|
{ |
|
22
|
|
|
$files = []; |
|
23
|
|
|
$inflector = new Inflector(); |
|
24
|
|
|
$filename = $inflector->item($request->schema->name()); |
|
25
|
|
|
$actions = [ |
|
26
|
|
|
'index' => 'index.html', |
|
27
|
|
|
'create' => 'new.html', |
|
28
|
|
|
'read' => 'show.html', |
|
29
|
|
|
'update' => 'edit.html', |
|
30
|
|
|
'delete' => '_delete_form.html', |
|
31
|
|
|
'ajax' => '_ajax.html', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
foreach (\array_keys($actions) as $action) { |
|
35
|
|
|
if ($action !== 'ajax' && !$request->schema->hasAction(\substr($action, 0, 1))) { |
|
36
|
|
|
unset($actions[$action]); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (empty($actions['index'])) { |
|
41
|
|
|
unset($actions['ajax']); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$path = \sprintf('%1$s/../../tmp/skeleton/templates/%2$s', __DIR__, $filename); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($actions as $action => $filename) { |
|
47
|
|
|
$builder = new TemplateBuilder($request->schema, $action); |
|
48
|
|
|
$writer = new TemplateWriter($builder->build(), $filename, $path); |
|
49
|
|
|
$files[] = $writer->save(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return new CreateTemplateFileResponse($files); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|