@@ -37,9 +37,9 @@ public function generate(string $namespace, string $destinationPath)
37
37
}
38
38
}
39
39
40
- private function className (string $ className ): string
40
+ public static function className (string $ className ): string
41
41
{
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 ) ? '_ ' : '' );
43
43
}
44
44
45
45
private function cleanUpNamespace (string $ namespace ): string
@@ -57,17 +57,17 @@ private function cleanUpNamespace(string $namespace): string
57
57
*/
58
58
private function all (string $ namespace ): iterable
59
59
{
60
+ $ schemaRegistry = new SchemaRegistry ();
60
61
if (count ($ this ->spec ->components ->schemas ?? []) > 0 ) {
61
- $ schemaClassNameMap = [];
62
62
foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
63
- $ schemaClassName = $ this -> className ($ name );
63
+ $ schemaClassName = self :: className ($ name );
64
64
if (strlen ($ schemaClassName ) === 0 ) {
65
65
continue ;
66
66
}
67
- $ schemaClassNameMap [ spl_object_hash ( $ schema )] = $ schemaClassName ;
67
+ $ schemaRegistry -> addClassName ( $ schemaClassName , $ schema ) ;
68
68
}
69
69
foreach ($ this ->spec ->components ->schemas as $ name => $ schema ) {
70
- $ schemaClassName = $ schemaClassNameMap [ spl_object_hash ($ schema )] ;
70
+ $ schemaClassName = $ schemaRegistry -> get ($ schema );
71
71
if (strlen ($ schemaClassName ) === 0 ) {
72
72
continue ;
73
73
}
@@ -77,7 +77,7 @@ private function all(string $namespace): iterable
77
77
$ this ->dirname ($ namespace . 'Schema/ ' . $ schemaClassName ),
78
78
$ this ->basename ($ namespace . 'Schema/ ' . $ schemaClassName ),
79
79
$ schema ,
80
- $ schemaClassNameMap ,
80
+ $ schemaRegistry ,
81
81
$ namespace . 'Schema '
82
82
);
83
83
}
@@ -86,7 +86,7 @@ private function all(string $namespace): iterable
86
86
$ clients = [];
87
87
if (count ($ this ->spec ->paths ?? []) > 0 ) {
88
88
foreach ($ this ->spec ->paths as $ path => $ pathItem ) {
89
- $ pathClassName = $ this -> className ($ path );
89
+ $ pathClassName = self :: className ($ path );
90
90
if (strlen ($ pathClassName ) === 0 ) {
91
91
continue ;
92
92
}
@@ -100,7 +100,7 @@ private function all(string $namespace): iterable
100
100
);
101
101
102
102
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 ()) . '_ ' ;
104
104
$ operations [$ method ] = $ operationClassName ;
105
105
if (strlen ($ operationClassName ) === 0 ) {
106
106
continue ;
@@ -113,7 +113,7 @@ private function all(string $namespace): iterable
113
113
$ namespace ,
114
114
$ this ->basename ($ namespace . 'Operation/ ' . $ operationClassName ),
115
115
$ operation ,
116
- $ schemaClassNameMap
116
+ $ schemaRegistry
117
117
);
118
118
119
119
[$ operationGroup , $ operationOperation ] = explode ('/ ' , $ operationClassName );
@@ -128,7 +128,7 @@ private function all(string $namespace): iterable
128
128
}
129
129
}
130
130
131
- yield from (function (array $ clients , string $ namespace , array $ schemaClassNameMap ): \Generator {
131
+ yield from (function (array $ clients , string $ namespace , SchemaRegistry $ schemaRegistry ): \Generator {
132
132
foreach ($ clients as $ operationGroup => $ operations ) {
133
133
yield from Client::generate (
134
134
$ operationGroup ,
@@ -141,14 +141,14 @@ private function all(string $namespace): iterable
141
141
yield from Clients::generate (
142
142
$ namespace ,
143
143
$ clients ,
144
- $ schemaClassNameMap ,
144
+ $ schemaRegistry ,
145
145
);
146
- })($ clients , $ namespace , $ schemaClassNameMap );
146
+ })($ clients , $ namespace , $ schemaRegistry );
147
147
148
148
if (count ($ this ->spec ->webhooks ?? []) > 0 ) {
149
149
$ pathClassNameMapping = [];
150
150
foreach ($ this ->spec ->webhooks as $ path => $ pathItem ) {
151
- $ webHookClassName = $ this -> className ($ path );
151
+ $ webHookClassName = self :: className ($ path );
152
152
$ pathClassNameMapping [$ path ] = $ this ->fqcn ($ namespace . 'WebHook/ ' . $ webHookClassName );
153
153
if (strlen ($ webHookClassName ) === 0 ) {
154
154
continue ;
@@ -160,7 +160,7 @@ private function all(string $namespace): iterable
160
160
$ namespace ,
161
161
$ this ->basename ($ namespace . 'WebHook/ ' . $ webHookClassName ),
162
162
$ pathItem ,
163
- $ schemaClassNameMap ,
163
+ $ schemaRegistry ,
164
164
$ namespace
165
165
);
166
166
}
@@ -175,6 +175,17 @@ private function all(string $namespace): iterable
175
175
$ pathClassNameMapping ,
176
176
);
177
177
}
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
+ }
178
189
}
179
190
180
191
private function fqcn (string $ fqcn ): string
@@ -196,7 +207,7 @@ private function basename(string $fqcn): string
196
207
return $ this ->cleanUpNamespace (basename ($ fqcn ));
197
208
}
198
209
199
- private function isKeyword (string $ name ): bool
210
+ private static function isKeyword (string $ name ): bool
200
211
{
201
212
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 );
202
213
}
0 commit comments