28
28
use PhpCsFixer \RuleSet ;
29
29
use PhpCsFixer \Runner \Runner ;
30
30
use Psr \Log \LoggerInterface ;
31
+ use Symfony \Component \Filesystem \Filesystem ;
31
32
use Twig \Environment ;
32
33
33
34
/**
@@ -59,35 +60,16 @@ class TypesGenerator
59
60
*/
60
61
private const SCHEMA_ORG_SUPERSEDED_BY = 'schema:supersededBy ' ;
61
62
62
- /**
63
- * @var Environment
64
- */
65
- private $ twig ;
66
-
67
- /**
68
- * @var LoggerInterface
69
- */
70
- private $ logger ;
71
-
63
+ private Environment $ twig ;
64
+ private LoggerInterface $ logger ;
72
65
/**
73
66
* @var Graph[]
74
67
*/
75
- private $ graphs ;
76
-
77
- /**
78
- * @var GoodRelationsBridge
79
- */
80
- private $ goodRelationsBridge ;
81
-
82
- /**
83
- * @var array
84
- */
85
- private $ cardinalities ;
86
-
87
- /**
88
- * @var Inflector
89
- */
90
- private $ inflector ;
68
+ private array $ graphs ;
69
+ private GoodRelationsBridge $ goodRelationsBridge ;
70
+ private array $ cardinalities ;
71
+ private Inflector $ inflector ;
72
+ private Filesystem $ filesystem ;
91
73
92
74
/**
93
75
* @param Graph[] $graphs
@@ -98,13 +80,14 @@ public function __construct(Inflector $inflector, Environment $twig, LoggerInter
98
80
throw new \InvalidArgumentException ('At least one graph must be injected. ' );
99
81
}
100
82
83
+ $ this ->inflector = $ inflector ;
101
84
$ this ->twig = $ twig ;
102
85
$ this ->logger = $ logger ;
103
86
$ this ->graphs = $ graphs ;
104
87
$ this ->goodRelationsBridge = $ goodRelationsBridge ;
88
+ $ this ->filesystem = new Filesystem ();
105
89
106
90
$ this ->cardinalities = $ cardinalitiesExtractor ->extract ();
107
- $ this ->inflector = $ inflector ;
108
91
}
109
92
110
93
/**
@@ -199,7 +182,7 @@ public function generate(array $config): void
199
182
$ class ['parent ' ] = $ numberOfSupertypes ? $ type ->all ('rdfs:subClassOf ' )[0 ]->localName () : false ;
200
183
}
201
184
202
- if (isset ($ class ['parent ' ]) && isset ( $ config ['types ' ][$ class ['parent ' ]]['namespaces ' ]['class ' ])) {
185
+ if (isset ($ class ['parent ' ], $ config ['types ' ][$ class ['parent ' ]]['namespaces ' ]['class ' ])) {
203
186
$ parentNamespace = $ config ['types ' ][$ class ['parent ' ]]['namespaces ' ]['class ' ];
204
187
205
188
if ($ parentNamespace !== $ class ['namespace ' ]) {
@@ -274,7 +257,7 @@ public function generate(array $config): void
274
257
$ class ['abstract ' ] = $ config ['types ' ][$ class ['name ' ]]['abstract ' ] ?? $ class ['hasChild ' ];
275
258
276
259
// When including all properties, ignore properties already set on parent
277
- if (isset ($ config ['types ' ][$ class ['name ' ]]['allProperties ' ]) && $ config ['types ' ][$ class ['name ' ]]['allProperties ' ] && isset ( $ classes [ $ class [ ' parent ' ]]) ) {
260
+ if (isset ($ config ['types ' ][$ class ['name ' ]]['allProperties ' ], $ classes [ $ class [ ' parent ' ]] ) && $ config ['types ' ][$ class ['name ' ]]['allProperties ' ]) {
278
261
$ type = $ class ['resource ' ];
279
262
280
263
foreach ($ propertiesMap [$ type ->getUri ()] as $ property ) {
@@ -401,10 +384,7 @@ public function generate(array $config): void
401
384
}
402
385
403
386
$ classDir = $ this ->namespaceToDir ($ config , $ class ['namespace ' ]);
404
-
405
- if (!file_exists ($ classDir )) {
406
- mkdir ($ classDir , 0777 , true );
407
- }
387
+ $ this ->filesystem ->mkdir ($ classDir );
408
388
409
389
$ path = sprintf ('%s%s.php ' , $ classDir , $ className );
410
390
$ generatedFiles [] = $ path ;
@@ -419,10 +399,7 @@ public function generate(array $config): void
419
399
420
400
if (isset ($ class ['interfaceNamespace ' ])) {
421
401
$ interfaceDir = $ this ->namespaceToDir ($ config , $ class ['interfaceNamespace ' ]);
422
-
423
- if (!file_exists ($ interfaceDir )) {
424
- mkdir ($ interfaceDir , 0777 , true );
425
- }
402
+ $ this ->filesystem ->mkdir ($ interfaceDir );
426
403
427
404
$ path = sprintf ('%s%s.php ' , $ interfaceDir , $ class ['interfaceName ' ]);
428
405
$ generatedFiles [] = $ path ;
@@ -440,12 +417,10 @@ public function generate(array $config): void
440
417
}
441
418
}
442
419
443
- if (\count ( $ interfaceMappings ) > 0 && $ config ['doctrine ' ]['resolveTargetEntityConfigPath ' ]) {
420
+ if ($ config ['doctrine ' ]['resolveTargetEntityConfigPath ' ] && \count ( $ interfaceMappings ) > 0 ) {
444
421
$ file = $ config ['output ' ].'/ ' .$ config ['doctrine ' ]['resolveTargetEntityConfigPath ' ];
445
422
$ dir = \dirname ($ file );
446
- if (!file_exists ($ dir )) {
447
- mkdir ($ dir , 0777 , true );
448
- }
423
+ $ this ->filesystem ->mkdir ($ dir );
449
424
450
425
file_put_contents (
451
426
$ file ,
@@ -697,10 +672,10 @@ private function generateFieldAnnotations(array $annotationGenerators, string $c
697
672
{
698
673
$ annotations = [];
699
674
foreach ($ annotationGenerators as $ generator ) {
700
- $ annotations = array_merge ( $ annotations , $ generator ->generateFieldAnnotations ($ className , $ fieldName) );
675
+ $ annotations[] = $ generator ->generateFieldAnnotations ($ className , $ fieldName );
701
676
}
702
677
703
- return $ annotations ;
678
+ return array_merge (... $ annotations) ;
704
679
}
705
680
706
681
/**
@@ -710,10 +685,10 @@ private function generateConstantAnnotations(array $annotationGenerators, string
710
685
{
711
686
$ annotations = [];
712
687
foreach ($ annotationGenerators as $ generator ) {
713
- $ annotations = array_merge ( $ annotations , $ generator ->generateConstantAnnotations ($ className , $ constantName) );
688
+ $ annotations[] = $ generator ->generateConstantAnnotations ($ className , $ constantName );
714
689
}
715
690
716
- return $ annotations ;
691
+ return array_merge (... $ annotations) ;
717
692
}
718
693
719
694
/**
@@ -723,10 +698,10 @@ private function generateClassAnnotations(array $annotationGenerators, string $c
723
698
{
724
699
$ annotations = [];
725
700
foreach ($ annotationGenerators as $ generator ) {
726
- $ annotations = array_merge ( $ annotations , $ generator ->generateClassAnnotations ($ className) );
701
+ $ annotations[] = $ generator ->generateClassAnnotations ($ className );
727
702
}
728
703
729
- return $ annotations ;
704
+ return array_merge (... $ annotations) ;
730
705
}
731
706
732
707
/**
@@ -736,10 +711,10 @@ private function generateInterfaceAnnotations(array $annotationGenerators, strin
736
711
{
737
712
$ annotations = [];
738
713
foreach ($ annotationGenerators as $ generator ) {
739
- $ annotations = array_merge ( $ annotations , $ generator ->generateInterfaceAnnotations ($ className) );
714
+ $ annotations[] = $ generator ->generateInterfaceAnnotations ($ className );
740
715
}
741
716
742
- return $ annotations ;
717
+ return array_merge (... $ annotations) ;
743
718
}
744
719
745
720
/**
@@ -749,10 +724,10 @@ private function generateGetterAnnotations(array $annotationGenerators, string $
749
724
{
750
725
$ annotations = [];
751
726
foreach ($ annotationGenerators as $ generator ) {
752
- $ annotations = array_merge ( $ annotations , $ generator ->generateGetterAnnotations ($ className , $ fieldName) );
727
+ $ annotations[] = $ generator ->generateGetterAnnotations ($ className , $ fieldName );
753
728
}
754
729
755
- return $ annotations ;
730
+ return array_merge (... $ annotations) ;
756
731
}
757
732
758
733
/**
@@ -762,10 +737,10 @@ private function generateAdderAnnotations(array $annotationGenerators, string $c
762
737
{
763
738
$ annotations = [];
764
739
foreach ($ annotationGenerators as $ generator ) {
765
- $ annotations = array_merge ( $ annotations , $ generator ->generateAdderAnnotations ($ className , $ fieldName) );
740
+ $ annotations[] = $ generator ->generateAdderAnnotations ($ className , $ fieldName );
766
741
}
767
742
768
- return $ annotations ;
743
+ return array_merge (... $ annotations) ;
769
744
}
770
745
771
746
/**
@@ -775,10 +750,10 @@ private function generateRemoverAnnotations(array $annotationGenerators, string
775
750
{
776
751
$ annotations = [];
777
752
foreach ($ annotationGenerators as $ generator ) {
778
- $ annotations = array_merge ( $ annotations , $ generator ->generateRemoverAnnotations ($ className , $ fieldName) );
753
+ $ annotations[] = $ generator ->generateRemoverAnnotations ($ className , $ fieldName );
779
754
}
780
755
781
- return $ annotations ;
756
+ return array_merge (... $ annotations) ;
782
757
}
783
758
784
759
/**
@@ -788,10 +763,10 @@ private function generateSetterAnnotations(array $annotationGenerators, string $
788
763
{
789
764
$ annotations = [];
790
765
foreach ($ annotationGenerators as $ generator ) {
791
- $ annotations = array_merge ( $ annotations , $ generator ->generateSetterAnnotations ($ className , $ fieldName) );
766
+ $ annotations[] = $ generator ->generateSetterAnnotations ($ className , $ fieldName );
792
767
}
793
768
794
- return $ annotations ;
769
+ return array_merge (... $ annotations) ;
795
770
}
796
771
797
772
/**
@@ -825,10 +800,13 @@ private function generateClassUses(array $annotationGenerators, array $classes,
825
800
}
826
801
}
827
802
803
+ $ newUses = [];
828
804
foreach ($ annotationGenerators as $ generator ) {
829
- $ uses = array_merge ( $ uses , $ generator ->generateUses ($ className) );
805
+ $ newUses [] = $ generator ->generateUses ($ className );
830
806
}
831
807
808
+ $ uses = array_merge ($ uses , ...$ newUses );
809
+
832
810
// Order alphabetically
833
811
sort ($ uses );
834
812
@@ -844,7 +822,7 @@ private function namespaceToDir(array $config, string $namespace): string
844
822
$ namespace = substr ($ namespace , \strlen ($ prefix ));
845
823
}
846
824
847
- return sprintf ('%s/%s/ ' , $ config ['output ' ], strtr ( $ namespace , '\\' , '/ ' ));
825
+ return sprintf ('%s/%s/ ' , $ config ['output ' ], str_replace ( '\\' , '/ ' , $ namespace ));
848
826
}
849
827
850
828
/**
0 commit comments