|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Generator\Factory; |
|
5
|
|
|
|
|
6
|
|
|
use Nette\PhpGenerator\ClassType; |
|
7
|
|
|
use Nette\PhpGenerator\Parameter; |
|
8
|
|
|
use Nette\PhpGenerator\PhpFile; |
|
9
|
|
|
use Nette\PhpGenerator\PsrPrinter; |
|
10
|
|
|
use SlayerBirden\DFCodeGeneration\Generator\DataProvider\DataProviderInterface; |
|
11
|
|
|
use SlayerBirden\DFCodeGeneration\Generator\GeneratorInterface; |
|
12
|
|
|
use Twig\Environment; |
|
13
|
|
|
use Twig\Loader\FilesystemLoader; |
|
14
|
|
|
|
|
15
|
|
|
final class HydratorFactoryGenerator implements GeneratorInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var DataProviderInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $dataProvider; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Environment |
|
23
|
|
|
*/ |
|
24
|
|
|
private $twig; |
|
25
|
|
|
|
|
26
|
10 |
|
public function __construct(DataProviderInterface $dataProvider) |
|
27
|
|
|
{ |
|
28
|
10 |
|
$this->dataProvider = $dataProvider; |
|
29
|
10 |
|
$loader = new FilesystemLoader(__DIR__ . '/Templates'); |
|
30
|
10 |
|
$this->twig = new Environment($loader); |
|
31
|
10 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return string |
|
35
|
|
|
* @throws \Twig_Error_Loader |
|
36
|
|
|
* @throws \Twig_Error_Runtime |
|
37
|
|
|
* @throws \Twig_Error_Syntax |
|
38
|
|
|
*/ |
|
39
|
10 |
|
public function generate(): string |
|
40
|
|
|
{ |
|
41
|
10 |
|
$file = new PhpFile(); |
|
42
|
10 |
|
$file->setStrictTypes(); |
|
43
|
10 |
|
$file->addComment('This file is generated by SlayerBirden\DFCodeGeneration'); |
|
44
|
|
|
|
|
45
|
10 |
|
$namespace = $file->addNamespace($this->getFactoryNamespace()); |
|
46
|
10 |
|
$namespace->addUse(\Interop\Container\ContainerInterface::class); |
|
47
|
10 |
|
if (!empty($this->getSingleDeps())) { |
|
48
|
|
|
$namespace->addUse( |
|
49
|
|
|
'\SlayerBirden\DataFlowServer\Doctrine\Hydrator\Strategy\ExtractionNestedEntityStrategy' |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
10 |
|
if (!empty($this->getMultiDeps())) { |
|
53
|
|
|
$namespace->addUse('\SlayerBirden\DataFlowServer\Doctrine\Hydrator\Strategy\CollectionStrategy'); |
|
54
|
|
|
} |
|
55
|
10 |
|
$namespace->addUse(\Zend\Hydrator\ClassMethods::class); |
|
56
|
10 |
|
$namespace->addUse(\Zend\ServiceManager\Factory\FactoryInterface::class); |
|
57
|
10 |
|
$namespace->addUse(\Zend\Hydrator\HydratorInterface::class); |
|
58
|
|
|
|
|
59
|
10 |
|
$class = $namespace->addClass($this->getShortClassName()); |
|
60
|
|
|
|
|
61
|
10 |
|
$class->addImplement(\Zend\ServiceManager\Factory\FactoryInterface::class) |
|
62
|
10 |
|
->setFinal() |
|
63
|
10 |
|
->addMethod('__invoke') |
|
64
|
10 |
|
->setParameters([ |
|
65
|
10 |
|
(new Parameter('container'))->setTypeHint(\Interop\Container\ContainerInterface::class), |
|
66
|
10 |
|
(new Parameter('requestedName')), |
|
67
|
10 |
|
(new Parameter('options'))->setTypeHint('array')->setDefaultValue(null) |
|
68
|
|
|
]) |
|
69
|
10 |
|
->setReturnType(\Zend\Hydrator\HydratorInterface::class) |
|
70
|
10 |
|
->setBody( |
|
71
|
10 |
|
$this->twig->load('HydratorFactory/Invoke.template.twig')->render($this->dataProvider->provide()) |
|
72
|
|
|
) |
|
73
|
10 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC); |
|
74
|
|
|
|
|
75
|
10 |
|
return (new PsrPrinter())->printFile($file); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getClassName(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->getFactoryNamespace() . '\\' . $this->getShortClassName(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
10 |
|
public function getFileName(): string |
|
84
|
|
|
{ |
|
85
|
10 |
|
return sprintf( |
|
86
|
10 |
|
'src/%s/Factory/%s.php', |
|
87
|
10 |
|
$this->dataProvider->provide()['moduleName'], |
|
88
|
10 |
|
$this->getShortClassName() |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
10 |
|
private function getShortClassName(): string |
|
96
|
|
|
{ |
|
97
|
10 |
|
return $this->dataProvider->provide()['hydrator_factory_name']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
10 |
|
private function getFactoryNamespace(): string |
|
104
|
|
|
{ |
|
105
|
10 |
|
return $this->dataProvider->provide()['factory_namespace']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
10 |
|
private function getSingleDeps(): array |
|
109
|
|
|
{ |
|
110
|
10 |
|
return $this->dataProvider->provide()['single_deps']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
10 |
|
private function getMultiDeps(): array |
|
114
|
|
|
{ |
|
115
|
10 |
|
return $this->dataProvider->provide()['multi_deps']; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|