Skip to content

Commit d19bbc9

Browse files
committed
Introduce central operators object
1 parent 2fb655b commit d19bbc9

30 files changed

+368
-381
lines changed

src/Gatherer/Operation.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ public static function gather(
156156
ClassString::factory($baseNamespace, 'Operation\\' . Utils::fixKeyword($className)),
157157
ClassString::factory($baseNamespace, $classNameSanitized),
158158
ClassString::factory($baseNamespace, 'Operator\\' . Utils::fixKeyword($className)),
159+
lcfirst(
160+
str_replace(
161+
['\\'],
162+
['👷'],
163+
ClassString::factory($baseNamespace, Utils::fixKeyword($className))->relative,
164+
),
165+
),
159166
$name,
160167
(new Convert($name))->toCamel(),
161168
$group,

src/Generator/Client.php

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -158,39 +158,46 @@ public static function generate(Configuration $configuration, string $pathPrefix
158158
new Node\Name('Operations'),
159159
[
160160
new Arg(
161-
new Node\Expr\Variable('browser'),
162-
false,
163-
false,
164-
[],
165-
new Node\Identifier('browser'),
166-
),
167-
new Arg(
168-
new Node\Expr\Variable('authentication'),
169-
false,
170-
false,
171-
[],
172-
new Node\Identifier('authentication'),
173-
),
174-
new Arg(
175-
new Node\Expr\Variable('requestSchemaValidator'),
176-
false,
177-
false,
178-
[],
179-
new Node\Identifier('requestSchemaValidator'),
180-
),
181-
new Arg(
182-
new Node\Expr\Variable('responseSchemaValidator'),
183-
false,
184-
false,
185-
[],
186-
new Node\Identifier('responseSchemaValidator'),
187-
),
188-
new Arg(
189-
new Node\Expr\Variable('hydrators'),
190-
false,
191-
false,
192-
[],
193-
new Node\Identifier('hydrators'),
161+
new Node\Expr\New_(
162+
new Node\Name('Operators'),
163+
[
164+
new Arg(
165+
new Node\Expr\Variable('browser'),
166+
false,
167+
false,
168+
[],
169+
new Node\Identifier('browser'),
170+
),
171+
new Arg(
172+
new Node\Expr\Variable('authentication'),
173+
false,
174+
false,
175+
[],
176+
new Node\Identifier('authentication'),
177+
),
178+
new Arg(
179+
new Node\Expr\Variable('requestSchemaValidator'),
180+
false,
181+
false,
182+
[],
183+
new Node\Identifier('requestSchemaValidator'),
184+
),
185+
new Arg(
186+
new Node\Expr\Variable('responseSchemaValidator'),
187+
false,
188+
false,
189+
[],
190+
new Node\Identifier('responseSchemaValidator'),
191+
),
192+
new Arg(
193+
new Node\Expr\Variable('hydrators'),
194+
false,
195+
false,
196+
[],
197+
new Node\Identifier('hydrator'),
198+
),
199+
],
200+
),
194201
),
195202
],
196203
),
@@ -206,14 +213,20 @@ public static function generate(Configuration $configuration, string $pathPrefix
206213
new Node\Expr\New_(
207214
new Node\Name('WebHooks'),
208215
[
209-
new Node\Arg(new Node\Expr\PropertyFetch(
210-
new Node\Expr\Variable('this'),
211-
'requestSchemaValidator',
212-
)),
213-
new Node\Arg(new Node\Expr\PropertyFetch(
214-
new Node\Expr\Variable('this'),
215-
'hydrators',
216-
)),
216+
new Arg(
217+
new Node\Expr\Variable('requestSchemaValidator'),
218+
false,
219+
false,
220+
[],
221+
new Node\Identifier('requestSchemaValidator'),
222+
),
223+
new Arg(
224+
new Node\Expr\Variable('hydrators'),
225+
false,
226+
false,
227+
[],
228+
new Node\Identifier('hydrator'),
229+
),
217230
],
218231
),
219232
),
@@ -229,50 +242,35 @@ public static function generate(Configuration $configuration, string $pathPrefix
229242
new Node\Name('Routers'),
230243
[
231244
new Arg(
232-
new Node\Expr\PropertyFetch(
233-
new Node\Expr\Variable('this'),
234-
'browser',
235-
),
245+
new Node\Expr\Variable('browser'),
236246
false,
237247
false,
238248
[],
239249
new Node\Identifier('browser'),
240250
),
241251
new Arg(
242-
new Node\Expr\PropertyFetch(
243-
new Node\Expr\Variable('this'),
244-
'authentication',
245-
),
252+
new Node\Expr\Variable('authentication'),
246253
false,
247254
false,
248255
[],
249256
new Node\Identifier('authentication'),
250257
),
251258
new Arg(
252-
new Node\Expr\PropertyFetch(
253-
new Node\Expr\Variable('this'),
254-
'requestSchemaValidator',
255-
),
259+
new Node\Expr\Variable('requestSchemaValidator'),
256260
false,
257261
false,
258262
[],
259263
new Node\Identifier('requestSchemaValidator'),
260264
),
261265
new Arg(
262-
new Node\Expr\PropertyFetch(
263-
new Node\Expr\Variable('this'),
264-
'responseSchemaValidator',
265-
),
266+
new Node\Expr\Variable('responseSchemaValidator'),
266267
false,
267268
false,
268269
[],
269270
new Node\Identifier('responseSchemaValidator'),
270271
),
271272
new Arg(
272-
new Node\Expr\PropertyFetch(
273-
new Node\Expr\Variable('this'),
274-
'hydrators',
275-
),
273+
new Node\Expr\Variable('hydrators'),
276274
false,
277275
false,
278276
[],

src/Generator/Helper/Operation.php

Lines changed: 16 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace ApiClients\Tools\OpenApiClientGenerator\Generator\Helper;
66

77
use ApiClients\Tools\OpenApiClientGenerator\Representation;
8-
use ApiClients\Tools\OpenApiClientGenerator\Representation\Hydrator;
98
use PhpParser\Builder;
109
use PhpParser\Comment\Doc;
1110
use PhpParser\Node;
@@ -27,7 +26,6 @@
2726
use function is_string;
2827
use function str_replace;
2928
use function strpos;
30-
use function ucfirst;
3129

3230
use const PHP_EOL;
3331

@@ -61,100 +59,28 @@ public static function methodReturnType(Builder\Method $method, Representation\O
6159
);
6260
}
6361

64-
/**
65-
* @param array<string, Hydrator> $operationHydratorMap
66-
*
67-
* @return array<Node>
68-
*/
69-
public static function methodCallOperation(Representation\Operation $operation, array $operationHydratorMap): array
62+
public static function methodCallOperation(Representation\Operation $operation): Node\Stmt\Return_
7063
{
71-
return [
72-
new Node\Stmt\If_(
73-
new Node\Expr\BinaryOp\Equal(
74-
new Node\Expr\FuncCall(
75-
new Node\Name('\array_key_exists'),
76-
[
77-
new Arg(new Node\Expr\ClassConstFetch(
78-
new Node\Name($operation->operatorClassName->relative),
79-
'class',
80-
)),
81-
new Arg(new Node\Expr\PropertyFetch(
82-
new Node\Expr\Variable('this'),
83-
'operator',
84-
)),
85-
],
64+
return new Node\Stmt\Return_(
65+
new Expr\MethodCall(
66+
new Expr\MethodCall(
67+
new Node\Expr\PropertyFetch(
68+
new Node\Expr\Variable('this'),
69+
'operators',
8670
),
87-
new Node\Expr\ConstFetch(new Node\Name('false')),
71+
$operation->operatorLookUpMethod,
8872
),
73+
'call',
8974
[
90-
'stmts' => [
91-
new Node\Stmt\Expression(
92-
new Node\Expr\Assign(
93-
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
94-
new Node\Expr\Variable('this'),
95-
'operator',
96-
), new Node\Expr\ClassConstFetch(
97-
new Node\Name($operation->operatorClassName->relative),
98-
'class',
99-
)),
100-
new Node\Expr\New_(
101-
new Node\Name($operation->operatorClassName->relative),
102-
[
103-
new Arg(new Node\Expr\PropertyFetch(
104-
new Node\Expr\Variable('this'),
105-
'browser',
106-
)),
107-
new Arg(new Node\Expr\PropertyFetch(
108-
new Node\Expr\Variable('this'),
109-
'authentication',
110-
)),
111-
...(count($operation->requestBody) > 0 ? [
112-
new Arg(new Node\Expr\PropertyFetch(
113-
new Node\Expr\Variable('this'),
114-
'requestSchemaValidator',
115-
)),
116-
] : []),
117-
new Arg(new Node\Expr\PropertyFetch(
118-
new Node\Expr\Variable('this'),
119-
'responseSchemaValidator',
120-
)),
121-
new Arg(
122-
new Expr\MethodCall(
123-
new Node\Expr\PropertyFetch(
124-
new Node\Expr\Variable('this'),
125-
'hydrators',
126-
),
127-
'getObjectMapper' . ucfirst($operationHydratorMap[$operation->operationId]->methodName),
128-
),
129-
),
130-
],
131-
),
132-
),
133-
),
134-
],
75+
...(static function (array $params): iterable {
76+
foreach ($params as $param) {
77+
yield new Arg(new Node\Expr\Variable($param->name));
78+
}
79+
})($operation->parameters),
80+
...(count($operation->requestBody) > 0 ? [new Arg(new Node\Expr\Variable('params'))] : []),
13581
],
13682
),
137-
new Node\Stmt\Return_(
138-
new Expr\MethodCall(
139-
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
140-
new Node\Expr\Variable('this'),
141-
'operator',
142-
), new Node\Expr\ClassConstFetch(
143-
new Node\Name($operation->operatorClassName->relative),
144-
'class',
145-
)),
146-
'call',
147-
[
148-
...(static function (array $params): iterable {
149-
foreach ($params as $param) {
150-
yield new Arg(new Node\Expr\Variable($param->name));
151-
}
152-
})($operation->parameters),
153-
...(count($operation->requestBody) > 0 ? [new Arg(new Node\Expr\Variable('params'))] : []),
154-
],
155-
),
156-
),
157-
];
83+
);
15884
}
15985

16086
public static function getResultTypeFromOperation(Representation\Operation $operation): string

src/Generator/Operation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public static function generate(string $pathPrefix, \ApiClients\Tools\OpenApiCli
324324
new Node\Stmt\Return_(new Node\Expr\MethodCall(
325325
new Node\Expr\PropertyFetch(
326326
new Node\Expr\Variable('this'),
327-
'hydrator',
327+
'hydrators',
328328
),
329329
'hydrateObject',
330330
[
@@ -855,7 +855,7 @@ public static function generate(string $pathPrefix, \ApiClients\Tools\OpenApiCli
855855
$class->addStmt(
856856
$factory->property('responseSchemaValidator')->setType('\League\OpenAPIValidation\Schema\SchemaValidator')->makeReadonly()->makePrivate(),
857857
)->addStmt(
858-
$factory->property('hydrator')->setType($hydrator->className->relative)->makeReadonly()->makePrivate(),
858+
$factory->property('hydrators')->setType($hydrator->className->relative)->makeReadonly()->makePrivate(),
859859
);
860860

861861
$constructor->addParam(
@@ -869,14 +869,14 @@ public static function generate(string $pathPrefix, \ApiClients\Tools\OpenApiCli
869869
new Node\Expr\Variable('responseSchemaValidator'),
870870
),
871871
)->addParam(
872-
(new Param('hydrator'))->setType($hydrator->className->relative),
872+
(new Param('hydrators'))->setType($hydrator->className->relative),
873873
)->addStmt(
874874
new Node\Expr\Assign(
875875
new Node\Expr\PropertyFetch(
876876
new Node\Expr\Variable('this'),
877-
'hydrator',
877+
'hydrators',
878878
),
879-
new Node\Expr\Variable('hydrator'),
879+
new Node\Expr\Variable('hydrators'),
880880
),
881881
);
882882
}

0 commit comments

Comments
 (0)