@@ -37,9 +37,9 @@ public function generate(string $namespace, string $destinationPath)
3737 }
3838 }
3939
40- private function className (string $ className ): string
40+ public static function className (string $ className ): string
4141 {
42- return str_replace (['{ ' , '} ' , '- ' , '$ ' , '_ ' ], ['Cb ' , 'Rcb ' , 'Dash ' , '_ ' , '\\' ], (new Convert ($ className ))->toPascal ()) . ($ this -> isKeyword ($ className ) ? '_ ' : '' );
42+ return str_replace (['{ ' , '} ' , '- ' , '$ ' , '_ ' ], ['Cb ' , 'Rcb ' , 'Dash ' , '_ ' , '\\' ], (new Convert ($ className ))->toPascal ()) . (self :: isKeyword ($ className ) ? '_ ' : '' );
4343 }
4444
4545 private function cleanUpNamespace (string $ namespace ): string
@@ -57,17 +57,17 @@ private function cleanUpNamespace(string $namespace): string
5757 */
5858 private function all (string $ namespace ): iterable
5959 {
60+ $ schemaRegistry = new SchemaRegistry ();
6061 if (count ($ this ->spec ->components ->schemas ?? []) > 0 ) {
61- $ schemaClassNameMap = [];
6262 foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
63- $ schemaClassName = $ this -> className ($ name );
63+ $ schemaClassName = self :: className ($ name );
6464 if (strlen ($ schemaClassName ) === 0 ) {
6565 continue ;
6666 }
67- $ schemaClassNameMap [ spl_object_hash ( $ schema )] = $ schemaClassName ;
67+ $ schemaRegistry -> addClassName ( $ schemaClassName , $ schema ) ;
6868 }
6969 foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
70- $ schemaClassName = $ schemaClassNameMap [ spl_object_hash ($ schema )] ;
70+ $ schemaClassName = $ schemaRegistry -> get ($ schema );
7171 if (strlen ($ schemaClassName ) === 0 ) {
7272 continue ;
7373 }
@@ -77,7 +77,7 @@ private function all(string $namespace): iterable
7777 $ this ->dirname ($ namespace . 'Schema/ ' . $ schemaClassName ),
7878 $ this ->basename ($ namespace . 'Schema/ ' . $ schemaClassName ),
7979 $ schema ,
80- $ schemaClassNameMap ,
80+ $ schemaRegistry ,
8181 $ namespace . 'Schema '
8282 );
8383 }
@@ -86,7 +86,7 @@ private function all(string $namespace): iterable
8686 $ clients = [];
8787 if (count ($ this ->spec ->paths ?? []) > 0 ) {
8888 foreach ($ this ->spec ->paths as $ path => $ pathItem ) {
89- $ pathClassName = $ this -> className ($ path );
89+ $ pathClassName = self :: className ($ path );
9090 if (strlen ($ pathClassName ) === 0 ) {
9191 continue ;
9292 }
@@ -100,7 +100,7 @@ private function all(string $namespace): iterable
100100 );
101101
102102 foreach ($ pathItem ->getOperations () as $ method => $ operation ) {
103- $ operationClassName = $ this -> className ((new Convert ($ operation ->operationId ))->fromTrain ()->toPascal ()) . '_ ' ;
103+ $ operationClassName = self :: className ((new Convert ($ operation ->operationId ))->fromTrain ()->toPascal ()) . '_ ' ;
104104 $ operations [$ method ] = $ operationClassName ;
105105 if (strlen ($ operationClassName ) === 0 ) {
106106 continue ;
@@ -113,7 +113,7 @@ private function all(string $namespace): iterable
113113 $ namespace ,
114114 $ this ->basename ($ namespace . 'Operation/ ' . $ operationClassName ),
115115 $ operation ,
116- $ schemaClassNameMap
116+ $ schemaRegistry
117117 );
118118
119119 [$ operationGroup , $ operationOperation ] = explode ('/ ' , $ operationClassName );
@@ -128,7 +128,7 @@ private function all(string $namespace): iterable
128128 }
129129 }
130130
131- yield from (function (array $ clients , string $ namespace , array $ schemaClassNameMap ): \Generator {
131+ yield from (function (array $ clients , string $ namespace , SchemaRegistry $ schemaRegistry ): \Generator {
132132 foreach ($ clients as $ operationGroup => $ operations ) {
133133 yield from Client::generate (
134134 $ operationGroup ,
@@ -141,14 +141,14 @@ private function all(string $namespace): iterable
141141 yield from Clients::generate (
142142 $ namespace ,
143143 $ clients ,
144- $ schemaClassNameMap ,
144+ $ schemaRegistry ,
145145 );
146- })($ clients , $ namespace , $ schemaClassNameMap );
146+ })($ clients , $ namespace , $ schemaRegistry );
147147
148148 if (count ($ this ->spec ->webhooks ?? []) > 0 ) {
149149 $ pathClassNameMapping = [];
150150 foreach ($ this ->spec ->webhooks as $ path => $ pathItem ) {
151- $ webHookClassName = $ this -> className ($ path );
151+ $ webHookClassName = self :: className ($ path );
152152 $ pathClassNameMapping [$ path ] = $ this ->fqcn ($ namespace . 'WebHook/ ' . $ webHookClassName );
153153 if (strlen ($ webHookClassName ) === 0 ) {
154154 continue ;
@@ -160,7 +160,7 @@ private function all(string $namespace): iterable
160160 $ namespace ,
161161 $ this ->basename ($ namespace . 'WebHook/ ' . $ webHookClassName ),
162162 $ pathItem ,
163- $ schemaClassNameMap ,
163+ $ schemaRegistry ,
164164 $ namespace
165165 );
166166 }
@@ -175,6 +175,17 @@ private function all(string $namespace): iterable
175175 $ pathClassNameMapping ,
176176 );
177177 }
178+
179+ foreach ($ schemaRegistry ->unknownSchemas () as $ schema ) {
180+ yield from Schema::generate (
181+ $ schema ['name ' ],
182+ $ this ->dirname ($ namespace . 'Schema/ ' . $ schema ['className ' ]),
183+ $ this ->basename ($ namespace . 'Schema/ ' . $ schema ['className ' ]),
184+ $ schema ['schema ' ],
185+ $ schemaRegistry ,
186+ $ namespace . 'Schema '
187+ );
188+ }
178189 }
179190
180191 private function fqcn (string $ fqcn ): string
@@ -196,7 +207,7 @@ private function basename(string $fqcn): string
196207 return $ this ->cleanUpNamespace (basename ($ fqcn ));
197208 }
198209
199- private function isKeyword (string $ name ): bool
210+ private static function isKeyword (string $ name ): bool
200211 {
201212 return in_array ($ name , array ('__halt_compiler ' , 'abstract ' , 'and ' , 'array ' , 'as ' , 'break ' , 'callable ' , 'case ' , 'catch ' , 'class ' , 'clone ' , 'const ' , 'continue ' , 'declare ' , 'default ' , 'die ' , 'do ' , 'echo ' , 'else ' , 'elseif ' , 'empty ' , 'enddeclare ' , 'endfor ' , 'endforeach ' , 'endif ' , 'endswitch ' , 'endwhile ' , 'eval ' , 'exit ' , 'extends ' , 'final ' , 'for ' , 'foreach ' , 'function ' , 'global ' , 'goto ' , 'if ' , 'implements ' , 'include ' , 'include_once ' , 'instanceof ' , 'insteadof ' , 'interface ' , 'isset ' , 'list ' , 'namespace ' , 'new ' , 'or ' , 'print ' , 'private ' , 'protected ' , 'public ' , 'require ' , 'require_once ' , 'return ' , 'static ' , 'switch ' , 'throw ' , 'trait ' , 'try ' , 'unset ' , 'use ' , 'var ' , 'while ' , 'xor ' ), false );
202213 }
0 commit comments