1313use ApiClients \Tools \OpenApiClientGenerator \Generator \WebHooks ;
1414use cebe \openapi \Reader ;
1515use cebe \openapi \spec \OpenApi ;
16+ use EventSauce \ObjectHydrator \ObjectMapperCodeGenerator ;
1617use Jawira \CaseConverter \Convert ;
18+ use League \ConstructFinder \ConstructFinder ;
19+ use PhpParser \Node ;
1720use PhpParser \PrettyPrinter \Standard ;
1821
1922final class Generator
@@ -31,16 +34,17 @@ public function generate(string $namespace, string $destinationPath)
3134 $ namespace = self ::cleanUpNamespace ($ namespace );
3235 $ codePrinter = new Standard ();
3336
34- foreach ($ this ->all ($ namespace ) as $ file ) {
35- $ fileName = $ destinationPath . DIRECTORY_SEPARATOR . str_replace ('\\' , DIRECTORY_SEPARATOR , substr ($ file ->fqcn () , strlen ($ namespace )));
37+ foreach ($ this ->all ($ namespace, $ destinationPath . DIRECTORY_SEPARATOR ) as $ file ) {
38+ $ fileName = $ destinationPath . DIRECTORY_SEPARATOR . str_replace ('\\' , DIRECTORY_SEPARATOR , substr ($ file ->fqcn , strlen ($ namespace )));
3639 @mkdir (dirname ($ fileName ), 0744 , true );
37- file_put_contents ($ fileName . '.php ' , $ codePrinter ->prettyPrintFile ([$ file ->contents ()]) . PHP_EOL );
40+ file_put_contents ($ fileName . '.php ' , ($ file ->contents instanceof Node ? $ codePrinter ->prettyPrintFile ([$ file ->contents ]) : $ file ->contents ) . PHP_EOL );
41+ include_once $ fileName . '.php ' ;
3842 }
3943 }
4044
4145 public static function className (string $ className ): string
4246 {
43- return str_replace (['{ ' , '} ' , '- ' , '$ ' , '_ ' , '+ ' ], ['Cb ' , 'Rcb ' , 'Dash ' , '_ ' , '\\' , 'Plus ' ], (new Convert ($ className ))->toPascal ()) . (self ::isKeyword (self ::basename ($ className )) ? '_ ' : '' );
47+ return str_replace (['{ ' , '} ' , '- ' , '$ ' , '_ ' , '+ ' , ' * ' , ' . ' ], ['Cb ' , 'Rcb ' , 'Dash ' , '_ ' , '\\' , 'Plus ' , ' Obelix ' , ' Dot ' ], (new Convert ($ className ))->toPascal ()) . (self ::isKeyword (self ::basename ($ className )) ? '_ ' : '' );
4448 }
4549
4650 private static function cleanUpNamespace (string $ namespace ): string
@@ -56,8 +60,9 @@ private static function cleanUpNamespace(string $namespace): string
5660 * @param string $destinationPath
5761 * @return iterable<File>
5862 */
59- private function all (string $ namespace ): iterable
63+ private function all (string $ namespace, string $ rootPath ): iterable
6064 {
65+ $ schemaClasses = [];
6166 $ schemaRegistry = new SchemaRegistry ();
6267 if (count ($ this ->spec ->components ->schemas ?? []) > 0 ) {
6368 foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
@@ -73,6 +78,7 @@ private function all(string $namespace): iterable
7378 continue ;
7479 }
7580
81+ $ schemaClasses [] = $ namespace . 'Schema/ ' . $ schemaClassName ;
7682 yield from Schema::generate (
7783 $ name ,
7884 self ::dirname ($ namespace . 'Schema/ ' . $ schemaClassName ),
@@ -134,6 +140,7 @@ private function all(string $namespace): iterable
134140 yield from Clients::generate (
135141 $ operationGroup ,
136142 self ::dirname ($ namespace . 'Operation/ ' . $ operationGroup ),
143+ $ namespace ,
137144 self ::basename ($ namespace . 'Operation/ ' . $ operationGroup ),
138145 $ operations ,
139146 );
@@ -166,10 +173,6 @@ private function all(string $namespace): iterable
166173 );
167174 }
168175
169- yield from WebHookInterface::generate (
170- self ::dirname ($ namespace . 'WebHookInterface ' ),
171- 'WebHookInterface ' ,
172- );
173176 yield from WebHooks::generate (
174177 self ::dirname ($ namespace . 'WebHooks ' ),
175178 $ namespace ,
@@ -179,6 +182,7 @@ private function all(string $namespace): iterable
179182
180183 while ($ schemaRegistry ->hasUnknownSchemas ()) {
181184 foreach ($ schemaRegistry ->unknownSchemas () as $ schema ) {
185+ $ schemaClasses [] = $ namespace . 'Schema/ ' . $ schema ['className ' ];
182186 yield from Schema::generate (
183187 $ schema ['name ' ],
184188 self ::dirname ($ namespace . 'Schema/ ' . $ schema ['className ' ]),
@@ -189,6 +193,14 @@ private function all(string $namespace): iterable
189193 );
190194 }
191195 }
196+
197+ yield new File (
198+ $ namespace . 'OptimizedHydratorMapper ' ,
199+ (new ObjectMapperCodeGenerator ())->dump (
200+ array_unique (array_filter (array_map (static fn (string $ className ): string => str_replace ('/ ' , '\\' , $ className ), $ schemaClasses ), static fn (string $ className ): bool => count ((new \ReflectionMethod ($ className , '__construct ' ))->getParameters ()) > 0 )),
201+ $ namespace . 'OptimizedHydratorMapper '
202+ )
203+ );
192204 }
193205
194206 private static function fqcn (string $ fqcn ): string
@@ -212,6 +224,6 @@ public static function basename(string $fqcn): string
212224
213225 private static function isKeyword (string $ name ): bool
214226 {
215- return in_array (strtolower ($ 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 ' , 'self ' ), false );
227+ return in_array (strtolower ($ 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 ' , 'self ' , ' parent ' , ' object ' ), false );
216228 }
217229}
0 commit comments