@@ -24,6 +24,34 @@ public function generate(string $namespace, string $destinationPath)
2424 {
2525 $ namespace = $ this ->cleanUpNamespace ($ namespace );
2626 $ 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+ {
2755 if (count ($ this ->spec ->components ->schemas ?? []) > 0 ) {
2856 $ schemaClassNameMap = [];
2957 foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
@@ -38,16 +66,14 @@ public function generate(string $namespace, string $destinationPath)
3866 if (strlen ($ schemaClassName ) === 0 ) {
3967 continue ;
4068 }
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+ );
5177 }
5278 }
5379
@@ -57,47 +83,31 @@ public function generate(string $namespace, string $destinationPath)
5783 if (strlen ($ pathClassName ) === 0 ) {
5884 continue ;
5985 }
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+
7095 foreach ($ pathItem ->getOperations () as $ method => $ operation ) {
7196 $ operationClassName = $ this ->className ((new Convert ($ operation ->operationId ))->fromTrain ()->toPascal ());
7297 $ operations [$ method ] = $ operationClassName ;
7398 if (strlen ($ operationClassName ) === 0 ) {
7499 continue ;
75100 }
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+ );
86109 }
87110 }
88111 }
89112 }
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- }
103113}
0 commit comments