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