1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace carono\codegen; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Nette\PhpGenerator\ClassType; |
8
|
|
|
use Nette\PhpGenerator\PhpFile; |
9
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ClassGenerator |
13
|
|
|
* |
14
|
|
|
* @package carono\codegen |
15
|
|
|
*/ |
16
|
|
|
abstract class ClassGenerator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* External generator |
20
|
|
|
* |
21
|
|
|
* @var mixed |
22
|
|
|
*/ |
23
|
|
|
public $generator; |
24
|
|
|
|
25
|
|
|
public $namespace; |
26
|
|
|
public $className; |
27
|
|
|
public $extends; |
28
|
|
|
public $output; |
29
|
|
|
|
30
|
|
|
protected $params = []; |
31
|
|
|
/** |
32
|
|
|
* @var ClassType |
33
|
|
|
*/ |
34
|
|
|
protected $phpClass; |
35
|
|
|
/** |
36
|
|
|
* @var PhpNamespace |
37
|
|
|
*/ |
38
|
|
|
protected $phpNamespace; |
39
|
|
|
/** |
40
|
|
|
* @var PhpFile |
41
|
|
|
*/ |
42
|
|
|
protected $phpFile; |
43
|
|
|
protected $exceptRenderMethods = [ |
44
|
|
|
'render', |
45
|
|
|
'renderToFile', |
46
|
|
|
'classUses', |
47
|
|
|
'classAfterRender', |
48
|
|
|
'classGeneratedBy', |
49
|
|
|
'formClassNamespace', |
50
|
|
|
'formClassName', |
51
|
|
|
'formExtends', |
52
|
|
|
'formOutputPath', |
53
|
|
|
'classTraits', |
54
|
|
|
'classImplements', |
55
|
|
|
'phpDocComments', |
56
|
|
|
'phpProperties', |
57
|
|
|
'classConstants', |
58
|
|
|
'__construct' |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
protected function classUses() |
65
|
|
|
{ |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $className |
71
|
|
|
* @return ClassType |
72
|
|
|
*/ |
73
|
|
|
protected function getPhpClass($className) |
74
|
|
|
{ |
75
|
|
|
if (!$this->phpClass) { |
76
|
|
|
$this->phpFile = new PhpFile(); |
77
|
|
|
$namespace = $this->phpFile->addNamespace($this->namespace ? $this->namespace : $this->formClassNamespace()); |
|
|
|
|
78
|
|
|
$this->phpNamespace = $namespace; |
79
|
|
|
return $this->phpClass = $namespace->addClass($className); |
80
|
|
|
} else { |
81
|
|
|
return $this->phpClass; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
protected function classAfterRender() |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function classGeneratedBy() |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'This class is generated using the package carono/codegen' |
100
|
|
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return null|string |
105
|
|
|
*/ |
106
|
|
|
protected function formClassNamespace() |
107
|
|
|
{ |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return null|string |
113
|
|
|
*/ |
114
|
|
|
protected function formClassName() |
115
|
|
|
{ |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return null|string |
121
|
|
|
*/ |
122
|
|
|
protected function formExtends() |
123
|
|
|
{ |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param ClassType $class |
129
|
|
|
*/ |
130
|
|
|
protected function applyClassProperties($class) |
131
|
|
|
{ |
132
|
|
|
foreach (array_filter($this->classProperties()) as $property => $value) { |
133
|
|
|
if (is_array($value)) { |
134
|
|
|
if (isset($value['value'])) { |
135
|
|
|
$classProperty = $class->addProperty($property, $value['value']); |
136
|
|
|
} else { |
137
|
|
|
$classProperty = $class->addProperty($property); |
138
|
|
|
} |
139
|
|
|
if (isset($value['visibility'])) { |
140
|
|
|
$classProperty->setVisibility($value['visibility']); |
141
|
|
|
} |
142
|
|
|
if (isset($value['comment'])) { |
143
|
|
|
foreach ((array)$value['comment'] as $comment) { |
144
|
|
|
$classProperty->addComment($comment); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} else { |
148
|
|
|
$class->addProperty($property, $value); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $params |
155
|
|
|
* @return string |
156
|
|
|
* @throws \Exception |
157
|
|
|
*/ |
158
|
|
|
public function render($params = []) |
159
|
|
|
{ |
160
|
|
|
$this->params = $params; |
161
|
|
|
$className = $this->className ? $this->className : $this->formClassName(); |
|
|
|
|
162
|
|
|
if (!$className) { |
163
|
|
|
throw new \Exception('The class name was not set, update $className parameter or implement formClassName()'); |
164
|
|
|
} |
165
|
|
|
$class = $this->getPhpClass($className); |
166
|
|
|
if ($extends = ($this->extends ? $this->extends : $this->formExtends())) { |
|
|
|
|
167
|
|
|
$this->phpClass->setExtends($extends); |
168
|
|
|
} |
169
|
|
|
$reflection = new \ReflectionClass($this); |
170
|
|
|
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
171
|
|
|
if (!in_array($method->name, $this->exceptRenderMethods)) { |
172
|
|
|
$result = call_user_func([$this, $method->name], $class->addMethod($method->name)); |
173
|
|
|
if ($result === false) { |
174
|
|
|
$methods = $class->getMethods(); |
175
|
|
|
unset($methods[$method->name]); |
176
|
|
|
$class->setMethods($methods); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
$this->applyClassProperties($class); |
181
|
|
|
foreach (array_filter($this->classUses()) as $alias => $namespace) { |
182
|
|
|
$this->phpNamespace->addUse($namespace, is_numeric($alias) ? null : $alias); |
183
|
|
|
} |
184
|
|
|
foreach ($this->phpDocComments() as $comment) { |
185
|
|
|
$this->phpClass->addComment($comment); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
foreach (array_filter($this->classConstants()) as $constant => $value) { |
189
|
|
|
$this->phpClass->addConstant($constant, $value); |
190
|
|
|
} |
191
|
|
|
foreach (array_filter($this->classTraits()) as $trait => $resolutions) { |
192
|
|
|
$this->phpClass->addTrait(is_numeric($trait) ? $resolutions : $trait, is_numeric($trait) ? [] : $resolutions); |
193
|
|
|
} |
194
|
|
|
foreach (array_filter($this->classImplements()) as $implement) { |
195
|
|
|
$this->phpClass->addImplement($implement); |
196
|
|
|
} |
197
|
|
|
$this->classAfterRender(); |
198
|
|
|
$generatedBy = $this->classGeneratedBy(); |
199
|
|
|
$this->phpFile->addComment(is_array($generatedBy) ? join("\n", $generatedBy) : $generatedBy); |
|
|
|
|
200
|
|
|
$this->output = $this->formOutputPath(); |
|
|
|
|
201
|
|
|
return (string)$this->phpFile; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return null|string |
206
|
|
|
*/ |
207
|
|
|
protected function formOutputPath() |
208
|
|
|
{ |
209
|
|
|
return null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param $filePath |
214
|
|
|
* @param $params |
215
|
|
|
* @return bool|int |
216
|
|
|
*/ |
217
|
|
|
public function renderToFile($params = [], $filePath = null) |
218
|
|
|
{ |
219
|
|
|
$content = $this->render($params); |
220
|
|
|
return file_put_contents($filePath ?: $this->output, $content); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
protected function classTraits() |
227
|
|
|
{ |
228
|
|
|
return []; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
|
|
protected function classImplements() |
235
|
|
|
{ |
236
|
|
|
return []; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
protected function phpDocComments() |
243
|
|
|
{ |
244
|
|
|
return []; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
protected function classProperties() |
251
|
|
|
{ |
252
|
|
|
return []; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
protected function classConstants() |
259
|
|
|
{ |
260
|
|
|
return []; |
261
|
|
|
} |
262
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.