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 GetGenerator 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('Get/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
|
|
|
]) |
69
|
2 |
|
->setMethods([ |
70
|
2 |
|
(new Method('__construct'))->setParameters([ |
71
|
2 |
|
(new Parameter('hydrator')) |
72
|
2 |
|
->setTypeHint('Zend\Hydrator\HydratorInterface'), |
73
|
2 |
|
])->setBody(<<<'BODY' |
74
|
2 |
|
$this->hydrator = $hydrator; |
75
|
|
|
BODY |
76
|
2 |
|
)->setVisibility(ClassType::VISIBILITY_PUBLIC), |
77
|
2 |
|
(new Method('process')) |
78
|
2 |
|
->setParameters([ |
79
|
2 |
|
(new Parameter('request')) |
80
|
2 |
|
->setTypeHint('Psr\Http\Message\ServerRequestInterface'), |
81
|
2 |
|
(new Parameter('handler')) |
82
|
2 |
|
->setTypeHint('Psr\Http\Server\RequestHandlerInterface'), |
83
|
|
|
]) |
84
|
2 |
|
->setReturnType('Psr\Http\Message\ResponseInterface') |
85
|
2 |
|
->setComment("@inheritdoc\n") |
86
|
2 |
|
->setBody($this->getProcessMethodBody()) |
87
|
2 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC), |
88
|
|
|
]); |
89
|
|
|
|
90
|
2 |
|
return (new PsrPrinter())->printFile($file); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
private function addUsages(PhpNamespace $namespace) |
94
|
|
|
{ |
95
|
2 |
|
$namespace->addUse('Psr\Http\Message\ResponseInterface'); |
96
|
2 |
|
$namespace->addUse('Psr\Http\Message\ServerRequestInterface'); |
97
|
2 |
|
$namespace->addUse('Psr\Http\Server\MiddlewareInterface'); |
98
|
2 |
|
$namespace->addUse('Psr\Http\Server\RequestHandlerInterface'); |
99
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface'); |
100
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Stdlib\ResponseFactory'); |
101
|
2 |
|
$namespace->addUse('Zend\Hydrator\HydratorInterface'); |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getClassName(): string |
108
|
|
|
{ |
109
|
|
|
return $this->getFullClassName(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
2 |
|
private function getShortClassName(): string |
116
|
|
|
{ |
117
|
2 |
|
return 'Get' . $this->dataProvider->provide()['entityClassName'] . 'Action'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
private function getFullClassName(): string |
124
|
|
|
{ |
125
|
|
|
return $this->getNamespace() . '\\' . $this->getShortClassName(); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
private function getNamespace(): string |
129
|
|
|
{ |
130
|
2 |
|
return $this->dataProvider->provide()['controller_namespace']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
2 |
|
public function getFileName(): string |
137
|
|
|
{ |
138
|
2 |
|
return sprintf( |
139
|
2 |
|
'src/%s/Controller/%s.php', |
140
|
2 |
|
$this->dataProvider->provide()['moduleName'], |
141
|
2 |
|
$this->getShortClassName() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|