Skip to content

Commit 48228db

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

File tree

20 files changed

+203
-205
lines changed

20 files changed

+203
-205
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: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,20 @@ public static function generate(Configuration $configuration, string $pathPrefix
206206
new Node\Expr\New_(
207207
new Node\Name('WebHooks'),
208208
[
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-
)),
209+
new Arg(
210+
new Node\Expr\Variable('requestSchemaValidator'),
211+
false,
212+
false,
213+
[],
214+
new Node\Identifier('requestSchemaValidator'),
215+
),
216+
new Arg(
217+
new Node\Expr\Variable('hydrators'),
218+
false,
219+
false,
220+
[],
221+
new Node\Identifier('hydrator'),
222+
),
217223
],
218224
),
219225
),
@@ -229,50 +235,35 @@ public static function generate(Configuration $configuration, string $pathPrefix
229235
new Node\Name('Routers'),
230236
[
231237
new Arg(
232-
new Node\Expr\PropertyFetch(
233-
new Node\Expr\Variable('this'),
234-
'browser',
235-
),
238+
new Node\Expr\Variable('browser'),
236239
false,
237240
false,
238241
[],
239242
new Node\Identifier('browser'),
240243
),
241244
new Arg(
242-
new Node\Expr\PropertyFetch(
243-
new Node\Expr\Variable('this'),
244-
'authentication',
245-
),
245+
new Node\Expr\Variable('authentication'),
246246
false,
247247
false,
248248
[],
249249
new Node\Identifier('authentication'),
250250
),
251251
new Arg(
252-
new Node\Expr\PropertyFetch(
253-
new Node\Expr\Variable('this'),
254-
'requestSchemaValidator',
255-
),
252+
new Node\Expr\Variable('requestSchemaValidator'),
256253
false,
257254
false,
258255
[],
259256
new Node\Identifier('requestSchemaValidator'),
260257
),
261258
new Arg(
262-
new Node\Expr\PropertyFetch(
263-
new Node\Expr\Variable('this'),
264-
'responseSchemaValidator',
265-
),
259+
new Node\Expr\Variable('responseSchemaValidator'),
266260
false,
267261
false,
268262
[],
269263
new Node\Identifier('responseSchemaValidator'),
270264
),
271265
new Arg(
272-
new Node\Expr\PropertyFetch(
273-
new Node\Expr\Variable('this'),
274-
'hydrators',
275-
),
266+
new Node\Expr\Variable('hydrators'),
276267
false,
277268
false,
278269
[],

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-
],
86-
),
87-
new Node\Expr\ConstFetch(new Node\Name('false')),
88-
),
89-
[
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-
],
135-
],
136-
),
137-
new Node\Stmt\Return_(
64+
return new Node\Stmt\Return_(
65+
new Expr\MethodCall(
13866
new Expr\MethodCall(
139-
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
67+
new Node\Expr\PropertyFetch(
14068
new Node\Expr\Variable('this'),
14169
'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-
],
70+
),
71+
$operation->operatorLookUpMethod,
15572
),
73+
'call',
74+
[
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'))] : []),
81+
],
15682
),
157-
];
83+
);
15884
}
15985

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

src/Generator/Operations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static function generate(Configuration $configuration, string $pathPrefix
9292
Helper\Operation::methodSignature(
9393
$factory->method((new Convert($groupsOperation->name))->toCamel())->makePublic(),
9494
$groupsOperation,
95-
)->addStmts(Helper\Operation::methodCallOperation($groupsOperation, $operationHydratorMap)),
95+
)->addStmt(Helper\Operation::methodCallOperation($groupsOperation)),
9696
);
9797
}
9898

@@ -194,7 +194,7 @@ private static function generateOperationsGroup(string $pathPrefix, Configuratio
194194
Helper\Operation::methodSignature(
195195
$factory->method((new Convert($operation->name))->toCamel())->makePublic(),
196196
$operation,
197-
)->addStmts(Helper\Operation::methodCallOperation($operation, $operationHydratorMap)),
197+
)->addStmt(Helper\Operation::methodCallOperation($operation)),
198198
);
199199
}
200200

src/Generator/Operator.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,6 @@ public static function generate(string $pathPrefix, Operation $operation, Repres
6969
],
7070
Class_::MODIFIER_PUBLIC,
7171
),
72-
)->addStmt(
73-
new Node\Stmt\ClassConst(
74-
[
75-
new Node\Const_(
76-
'METHOD',
77-
new Node\Scalar\String_(
78-
$operation->method,
79-
),
80-
),
81-
],
82-
Class_::MODIFIER_PRIVATE,
83-
),
84-
)->addStmt(
85-
new Node\Stmt\ClassConst(
86-
[
87-
new Node\Const_(
88-
'PATH',
89-
new Node\Scalar\String_(
90-
$operation->path, // Deal with the query
91-
),
92-
),
93-
],
94-
Class_::MODIFIER_PRIVATE,
95-
),
9672
);
9773

9874
$constructor = $factory->method('__construct')->makePublic();

0 commit comments

Comments
 (0)