1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Library\DTO\View\Nette; |
15
|
|
|
|
16
|
|
|
use Micro\Library\DTO\ClassDef\ClassDefinition; |
17
|
|
|
use Micro\Library\DTO\ClassDef\MethodDefinition; |
18
|
|
|
use Micro\Library\DTO\ClassDef\PropertyDefinition; |
19
|
|
|
use Micro\Library\DTO\View\RendererInterface; |
20
|
|
|
use Nette\PhpGenerator\ClassType; |
21
|
|
|
use Nette\PhpGenerator\Method; |
22
|
|
|
use Nette\PhpGenerator\PhpFile; |
23
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
24
|
|
|
use Nette\PhpGenerator\Property; |
25
|
|
|
use Nette\PhpGenerator\PsrPrinter; |
26
|
|
|
|
27
|
|
|
class NetteRenderer implements RendererInterface |
28
|
|
|
{ |
29
|
1 |
|
public function render(ClassDefinition $classDefinition): string |
30
|
|
|
{ |
31
|
1 |
|
$phpFile = $this->createPhpFileType($classDefinition); |
32
|
1 |
|
$class = array_values($phpFile->getClasses()); |
33
|
|
|
/** @var ClassType $class */ |
34
|
1 |
|
$class = $class[0]; |
35
|
|
|
|
36
|
1 |
|
$class |
37
|
1 |
|
->setExtends($classDefinition->getExtends()) |
38
|
1 |
|
->setFinal() |
39
|
1 |
|
; |
40
|
|
|
|
41
|
1 |
|
foreach ($classDefinition->getComments() as $comment) { |
42
|
1 |
|
$class->addComment($comment); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$this->provideProperties($class, $classDefinition); |
46
|
1 |
|
$this->provideMethods($class, $classDefinition); |
47
|
|
|
// $this->provideMeta($class, $classData); |
48
|
|
|
|
49
|
1 |
|
return $this->printClass($phpFile); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
protected function createPhpFileType(ClassDefinition $classDefinition): PhpFile |
53
|
|
|
{ |
54
|
1 |
|
$file = new PhpFile(); |
55
|
1 |
|
$file->addComment('This file is auto-generated.'); |
56
|
1 |
|
$file->setStrictTypes(); |
57
|
1 |
|
$namespaceObj = new PhpNamespace($classDefinition->getNamespace()); |
58
|
1 |
|
$namespaceObj->addClass($classDefinition->getName()); |
59
|
1 |
|
$namespace = $file->addNamespace($namespaceObj); |
60
|
|
|
|
61
|
1 |
|
$this->provideUsages($namespace, $classDefinition); |
62
|
|
|
|
63
|
1 |
|
return $file; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
protected function provideUsages(PhpNamespace $namespace, ClassDefinition $classDefinition): void |
67
|
|
|
{ |
68
|
1 |
|
$usages = $classDefinition->getUseStatements(); |
69
|
1 |
|
foreach ($usages as $ns => $alias) { |
70
|
1 |
|
$namespace->addUse($ns, $alias); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
protected function provideProperties(ClassType $classType, ClassDefinition $classDefinition): void |
75
|
|
|
{ |
76
|
1 |
|
foreach ($classDefinition->getProperties() as $property) { |
77
|
1 |
|
$this->provideProperty($classType, $property); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
protected function provideProperty(ClassType $classType, PropertyDefinition $propertyDefinition): void |
82
|
|
|
{ |
83
|
1 |
|
$property = new Property($propertyDefinition->getName()); |
84
|
1 |
|
$isRequired = $propertyDefinition->isRequired(); |
85
|
1 |
|
$property |
86
|
1 |
|
->setProtected() |
87
|
1 |
|
->setType(implode('|', $propertyDefinition->getTypes())); |
88
|
|
|
|
89
|
1 |
|
if (false === $isRequired) { |
90
|
1 |
|
$property->setValue(null); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
foreach ($propertyDefinition->getComments() as $comment) { |
94
|
1 |
|
$property->addComment($comment); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
foreach ($propertyDefinition->getAttributes() as $attribute) { |
98
|
1 |
|
foreach ($attribute as $attrName => $attrArgs) { |
99
|
1 |
|
$property->addAttribute($attrName, $attrArgs); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$classType->addMember($property); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
protected function printClass(PhpFile $phpFile): string |
107
|
|
|
{ |
108
|
1 |
|
$printer = new PsrPrinter(); |
109
|
|
|
|
110
|
1 |
|
return $printer->printFile($phpFile); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
protected function provideMethods(ClassType $classType, ClassDefinition $classDefinition): void |
114
|
|
|
{ |
115
|
1 |
|
foreach ($classDefinition->getMethods() as $methodDef) { |
116
|
1 |
|
$this->provideMethod($classType, $methodDef); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
protected function provideMethod(ClassType $classType, MethodDefinition $methodDefinition): void |
121
|
|
|
{ |
122
|
1 |
|
$method = new Method($methodDefinition->getName()); |
123
|
1 |
|
$method |
124
|
1 |
|
->setBody($methodDefinition->getBody()) |
125
|
1 |
|
->setReturnType(implode(separator: '|', array: $methodDefinition->getTypesReturn())) |
126
|
1 |
|
->setVisibility($methodDefinition->getVisibility()) |
127
|
1 |
|
->setStatic($methodDefinition->isStatic()) |
128
|
1 |
|
; |
129
|
|
|
|
130
|
1 |
|
foreach ($methodDefinition->getComments() as $comment) { |
131
|
|
|
$method->addComment($comment); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
foreach ($methodDefinition->getArgs() as $arg) { |
135
|
1 |
|
$parameter = $method->addParameter($arg->getName()); |
136
|
1 |
|
$parameter->setType(implode('|', $arg->getTypes())); |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
$classType->addMember($method); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|