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 Psr\Log\LoggerInterface; |
14
|
|
|
use SlayerBirden\DFCodeGeneration\Generator\DataProvider\DataProviderInterface; |
15
|
|
|
use SlayerBirden\DFCodeGeneration\Generator\GeneratorInterface; |
16
|
|
|
use Twig\Environment; |
17
|
|
|
use Twig\Loader\FilesystemLoader; |
18
|
|
|
|
19
|
|
|
final class GetsGenerator implements GeneratorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var DataProviderInterface |
23
|
|
|
*/ |
24
|
|
|
private $dataProvider; |
25
|
|
|
/** |
26
|
|
|
* @var Environment |
27
|
|
|
*/ |
28
|
|
|
private $twig; |
29
|
|
|
|
30
|
2 |
|
public function __construct(DataProviderInterface $dataProvider) |
31
|
|
|
{ |
32
|
2 |
|
$this->dataProvider = $dataProvider; |
33
|
2 |
|
$loader = new FilesystemLoader(__DIR__ . '/Templates'); |
34
|
2 |
|
$this->twig = new Environment($loader); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
* @throws \Twig_Error_Loader |
40
|
|
|
* @throws \Twig_Error_Runtime |
41
|
|
|
* @throws \Twig_Error_Syntax |
42
|
|
|
*/ |
43
|
2 |
|
private function getProcessMethodBody(): string |
44
|
|
|
{ |
45
|
2 |
|
return $this->twig->load('Gets/Process.template.twig')->render($this->dataProvider->provide()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
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 |
|
$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 |
|
$repositoryPropName = $this->dataProvider->provide()['refName'] . 'Repository'; |
65
|
2 |
|
$class->setFinal() |
66
|
2 |
|
->setImplements(['Psr\Http\Server\MiddlewareInterface']) |
67
|
2 |
|
->setProperties([ |
68
|
2 |
|
(new Property($repositoryPropName))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
69
|
2 |
|
->addComment('@var Selectable'), |
70
|
2 |
|
(new Property('logger'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
71
|
2 |
|
->addComment('@var LoggerInterface'), |
72
|
2 |
|
(new Property('hydrator'))->setVisibility(ClassType::VISIBILITY_PRIVATE) |
73
|
2 |
|
->addComment('@var HydratorInterface'), |
74
|
|
|
]) |
75
|
2 |
|
->setMethods([ |
76
|
2 |
|
(new Method('__construct'))->setParameters([ |
77
|
2 |
|
(new Parameter($repositoryPropName)) |
78
|
2 |
|
->setTypeHint('Doctrine\Common\Collections\Selectable'), |
79
|
2 |
|
(new Parameter('logger')) |
80
|
2 |
|
->setTypeHint(LoggerInterface::class), |
81
|
2 |
|
(new Parameter('hydrator')) |
82
|
2 |
|
->setTypeHint('Zend\Hydrator\HydratorInterface'), |
83
|
2 |
|
])->setBody(<<<BODY |
84
|
2 |
|
\$this->$repositoryPropName = \$$repositoryPropName; |
85
|
|
|
\$this->logger = \$logger; |
86
|
|
|
\$this->hydrator = \$hydrator; |
87
|
|
|
BODY |
88
|
2 |
|
)->setVisibility(ClassType::VISIBILITY_PUBLIC), |
89
|
2 |
|
(new Method('process')) |
90
|
2 |
|
->setParameters([ |
91
|
2 |
|
(new Parameter('request')) |
92
|
2 |
|
->setTypeHint('Psr\Http\Message\ServerRequestInterface'), |
93
|
2 |
|
(new Parameter('handler')) |
94
|
2 |
|
->setTypeHint('Psr\Http\Server\RequestHandlerInterface'), |
95
|
|
|
]) |
96
|
2 |
|
->setReturnType('Psr\Http\Message\ResponseInterface') |
97
|
2 |
|
->setComment("@inheritdoc\n") |
98
|
2 |
|
->setBody($this->getProcessMethodBody()) |
99
|
2 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC), |
100
|
|
|
]); |
101
|
|
|
|
102
|
2 |
|
return (new PsrPrinter())->printFile($file); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
private function addUsages(PhpNamespace $namespace) |
106
|
|
|
{ |
107
|
2 |
|
$namespace->addUse('Doctrine\Common\Collections\Selectable'); |
108
|
2 |
|
$namespace->addUse('Doctrine\ORM\ORMException'); |
109
|
2 |
|
$namespace->addUse('Psr\Http\Message\ResponseInterface'); |
110
|
2 |
|
$namespace->addUse('Psr\Http\Message\ServerRequestInterface'); |
111
|
2 |
|
$namespace->addUse('Psr\Http\Server\MiddlewareInterface'); |
112
|
2 |
|
$namespace->addUse('Psr\Http\Server\RequestHandlerInterface'); |
113
|
2 |
|
$namespace->addUse(LoggerInterface::class); |
114
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Collection\CriteriaBuilder'); |
115
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Hydrator\ListExtractor'); |
116
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\ResponseFactory'); |
117
|
2 |
|
$namespace->addUse('Zend\Hydrator\HydratorInterface'); |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getClassName(): string |
124
|
|
|
{ |
125
|
|
|
return $this->getFullClassName(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
2 |
|
private function getShortClassName(): string |
132
|
|
|
{ |
133
|
2 |
|
return 'Get' . $this->dataProvider->provide()['pluralEntityName'] . 'Action'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
private function getFullClassName(): string |
140
|
|
|
{ |
141
|
|
|
return $this->getNamespace() . '\\' . $this->getShortClassName(); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
private function getNamespace(): string |
145
|
|
|
{ |
146
|
2 |
|
return $this->dataProvider->provide()['controller_namespace']; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
2 |
|
public function getFileName(): string |
153
|
|
|
{ |
154
|
2 |
|
return sprintf( |
155
|
2 |
|
'src/%s/Controller/%s.php', |
156
|
2 |
|
$this->dataProvider->provide()['moduleName'], |
157
|
2 |
|
$this->getShortClassName() |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|