@@ -24,6 +24,34 @@ public function generate(string $namespace, string $destinationPath)
24
24
{
25
25
$ namespace = $ this ->cleanUpNamespace ($ namespace );
26
26
$ codePrinter = new Standard ();
27
+
28
+ foreach ($ this ->all ($ namespace ) as $ file ) {
29
+ $ fileName = $ destinationPath . DIRECTORY_SEPARATOR . str_replace ('\\' , DIRECTORY_SEPARATOR , substr ($ file ->fqcn (), strlen ($ namespace )));
30
+ @mkdir (dirname ($ fileName ), 0744 , true );
31
+ file_put_contents ($ fileName . '.php ' , $ codePrinter ->prettyPrintFile ([$ file ->contents ()]) . PHP_EOL );
32
+ }
33
+ }
34
+
35
+ private function className (string $ className ): string
36
+ {
37
+ return str_replace (['{ ' , '} ' , '- ' , '$ ' ], ['Cb ' , 'Rcb ' , 'Dash ' , '_ ' ], (new Convert ($ className ))->toPascal ());
38
+ }
39
+
40
+ private function cleanUpNamespace (string $ namespace ): string
41
+ {
42
+ $ namespace = str_replace ('/ ' , '\\' , $ namespace );
43
+ $ namespace = str_replace ('\\\\' , '\\' , $ namespace );
44
+
45
+ return $ namespace ;
46
+ }
47
+
48
+ /**
49
+ * @param string $namespace
50
+ * @param string $destinationPath
51
+ * @return iterable<File>
52
+ */
53
+ private function all (string $ namespace ): iterable
54
+ {
27
55
if (count ($ this ->spec ->components ->schemas ?? []) > 0 ) {
28
56
$ schemaClassNameMap = [];
29
57
foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
@@ -38,16 +66,14 @@ public function generate(string $namespace, string $destinationPath)
38
66
if (strlen ($ schemaClassName ) === 0 ) {
39
67
continue ;
40
68
}
41
- @mkdir (dirname ($ destinationPath . '/Schema/ ' . $ schemaClassName ), 0777 , true );
42
- file_put_contents ($ destinationPath . '/Schema/ ' . $ schemaClassName . '.php ' , $ codePrinter ->prettyPrintFile ([
43
- Schema::generate (
44
- $ name ,
45
- $ this ->cleanUpNamespace ($ namespace . dirname ('Schema/ ' . $ schemaClassName )),
46
- strrev (explode ('/ ' , strrev ($ schemaClassName ))[0 ]),
47
- $ schema ,
48
- $ schemaClassNameMap
49
- ),
50
- ]) . PHP_EOL );
69
+
70
+ yield from Schema::generate (
71
+ $ name ,
72
+ $ this ->cleanUpNamespace ($ namespace . dirname ('Schema/ ' . $ schemaClassName )),
73
+ strrev (explode ('/ ' , strrev ($ schemaClassName ))[0 ]),
74
+ $ schema ,
75
+ $ schemaClassNameMap
76
+ );
51
77
}
52
78
}
53
79
@@ -57,47 +83,31 @@ public function generate(string $namespace, string $destinationPath)
57
83
if (strlen ($ pathClassName ) === 0 ) {
58
84
continue ;
59
85
}
60
- @mkdir (dirname ($ destinationPath . '/Path/ ' . $ pathClassName ), 0777 , true );
61
- file_put_contents ($ destinationPath . '/Path/ ' . $ pathClassName . '.php ' , $ codePrinter ->prettyPrintFile ([
62
- Path::generate (
63
- $ path ,
64
- $ this ->cleanUpNamespace ($ namespace . dirname ('Path/ ' . $ pathClassName )),
65
- $ namespace ,
66
- strrev (explode ('/ ' , strrev ($ pathClassName ))[0 ]),
67
- $ pathItem
68
- ),
69
- ]) . PHP_EOL );
86
+
87
+ yield from Path::generate (
88
+ $ path ,
89
+ $ this ->cleanUpNamespace ($ namespace . dirname ('Path/ ' . $ pathClassName )),
90
+ $ namespace ,
91
+ strrev (explode ('/ ' , strrev ($ pathClassName ))[0 ]),
92
+ $ pathItem
93
+ );
94
+
70
95
foreach ($ pathItem ->getOperations () as $ method => $ operation ) {
71
96
$ operationClassName = $ this ->className ((new Convert ($ operation ->operationId ))->fromTrain ()->toPascal ());
72
97
$ operations [$ method ] = $ operationClassName ;
73
98
if (strlen ($ operationClassName ) === 0 ) {
74
99
continue ;
75
100
}
76
- @mkdir (dirname ($ destinationPath . '/Operation/ ' . $ operationClassName ), 0777 , true );
77
- file_put_contents ($ destinationPath . '/Operation/ ' . $ operationClassName . '.php ' , $ codePrinter ->prettyPrintFile ([
78
- Operation::generate (
79
- $ path ,
80
- $ method ,
81
- $ this ->cleanUpNamespace ($ namespace . dirname ('Operation/ ' . $ operationClassName )),
82
- strrev (explode ('/ ' , strrev ($ operationClassName ))[0 ]),
83
- $ operation
84
- ),
85
- ]) . PHP_EOL );
101
+
102
+ yield from Operation::generate (
103
+ $ path ,
104
+ $ method ,
105
+ $ this ->cleanUpNamespace ($ namespace . dirname ('Operation/ ' . $ operationClassName )),
106
+ strrev (explode ('/ ' , strrev ($ operationClassName ))[0 ]),
107
+ $ operation
108
+ );
86
109
}
87
110
}
88
111
}
89
112
}
90
-
91
- private function className (string $ className ): string
92
- {
93
- return str_replace (['{ ' , '} ' , '- ' , '$ ' ], ['Cb ' , 'Rcb ' , 'Dash ' , '_ ' ], (new Convert ($ className ))->toPascal ());
94
- }
95
-
96
- private function cleanUpNamespace (string $ namespace ): string
97
- {
98
- $ namespace = str_replace ('/ ' , '\\' , $ namespace );
99
- $ namespace = str_replace ('\\\\' , '\\' , $ namespace );
100
-
101
- return $ namespace ;
102
- }
103
113
}
0 commit comments