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 |
|
$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('managerRegistry'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
67
|
2 |
|
->addComment('@var EntityManagerRegistry'), |
68
|
2 |
|
(new Property('hydrator'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
69
|
2 |
|
->addComment('@var HydratorInterface'), |
70
|
2 |
|
(new Property('logger'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
71
|
2 |
|
->addComment('@var LoggerInterface'), |
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 |
|
if ($this->hasUnique()) { |
106
|
|
|
$namespace->addUse('Doctrine\DBAL\Exception\UniqueConstraintViolationException'); |
107
|
|
|
} |
108
|
2 |
|
$namespace->addUse('Doctrine\ORM\ORMException'); |
109
|
2 |
|
$namespace->addUse('Doctrine\ORM\ORMInvalidArgumentException'); |
110
|
2 |
|
$namespace->addUse('Psr\Http\Message\ResponseInterface'); |
111
|
2 |
|
$namespace->addUse('Psr\Http\Message\ServerRequestInterface'); |
112
|
2 |
|
$namespace->addUse('Psr\Http\Server\MiddlewareInterface'); |
113
|
2 |
|
$namespace->addUse('Psr\Http\Server\RequestHandlerInterface'); |
114
|
2 |
|
$namespace->addUse('Psr\Log\LoggerInterface'); |
115
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface'); |
116
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry'); |
117
|
2 |
|
$namespace->addUse($this->getEntityName()); |
118
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\Request\Parser'); |
119
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\ResponseFactory'); |
120
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Validation\Exception\ValidationException'); |
121
|
2 |
|
$namespace->addUse('Zend\Hydrator\HydratorInterface'); |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getClassName(): string |
128
|
|
|
{ |
129
|
|
|
return $this->getFullClassName(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
2 |
|
private function getShortClassName(): string |
136
|
|
|
{ |
137
|
2 |
|
return 'Update' . $this->dataProvider->provide()['entityClassName'] . 'Action'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function getFullClassName(): string |
144
|
|
|
{ |
145
|
|
|
return $this->getNamespace() . '\\' . $this->getShortClassName(); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
private function getEntityName(): string |
149
|
|
|
{ |
150
|
2 |
|
return $this->dataProvider->provide()['entityName']; |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
private function hasUnique(): bool |
154
|
|
|
{ |
155
|
2 |
|
return (bool)$this->dataProvider->provide()['hasUnique']; |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
private function getNamespace(): string |
159
|
|
|
{ |
160
|
2 |
|
return $this->dataProvider->provide()['controller_namespace']; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
2 |
|
public function getFileName(): string |
167
|
|
|
{ |
168
|
2 |
|
return sprintf( |
169
|
2 |
|
'src/%s/Controller/%s.php', |
170
|
2 |
|
$this->dataProvider->provide()['moduleName'], |
171
|
2 |
|
$this->getShortClassName() |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|