13
13
use ApiClients \Tools \OpenApiClientGenerator \Generator \WebHooks ;
14
14
use cebe \openapi \Reader ;
15
15
use cebe \openapi \spec \OpenApi ;
16
+ use EventSauce \ObjectHydrator \ObjectMapperCodeGenerator ;
16
17
use Jawira \CaseConverter \Convert ;
18
+ use League \ConstructFinder \ConstructFinder ;
19
+ use PhpParser \Node ;
17
20
use PhpParser \PrettyPrinter \Standard ;
18
21
19
22
final class Generator
@@ -31,16 +34,17 @@ public function generate(string $namespace, string $destinationPath)
31
34
$ namespace = self ::cleanUpNamespace ($ namespace );
32
35
$ codePrinter = new Standard ();
33
36
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 )));
36
39
@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 ' ;
38
42
}
39
43
}
40
44
41
45
public static function className (string $ className ): string
42
46
{
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 )) ? '_ ' : '' );
44
48
}
45
49
46
50
private static function cleanUpNamespace (string $ namespace ): string
@@ -56,8 +60,9 @@ private static function cleanUpNamespace(string $namespace): string
56
60
* @param string $destinationPath
57
61
* @return iterable<File>
58
62
*/
59
- private function all (string $ namespace ): iterable
63
+ private function all (string $ namespace, string $ rootPath ): iterable
60
64
{
65
+ $ schemaClasses = [];
61
66
$ schemaRegistry = new SchemaRegistry ();
62
67
if (count ($ this ->spec ->components ->schemas ?? []) > 0 ) {
63
68
foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
@@ -73,6 +78,7 @@ private function all(string $namespace): iterable
73
78
continue ;
74
79
}
75
80
81
+ $ schemaClasses [] = $ namespace . 'Schema/ ' . $ schemaClassName ;
76
82
yield from Schema::generate (
77
83
$ name ,
78
84
self ::dirname ($ namespace . 'Schema/ ' . $ schemaClassName ),
@@ -134,6 +140,7 @@ private function all(string $namespace): iterable
134
140
yield from Clients::generate (
135
141
$ operationGroup ,
136
142
self ::dirname ($ namespace . 'Operation/ ' . $ operationGroup ),
143
+ $ namespace ,
137
144
self ::basename ($ namespace . 'Operation/ ' . $ operationGroup ),
138
145
$ operations ,
139
146
);
@@ -166,10 +173,6 @@ private function all(string $namespace): iterable
166
173
);
167
174
}
168
175
169
- yield from WebHookInterface::generate (
170
- self ::dirname ($ namespace . 'WebHookInterface ' ),
171
- 'WebHookInterface ' ,
172
- );
173
176
yield from WebHooks::generate (
174
177
self ::dirname ($ namespace . 'WebHooks ' ),
175
178
$ namespace ,
@@ -179,6 +182,7 @@ private function all(string $namespace): iterable
179
182
180
183
while ($ schemaRegistry ->hasUnknownSchemas ()) {
181
184
foreach ($ schemaRegistry ->unknownSchemas () as $ schema ) {
185
+ $ schemaClasses [] = $ namespace . 'Schema/ ' . $ schema ['className ' ];
182
186
yield from Schema::generate (
183
187
$ schema ['name ' ],
184
188
self ::dirname ($ namespace . 'Schema/ ' . $ schema ['className ' ]),
@@ -189,6 +193,14 @@ private function all(string $namespace): iterable
189
193
);
190
194
}
191
195
}
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
+ );
192
204
}
193
205
194
206
private static function fqcn (string $ fqcn ): string
@@ -212,6 +224,6 @@ public static function basename(string $fqcn): string
212
224
213
225
private static function isKeyword (string $ name ): bool
214
226
{
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 );
216
228
}
217
229
}
0 commit comments