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 AddGenerator 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('Add/Process.template.twig')->render($this->dataProvider->provide()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
* @throws \ReflectionException |
50
|
|
|
* @throws \Twig_Error_Loader |
51
|
|
|
* @throws \Twig_Error_Runtime |
52
|
|
|
* @throws \Twig_Error_Syntax |
53
|
|
|
*/ |
54
|
2 |
|
public function generate(): string |
55
|
|
|
{ |
56
|
2 |
|
$fullEntityName = $this->getEntityName(); |
57
|
2 |
|
$file = new PhpFile; |
58
|
2 |
|
$file->addComment('This file is generated by SlayerBirden\DFCodeGeneration'); |
59
|
2 |
|
$file->setStrictTypes(); |
60
|
|
|
|
61
|
2 |
|
$namespace = $file->addNamespace($this->getNamespace()); |
62
|
2 |
|
$this->addUsages($namespace); |
63
|
|
|
|
64
|
2 |
|
$class = $namespace->addClass($this->getShortClassName()); |
65
|
2 |
|
$class->setFinal() |
66
|
2 |
|
->setImplements(['Psr\Http\Server\MiddlewareInterface']) |
67
|
2 |
|
->setProperties([ |
68
|
2 |
|
(new Property('hydrator'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
69
|
2 |
|
->addComment('@var HydratorInterface'), |
70
|
2 |
|
(new Property('inputFilter'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
71
|
2 |
|
->addComment('@var InputFilterInterface'), |
72
|
2 |
|
(new Property('logger'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
73
|
2 |
|
->addComment('@var LoggerInterface'), |
74
|
2 |
|
(new Property('managerRegistry'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
75
|
2 |
|
->addComment('@var EntityManagerRegistry'), |
76
|
|
|
]) |
77
|
2 |
|
->setMethods([ |
78
|
2 |
|
(new Method('__construct'))->setParameters([ |
79
|
2 |
|
(new Parameter('managerRegistry')) |
80
|
2 |
|
->setTypeHint('SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry'), |
81
|
2 |
|
(new Parameter('hydrator')) |
82
|
2 |
|
->setTypeHint('Zend\Hydrator\HydratorInterface'), |
83
|
2 |
|
(new Parameter('inputFilter')) |
84
|
2 |
|
->setTypeHint('Zend\InputFilter\InputFilterInterface'), |
85
|
2 |
|
(new Parameter('logger')) |
86
|
2 |
|
->setTypeHint('Psr\Log\LoggerInterface'), |
87
|
2 |
|
])->setBody(<<<'BODY' |
88
|
2 |
|
$this->managerRegistry = $managerRegistry; |
89
|
|
|
$this->hydrator = $hydrator; |
90
|
|
|
$this->inputFilter = $inputFilter; |
91
|
|
|
$this->logger = $logger; |
92
|
|
|
BODY |
93
|
2 |
|
)->setVisibility(ClassType::VISIBILITY_PUBLIC), |
94
|
2 |
|
(new Method('process')) |
95
|
2 |
|
->setParameters([ |
96
|
2 |
|
(new Parameter('request')) |
97
|
2 |
|
->setTypeHint('Psr\Http\Message\ServerRequestInterface'), |
98
|
2 |
|
(new Parameter('handler')) |
99
|
2 |
|
->setTypeHint('Psr\Http\Server\RequestHandlerInterface'), |
100
|
|
|
]) |
101
|
2 |
|
->setReturnType('Psr\Http\Message\ResponseInterface') |
102
|
2 |
|
->setComment("@inheritdoc\n") |
103
|
2 |
|
->setBody($this->getProcessMethodBody()) |
104
|
2 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC), |
105
|
2 |
|
(new Method('getEntity')) |
106
|
2 |
|
->setParameters([ |
107
|
2 |
|
(new Parameter('data'))->setTypeHint('array'), |
108
|
|
|
]) |
109
|
2 |
|
->setReturnType($fullEntityName) |
110
|
2 |
|
->setBody( |
111
|
2 |
|
$this->twig->load('Add/GetEntity.template.twig')->render($this->dataProvider->provide()) |
112
|
|
|
) |
113
|
2 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC), |
114
|
|
|
]); |
115
|
|
|
|
116
|
2 |
|
return (new PsrPrinter())->printFile($file); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
private function addUsages(PhpNamespace $namespace) |
120
|
|
|
{ |
121
|
2 |
|
if ($this->hasUnique()) { |
122
|
|
|
$namespace->addUse('Doctrine\DBAL\Exception\UniqueConstraintViolationException'); |
123
|
|
|
} |
124
|
2 |
|
$namespace->addUse('Doctrine\ORM\ORMException'); |
125
|
2 |
|
$namespace->addUse('Doctrine\ORM\ORMInvalidArgumentException'); |
126
|
2 |
|
$namespace->addUse('Psr\Http\Message\ResponseInterface'); |
127
|
2 |
|
$namespace->addUse('Psr\Http\Message\ServerRequestInterface'); |
128
|
2 |
|
$namespace->addUse('Psr\Http\Server\MiddlewareInterface'); |
129
|
2 |
|
$namespace->addUse('Psr\Http\Server\RequestHandlerInterface'); |
130
|
2 |
|
$namespace->addUse('Psr\Log\LoggerInterface'); |
131
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry'); |
132
|
2 |
|
$namespace->addUse($this->getEntityName()); |
133
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\Validation\DataValidationResponseFactory'); |
134
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory'); |
135
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory'); |
136
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\Validation\ValidationResponseFactory'); |
137
|
2 |
|
$namespace->addUse('Zend\Hydrator\HydratorInterface'); |
138
|
2 |
|
$namespace->addUse('Zend\InputFilter\InputFilterInterface'); |
139
|
2 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getClassName(): string |
145
|
|
|
{ |
146
|
|
|
return $this->getFullClassName(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
2 |
|
private function getShortClassName(): string |
153
|
|
|
{ |
154
|
2 |
|
return 'Add' . $this->dataProvider->provide()['entityClassName'] . 'Action'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private function getFullClassName(): string |
161
|
|
|
{ |
162
|
|
|
return $this->getNamespace() . '\\' . $this->getShortClassName(); |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
private function getEntityName(): string |
166
|
|
|
{ |
167
|
2 |
|
return $this->dataProvider->provide()['entityName']; |
168
|
|
|
} |
169
|
|
|
|
170
|
2 |
|
private function hasUnique(): bool |
171
|
|
|
{ |
172
|
2 |
|
return (bool)$this->dataProvider->provide()['hasUnique']; |
173
|
|
|
} |
174
|
|
|
|
175
|
2 |
|
private function getNamespace(): string |
176
|
|
|
{ |
177
|
2 |
|
return $this->dataProvider->provide()['controller_namespace']; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
2 |
|
public function getFileName(): string |
184
|
|
|
{ |
185
|
2 |
|
return sprintf( |
186
|
2 |
|
'src/%s/Controller/%s.php', |
187
|
2 |
|
$this->dataProvider->provide()['entityClassName'], |
188
|
2 |
|
$this->getShortClassName() |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|