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 RepositoryFactoryGenerator implements GeneratorInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var DataProviderInterface |
19
|
|
|
*/ |
20
|
|
|
private $dataProvider; |
21
|
|
|
/** |
22
|
|
|
* @var Environment |
23
|
|
|
*/ |
24
|
|
|
private $twig; |
25
|
|
|
|
26
|
2 |
|
public function __construct(DataProviderInterface $dataProvider) |
27
|
|
|
{ |
28
|
2 |
|
$this->dataProvider = $dataProvider; |
29
|
2 |
|
$loader = new FilesystemLoader(__DIR__ . '/Templates'); |
30
|
2 |
|
$this->twig = new Environment($loader); |
31
|
2 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
* @throws \Twig_Error_Loader |
36
|
|
|
* @throws \Twig_Error_Runtime |
37
|
|
|
* @throws \Twig_Error_Syntax |
38
|
|
|
*/ |
39
|
2 |
|
public function generate(): string |
40
|
|
|
{ |
41
|
2 |
|
$file = new PhpFile(); |
42
|
2 |
|
$file->setStrictTypes(); |
43
|
2 |
|
$file->addComment('This file is generated by SlayerBirden\DFCodeGeneration'); |
44
|
|
|
|
45
|
2 |
|
$namespace = $file->addNamespace($this->getFactoryNamespace()); |
46
|
2 |
|
$namespace->addUse(\Interop\Container\ContainerInterface::class); |
47
|
2 |
|
$namespace->addUse(\Zend\ServiceManager\Factory\FactoryInterface::class); |
48
|
2 |
|
$namespace->addUse('Doctrine\Common\Collections\Selectable'); |
49
|
2 |
|
$namespace->addUse('SlayerBirden\DataFlowServer\Doctrine\Persistence\EntityManagerRegistry'); |
50
|
2 |
|
$namespace->addUse($this->dataProvider->provide()['entityName']); |
51
|
|
|
|
52
|
2 |
|
$class = $namespace->addClass($this->getShortClassName()); |
53
|
|
|
|
54
|
2 |
|
$class->addImplement(\Zend\ServiceManager\Factory\FactoryInterface::class) |
55
|
2 |
|
->setFinal() |
56
|
2 |
|
->addMethod('__invoke') |
57
|
2 |
|
->setParameters([ |
58
|
2 |
|
(new Parameter('container'))->setTypeHint(\Interop\Container\ContainerInterface::class), |
59
|
2 |
|
(new Parameter('requestedName')), |
60
|
2 |
|
(new Parameter('options'))->setTypeHint('array')->setDefaultValue(null) |
61
|
|
|
]) |
62
|
2 |
|
->setReturnType('Doctrine\Common\Collections\Selectable') |
63
|
2 |
|
->setBody( |
64
|
2 |
|
$this->twig->load('RepositoryFactory/Invoke.template.twig') |
65
|
2 |
|
->render($this->dataProvider->provide()) |
66
|
|
|
) |
67
|
2 |
|
->setVisibility(ClassType::VISIBILITY_PUBLIC); |
68
|
|
|
|
69
|
2 |
|
return (new PsrPrinter())->printFile($file); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getClassName(): string |
73
|
|
|
{ |
74
|
|
|
return $this->getFactoryNamespace() . '\\' . $this->getShortClassName(); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
public function getFileName(): string |
78
|
|
|
{ |
79
|
2 |
|
return sprintf( |
80
|
2 |
|
'src/%s/Factory/%s.php', |
81
|
2 |
|
$this->dataProvider->provide()['moduleName'], |
82
|
2 |
|
$this->getShortClassName() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
2 |
|
private function getShortClassName(): string |
90
|
|
|
{ |
91
|
2 |
|
return $this->dataProvider->provide()['entityClassName'] . 'RepositoryFactory'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
2 |
|
private function getFactoryNamespace(): string |
98
|
|
|
{ |
99
|
2 |
|
return $this->dataProvider->provide()['factory_namespace']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|