11
11
namespace OpenCodeModeling \JsonSchemaToPhpAst ;
12
12
13
13
use OpenCodeModeling \CodeAst \Builder \ClassBuilder ;
14
- use OpenCodeModeling \CodeAst \Builder \ClassBuilderCollection ;
15
14
use OpenCodeModeling \CodeAst \Builder \ClassConstBuilder ;
16
15
use OpenCodeModeling \CodeAst \Builder \ClassMethodBuilder ;
17
16
use OpenCodeModeling \CodeAst \Builder \ClassPropertyBuilder ;
17
+ use OpenCodeModeling \CodeAst \Builder \FileCollection ;
18
18
use OpenCodeModeling \CodeAst \Code \ClassConstGenerator ;
19
- use OpenCodeModeling \CodeAst \Package \ClassInfo ;
20
19
use OpenCodeModeling \CodeAst \Package \ClassInfoList ;
21
20
use OpenCodeModeling \JsonSchemaToPhp \Type \ArrayType ;
22
21
use OpenCodeModeling \JsonSchemaToPhp \Type \ObjectType ;
23
22
use OpenCodeModeling \JsonSchemaToPhp \Type \ReferenceType ;
24
23
use OpenCodeModeling \JsonSchemaToPhp \Type \ScalarType ;
25
24
use OpenCodeModeling \JsonSchemaToPhp \Type \TypeDefinition ;
26
25
use OpenCodeModeling \JsonSchemaToPhp \Type \TypeSet ;
27
- use PhpParser \NodeTraverser ;
28
- use PhpParser \Parser ;
29
- use PhpParser \PrettyPrinterAbstract ;
30
26
31
27
final class ClassGenerator
32
28
{
@@ -57,15 +53,15 @@ public function __construct(
57
53
58
54
/**
59
55
* @param ClassBuilder $classBuilder Main class
60
- * @param ClassBuilderCollection $classBuilderCollection Collection for other classes
56
+ * @param FileCollection $fileCollection Collection for other classes
61
57
* @param TypeSet $typeSet
62
58
* @param string $srcFolder Source folder for namespace imports
63
59
* @param string|null $className Class name is used from $classBuilder if not set
64
60
* @return void
65
61
*/
66
62
public function generateClasses (
67
63
ClassBuilder $ classBuilder ,
68
- ClassBuilderCollection $ classBuilderCollection ,
64
+ FileCollection $ fileCollection ,
69
65
TypeSet $ typeSet ,
70
66
string $ srcFolder ,
71
67
string $ className = null
@@ -103,7 +99,7 @@ public function generateClasses(
103
99
104
100
$ this ->generateClasses (
105
101
ClassBuilder::fromScratch ($ itemClassName , $ classNamespace )->setFinal (true ),
106
- $ classBuilderCollection ,
102
+ $ fileCollection ,
107
103
$ itemTypeSet ,
108
104
$ srcFolder ,
109
105
$ itemPropertyName
@@ -113,7 +109,7 @@ public function generateClasses(
113
109
case $ propertyType instanceof ObjectType:
114
110
$ this ->generateClasses (
115
111
ClassBuilder::fromScratch ($ propertyClassName , $ classNamespace )->setFinal (true ),
116
- $ classBuilderCollection ,
112
+ $ fileCollection ,
117
113
$ propertyTypeSet ,
118
114
$ srcFolder ,
119
115
$ propertyClassName
@@ -130,7 +126,7 @@ public function generateClasses(
130
126
if ($ propertyRefType = $ propertyType ->resolvedType ()) {
131
127
$ this ->generateClasses (
132
128
ClassBuilder::fromScratch ($ propertyClassName , $ classNamespace )->setFinal (true ),
133
- $ classBuilderCollection ,
129
+ $ fileCollection ,
134
130
$ propertyRefType ,
135
131
$ srcFolder ,
136
132
$ propertyType ->name ()
@@ -146,7 +142,7 @@ public function generateClasses(
146
142
$ classBuilder ->addNamespaceImport ($ classNamespace . '\\' . $ propertyClassName );
147
143
break ;
148
144
case $ propertyType instanceof ScalarType:
149
- $ classBuilderCollection ->add (
145
+ $ fileCollection ->add (
150
146
$ this ->generateValueObject ($ propertyClassName , $ classNamespace , $ propertyType )
151
147
);
152
148
$ classBuilder ->addNamespaceImport ($ classNamespace . '\\' . $ propertyClassName );
@@ -161,17 +157,17 @@ public function generateClasses(
161
157
break ;
162
158
}
163
159
}
164
- $ classBuilderCollection ->add ($ classBuilder );
160
+ $ fileCollection ->add ($ classBuilder );
165
161
break ;
166
162
case $ type instanceof ScalarType:
167
- $ classBuilderCollection ->add (
163
+ $ fileCollection ->add (
168
164
$ this ->generateValueObject (($ this ->classNameFilter )($ className ), $ classNamespace , $ type )
169
165
);
170
166
break ;
171
167
case $ type instanceof ArrayType:
172
168
$ arrayClassBuilder = $ this ->generateValueObject (($ this ->classNameFilter )($ className ), $ classNamespace , $ type );
173
169
$ this ->addNamespaceImport ($ arrayClassBuilder , $ type );
174
- $ classBuilderCollection ->add ($ arrayClassBuilder );
170
+ $ fileCollection ->add ($ arrayClassBuilder );
175
171
break ;
176
172
default :
177
173
break ;
@@ -181,16 +177,19 @@ public function generateClasses(
181
177
/**
182
178
* Generation of getter methods for value object are skipped.
183
179
*
184
- * @param ClassBuilderCollection $classBuilderCollection
180
+ * @param FileCollection $classBuilderCollection
185
181
* @param bool $typed
186
182
* @param callable $methodNameFilter Filter the property name to your desired method name e.g. with get prefix
187
183
*/
188
184
public function addGetterMethods (
189
- ClassBuilderCollection $ classBuilderCollection ,
185
+ FileCollection $ classBuilderCollection ,
190
186
bool $ typed ,
191
187
callable $ methodNameFilter
192
188
): void {
193
189
foreach ($ classBuilderCollection as $ classBuilder ) {
190
+ if (! $ classBuilder instanceof ClassBuilder) {
191
+ continue ;
192
+ }
194
193
foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
195
194
$ methodName = ($ methodNameFilter )($ classPropertyBuilder ->getName ());
196
195
@@ -211,18 +210,21 @@ public function addGetterMethods(
211
210
/**
212
211
* Generation of constants for value object are skipped.
213
212
*
214
- * @param ClassBuilderCollection $classBuilderCollection
213
+ * @param FileCollection $fileCollection
215
214
* @param callable $constantNameFilter Converts the name to a proper class constant name
216
215
* @param callable $constantValueFilter Converts the name to a proper class constant value e.g. snake_case or camelCase
217
216
* @param int $visibility Visibility of the class constant
218
217
*/
219
218
public function addClassConstantsForProperties (
220
- ClassBuilderCollection $ classBuilderCollection ,
219
+ FileCollection $ fileCollection ,
221
220
callable $ constantNameFilter ,
222
221
callable $ constantValueFilter ,
223
222
int $ visibility = ClassConstGenerator::FLAG_PUBLIC
224
223
): void {
225
- foreach ($ classBuilderCollection as $ classBuilder ) {
224
+ foreach ($ fileCollection as $ classBuilder ) {
225
+ if (! $ classBuilder instanceof ClassBuilder) {
226
+ continue ;
227
+ }
226
228
foreach ($ classBuilder ->getProperties () as $ classPropertyBuilder ) {
227
229
$ constantName = ($ constantNameFilter )($ classPropertyBuilder ->getName ());
228
230
@@ -252,50 +254,6 @@ public function generateValueObject(string $className, string $classNamespace, T
252
254
return $ classBuilder ;
253
255
}
254
256
255
- /**
256
- * @param ClassBuilderCollection $classBuilderCollection
257
- * @param Parser $parser
258
- * @param PrettyPrinterAbstract $printer
259
- * @param callable|null $currentFileAst Callable to return current file AST, if null, file will be overwritten
260
- * @return array<string, string> List of filename => code
261
- */
262
- public function generateFiles (
263
- ClassBuilderCollection $ classBuilderCollection ,
264
- Parser $ parser ,
265
- PrettyPrinterAbstract $ printer ,
266
- callable $ currentFileAst = null
267
- ): array {
268
- $ files = [];
269
-
270
- if ($ currentFileAst === null ) {
271
- $ currentFileAst = static function (ClassBuilder $ classBuilder , ClassInfo $ classInfo ) {
272
- return [];
273
- };
274
- }
275
-
276
- $ previousNamespace = '__invalid//namespace__ ' ;
277
-
278
- foreach ($ classBuilderCollection as $ classBuilder ) {
279
- if ($ previousNamespace !== $ classBuilder ->getNamespace ()) {
280
- $ previousNamespace = $ classBuilder ->getNamespace ();
281
- $ classInfo = $ this ->classInfoList ->classInfoForNamespace ($ previousNamespace );
282
- $ path = $ classInfo ->getPath ($ classBuilder ->getNamespace () . '\\' . $ classBuilder ->getName ());
283
- }
284
- // @phpstan-ignore-next-line
285
- $ filename = $ classInfo ->getFilenameFromPathAndName ($ path , $ classBuilder ->getName ());
286
-
287
- $ nodeTraverser = new NodeTraverser ();
288
- $ classBuilder ->injectVisitors ($ nodeTraverser , $ parser );
289
-
290
- $ files [$ filename ] = $ printer ->prettyPrintFile (
291
- // @phpstan-ignore-next-line
292
- $ nodeTraverser ->traverse ($ currentFileAst ($ classBuilder , $ classInfo ))
293
- );
294
- }
295
-
296
- return $ files ;
297
- }
298
-
299
257
private function addNamespaceImport (ClassBuilder $ classBuilder , TypeDefinition $ typeDefinition ): void
300
258
{
301
259
switch (true ) {
0 commit comments