@@ -235,20 +235,29 @@ public function classBuilder(TypeDefinition $typeDefinition): ClassBuilder
235
235
* @param TypeSet $typeSet
236
236
* @param string $srcFolder Source folder for namespace imports
237
237
* @param string|null $className Class name is used from $classBuilder if not set
238
+ * @param string|null $rootSrcFolder Source folder for namespace imports with root e.g. /Building
238
239
* @return void
239
240
*/
240
241
public function generateClasses (
241
242
ClassBuilder $ classBuilder ,
242
243
FileCollection $ fileCollection ,
243
244
TypeSet $ typeSet ,
244
245
string $ srcFolder ,
245
- string $ className = null
246
+ string $ className = null ,
247
+ string $ rootSrcFolder = null
246
248
): void {
247
249
$ type = $ typeSet ->first ();
248
250
251
+ if ($ rootSrcFolder === null ) {
252
+ $ rootSrcFolder = $ srcFolder ;
253
+ }
254
+
249
255
$ classInfo = $ this ->classInfoList ->classInfoForPath ($ srcFolder );
250
256
$ classNamespacePath = $ classInfo ->getClassNamespaceFromPath ($ srcFolder );
251
257
258
+ $ rootClassInfo = $ this ->classInfoList ->classInfoForPath ($ rootSrcFolder );
259
+ $ rootClassNamespacePath = $ rootClassInfo ->getClassNamespaceFromPath ($ rootSrcFolder );
260
+
252
261
if ($ type instanceof ReferenceType
253
262
&& $ refType = $ type ->resolvedType ()
254
263
) {
@@ -263,7 +272,7 @@ public function generateClasses(
263
272
$ propertyType = $ propertyTypeSet ->first ();
264
273
265
274
$ propertyClassName = ($ this ->classNameFilter )($ propertyName );
266
- $ propertyClassNamespace = $ this ->extractNamespace ($ classNamespacePath , $ propertyType );
275
+ $ propertyClassNamespace = $ this ->extractNamespace ($ classNamespacePath , $ rootClassNamespacePath , $ propertyType );
267
276
$ propertyPropertyName = ($ this ->propertyNameFilter )($ propertyName );
268
277
269
278
switch (true ) {
@@ -282,7 +291,8 @@ public function generateClasses(
282
291
$ fileCollection ,
283
292
$ itemTypeSet ,
284
293
$ srcFolder ,
285
- $ itemPropertyName
294
+ $ itemPropertyName ,
295
+ $ rootSrcFolder
286
296
);
287
297
}
288
298
// no break
@@ -292,7 +302,8 @@ public function generateClasses(
292
302
$ fileCollection ,
293
303
$ propertyTypeSet ,
294
304
$ srcFolder ,
295
- $ propertyClassName
305
+ $ propertyClassName ,
306
+ $ rootSrcFolder
296
307
);
297
308
$ this ->addNamespaceImport ($ classBuilder , $ propertyClassNamespace . '\\' . $ propertyClassName );
298
309
$ classBuilder ->addProperty (
@@ -308,14 +319,15 @@ public function generateClasses(
308
319
if ($ propertyRefTypeSet = $ propertyType ->resolvedType ()) {
309
320
$ propertyRefType = $ propertyRefTypeSet ->first ();
310
321
$ propertyRefClassName = ($ this ->classNameFilter )($ propertyRefType ->name ());
311
- $ propertyRefClassNamespace = $ this ->extractNamespace ($ classNamespacePath , $ propertyRefType );
322
+ $ propertyRefClassNamespace = $ this ->extractNamespace ($ classNamespacePath , $ rootClassNamespacePath , $ propertyRefType );
312
323
313
324
$ this ->generateClasses (
314
325
ClassBuilder::fromScratch ($ propertyRefClassName , $ propertyRefClassNamespace )->setFinal (true ),
315
326
$ fileCollection ,
316
327
$ propertyRefTypeSet ,
317
328
$ srcFolder ,
318
- $ propertyType ->name ()
329
+ $ propertyType ->name (),
330
+ $ rootSrcFolder
319
331
);
320
332
$ propertyClassName = $ propertyRefClassName ;
321
333
$ propertyType = $ propertyRefType ;
@@ -350,27 +362,30 @@ public function generateClasses(
350
362
$ fileCollection ->add (
351
363
$ this ->generateValueObject (
352
364
($ this ->classNameFilter )($ className ),
353
- $ this ->extractNamespace ($ classNamespacePath , $ type ),
365
+ $ this ->extractNamespace ($ classNamespacePath , $ rootClassNamespacePath , $ type ),
354
366
$ type
355
367
)
356
368
);
357
369
break ;
358
370
case $ type instanceof ArrayType:
359
371
$ arrayClassBuilder = $ this ->generateValueObject (
360
372
($ this ->classNameFilter )($ className ),
361
- $ this ->extractNamespace ($ classNamespacePath , $ type ),
373
+ $ this ->extractNamespace ($ classNamespacePath , $ rootClassNamespacePath , $ type ),
362
374
$ type
363
375
);
364
- $ this ->addNamespaceImportForType ($ arrayClassBuilder , $ classNamespacePath , $ type );
376
+ $ this ->addNamespaceImportForType ($ arrayClassBuilder , $ classNamespacePath , $ rootClassNamespacePath , $ type );
365
377
$ fileCollection ->add ($ arrayClassBuilder );
366
378
break ;
367
379
default :
368
380
break ;
369
381
}
370
382
}
371
383
372
- private function extractNamespace (string $ classNamespacePath , TypeDefinition $ typeDefinition ): string
373
- {
384
+ private function extractNamespace (
385
+ string $ classNamespacePath ,
386
+ string $ rootClassNamespacePath ,
387
+ TypeDefinition $ typeDefinition
388
+ ): string {
374
389
if (! $ typeDefinition instanceof CustomSupport) {
375
390
return $ classNamespacePath ;
376
391
}
@@ -380,7 +395,13 @@ private function extractNamespace(string $classNamespacePath, TypeDefinition $ty
380
395
$ namespace = $ typeDefinition ->custom ()['ns ' ] ?? '' ;
381
396
}
382
397
383
- return \trim ($ classNamespacePath . '\\' . $ namespace , '\\' );
398
+ $ namespace = \str_replace ('/ ' , '\\' , $ namespace );
399
+
400
+ return \trim (
401
+ (\strpos ($ namespace , '\\' ) === 0
402
+ ? $ rootClassNamespacePath
403
+ : $ classNamespacePath )
404
+ . '\\' . \trim ($ namespace , '\\' ), '\\' );
384
405
}
385
406
386
407
/**
@@ -450,7 +471,7 @@ private function addNamespaceImport(ClassBuilder $classBuilder, string $namespac
450
471
}
451
472
}
452
473
453
- private function addNamespaceImportForType (ClassBuilder $ classBuilder , string $ classNamespacePath , TypeDefinition $ typeDefinition ): void
474
+ private function addNamespaceImportForType (ClassBuilder $ classBuilder , string $ classNamespacePath , string $ rootClassNamespacePath , TypeDefinition $ typeDefinition ): void
454
475
{
455
476
switch (true ) {
456
477
case $ typeDefinition instanceof ArrayType:
@@ -471,7 +492,7 @@ private function addNamespaceImportForType(ClassBuilder $classBuilder, string $c
471
492
$ itemName = $ refType ->name ();
472
493
}
473
494
}
474
- $ namespace = $ this ->extractNamespace ($ classNamespacePath , $ itemType );
495
+ $ namespace = $ this ->extractNamespace ($ classNamespacePath , $ rootClassNamespacePath , $ itemType );
475
496
476
497
if ($ namespace === $ classBuilder ->getNamespace ()) {
477
498
continue ;
0 commit comments