|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Generator\Controllers; |
|
5
|
|
|
|
|
6
|
|
|
use Nette\PhpGenerator\ClassType; |
|
7
|
|
|
use Nette\PhpGenerator\Method; |
|
8
|
|
|
use Nette\PhpGenerator\Parameter; |
|
9
|
|
|
use Nette\PhpGenerator\PhpFile; |
|
10
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
|
11
|
|
|
use Nette\PhpGenerator\Property; |
|
12
|
|
|
use Nette\PhpGenerator\PsrPrinter; |
|
13
|
|
|
use SlayerBirden\DFCodeGeneration\Generator\DataProvider\DataProviderInterface; |
|
14
|
|
|
use SlayerBirden\DFCodeGeneration\Generator\GeneratorInterface; |
|
15
|
|
|
use Twig\Environment; |
|
16
|
|
|
use Twig\Loader\FilesystemLoader; |
|
17
|
|
|
|
|
18
|
|
|
final class DeleteGenerator implements GeneratorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DataProviderInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $dataProvider; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Environment |
|
26
|
|
|
*/ |
|
27
|
|
|
private $twig; |
|
28
|
|
|
|
|
29
|
2 |
|
public function __construct(DataProviderInterface $dataProvider) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$this->dataProvider = $dataProvider; |
|
32
|
2 |
|
$loader = new FilesystemLoader(__DIR__ . '/Templates'); |
|
33
|
2 |
|
$this->twig = new Environment($loader); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string |
|
38
|
|
|
* @throws \Twig_Error_Loader |
|
39
|
|
|
* @throws \Twig_Error_Runtime |
|
40
|
|
|
* @throws \Twig_Error_Syntax |
|
41
|
|
|
*/ |
|
42
|
2 |
|
private function getProcessMethodBody(): string |
|
43
|
|
|
{ |
|
44
|
2 |
|
return $this->twig->load('Delete/Process.template.twig')->render($this->dataProvider->provide()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
* @throws \Twig_Error_Loader |
|
50
|
|
|
* @throws \Twig_Error_Runtime |
|
51
|
|
|
* @throws \Twig_Error_Syntax |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function generate(): string |
|
54
|
|
|
{ |
|
55
|
2 |
|
$file = new PhpFile; |
|
56
|
2 |
|
$file->addComment('This file is generated by SlayerBirden\DFCodeGeneration'); |
|
57
|
2 |
|
$file->setStrictTypes(); |
|
58
|
|
|
|
|
59
|
2 |
|
$namespace = $file->addNamespace($this->getNamespace()); |
|
60
|
2 |
|
$this->addUsages($namespace); |
|
61
|
|
|
|
|
62
|
2 |
|
$class = $namespace->addClass($this->getShortClassName()); |
|
63
|
2 |
|
$class->setFinal() |
|
64
|
2 |
|
->setImplements(['Psr\Http\Server\MiddlewareInterface']) |
|
65
|
2 |
|
->setProperties([ |
|
66
|
2 |
|
(new Property('hydrator'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
|
67
|
2 |
|
->addComment('@var HydratorInterface'), |
|
68
|
2 |
|
(new Property('logger'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
|
69
|
2 |
|
->addComment('@var LoggerInterface'), |
|
70
|
2 |
|
(new Property('managerRegistry'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
|
71
|
2 |
|
->addComment('@var EntityManagerRegistry'), |
|
72
|
|
|
]) |
|
73
|
2 |
|
->setMethods([ |
|
74
|
2 |
|
(new Method('__construct'))->setParameters([ |
|
75
|
2 |
|
(new Parameter('managerRegistry')) |
|
76
|
2 |
|
->setTypeHint('SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry'), |
|
77
|
2 |
|
(new Parameter('hydrator')) |
|
78
|
2 |
|
->setTypeHint('Zend\Hydrator\HydratorInterface'), |
|
79
|
2 |
|
(new Parameter('logger')) |
|
80
|
2 |
|
->setTypeHint('Psr\Log\LoggerInterface'), |
|
81
|
2 |
|
])->setBody(<<<'BODY' |
|
82
|
2 |
|
$this->managerRegistry = $managerRegistry; |
|
83
|
|
|
$this->hydrator = $hydrator; |
|
84
|
|
|
$this->logger = $logger; |
|
85
|
|
|
BODY |
|
86
|
2 |
|
)->setVisibility(ClassType::VISIBILITY_PUBLIC), |
|
87
|
2 |
|
(new Method('process')) |
|
88
|
2 |
|
->setParameters([ |
|
89
|
2 |
|
(new Parameter('request')) |
|
90
|
2 |
|
->setTypeHint('Psr\Http\Message\ServerRequestInterface'), |
|
91
|
2 |
|
(new Parameter('handler')) |
|
92
|
2 |
|
->setTypeHint('Psr\Http\Server\RequestHandlerInterface'), |
|
93
|
|
|
]) |
|
94
|
2 |
|
->setReturnType('Psr\Http\Message\ResponseInterface') |
|
95
|
2 |
|
->setComment("@inheritdoc\n") |
|
96
|
2 |
|
->setBody($this->getProcessMethodBody()) |
|
97
|
2 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC), |
|
98
|
|
|
]); |
|
99
|
|
|
|
|
100
|
2 |
|
return (new PsrPrinter())->printFile($file); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
private function addUsages(PhpNamespace $namespace) |
|
104
|
|
|
{ |
|
105
|
2 |
|
$namespace->addUse('Doctrine\ORM\ORMException'); |
|
106
|
2 |
|
$namespace->addUse('Psr\Http\Message\ResponseInterface'); |
|
107
|
2 |
|
$namespace->addUse('Psr\Http\Message\ServerRequestInterface'); |
|
108
|
2 |
|
$namespace->addUse('Psr\Http\Server\MiddlewareInterface'); |
|
109
|
2 |
|
$namespace->addUse('Psr\Http\Server\RequestHandlerInterface'); |
|
110
|
2 |
|
$namespace->addUse('Psr\Log\LoggerInterface'); |
|
111
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface'); |
|
112
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry'); |
|
113
|
2 |
|
$namespace->addUse($this->dataProvider->provide()['entityName']); |
|
114
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory'); |
|
115
|
2 |
|
$namespace->addUse('Zend\Hydrator\HydratorInterface'); |
|
116
|
2 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getClassName(): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->getFullClassName(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
2 |
|
private function getShortClassName(): string |
|
130
|
|
|
{ |
|
131
|
2 |
|
return 'Delete' . $this->dataProvider->provide()['entityClassName'] . 'Action'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
private function getFullClassName(): string |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->getNamespace() . '\\' . $this->getShortClassName(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
2 |
|
private function getNamespace(): string |
|
143
|
|
|
{ |
|
144
|
2 |
|
return $this->dataProvider->provide()['controller_namespace']; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function getFileName(): string |
|
151
|
|
|
{ |
|
152
|
2 |
|
return sprintf( |
|
153
|
2 |
|
'src/%s/Controller/%s.php', |
|
154
|
2 |
|
$this->dataProvider->provide()['moduleName'], |
|
155
|
2 |
|
$this->getShortClassName() |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|