Skip to content

Commit b221d51

Browse files
committed
Drop intermediate hydrator caches and use central object for that
1 parent 8ee97b2 commit b221d51

File tree

8 files changed

+23
-119
lines changed

8 files changed

+23
-119
lines changed

src/Generator/Client.php

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use ApiClients\Tools\OpenApiClientGenerator\PrivatePromotedPropertyAsParam;
1717
use ApiClients\Tools\OpenApiClientGenerator\Representation;
1818
use ApiClients\Tools\OpenApiClientGenerator\Utils;
19-
use EventSauce\ObjectHydrator\ObjectMapper;
2019
use Jawira\CaseConverter\Convert;
2120
use NumberToWords\NumberToWords;
2221
use PhpParser\Builder\Method;
@@ -903,49 +902,6 @@ private static function callOperation(Routers $routers, Representation\Operation
903902
}
904903
})($operation->parameters),
905904
] : []),
906-
/** @phpstan-ignore-next-line */
907-
...(count(array_filter((new ReflectionClass($operation->className->fullyQualified->source))->getConstructor()->getParameters(), static fn (ReflectionParameter $parameter): bool => $parameter->name === 'responseSchemaValidator' || $parameter->name === 'hydrator')) > 0 ? [
908-
new Node\Stmt\If_(
909-
new Node\Expr\BinaryOp\Equal(
910-
new Node\Expr\FuncCall(
911-
new Node\Name('\array_key_exists'),
912-
[
913-
new Arg(new Node\Expr\ClassConstFetch(
914-
new Node\Name($path->hydrator->className->relative),
915-
'class',
916-
)),
917-
new Arg(new Node\Expr\PropertyFetch(
918-
new Node\Expr\Variable('this'),
919-
'hydrator',
920-
)),
921-
],
922-
),
923-
new Node\Expr\ConstFetch(new Node\Name('false')),
924-
),
925-
[
926-
'stmts' => [
927-
new Node\Stmt\Expression(
928-
new Node\Expr\Assign(
929-
new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
930-
new Node\Expr\Variable('this'),
931-
'hydrator',
932-
), new Node\Expr\ClassConstFetch(
933-
new Node\Name($path->hydrator->className->relative),
934-
'class',
935-
)),
936-
new Node\Expr\MethodCall(
937-
new Node\Expr\PropertyFetch(
938-
new Node\Expr\Variable('this'),
939-
'hydrators',
940-
),
941-
'getObjectMapper' . ucfirst($path->hydrator->methodName),
942-
),
943-
),
944-
),
945-
],
946-
],
947-
),
948-
] : []),
949905
...($operation->matchMethod !== 'LIST' ? self::makeCall(
950906
$operation,
951907
$path,
@@ -1115,13 +1071,15 @@ private static function makeCall(Representation\Operation $operation, Representa
11151071
new Node\Expr\Variable('this'),
11161072
'responseSchemaValidator',
11171073
)),
1118-
new Arg(new Node\Expr\ArrayDimFetch(new Node\Expr\PropertyFetch(
1119-
new Node\Expr\Variable('this'),
1120-
'hydrator',
1121-
), new Node\Expr\ClassConstFetch(
1122-
new Node\Name($path->hydrator->className->relative),
1123-
'class',
1124-
))),
1074+
new Arg(
1075+
new Node\Expr\MethodCall(
1076+
new Node\Expr\PropertyFetch(
1077+
new Node\Expr\Variable('this'),
1078+
'hydrators',
1079+
),
1080+
'getObjectMapper' . ucfirst($path->hydrator->methodName),
1081+
),
1082+
),
11251083
] : []),
11261084
],
11271085
),
@@ -1150,12 +1108,6 @@ private static function createRouter(string $pathPrefix, string $namespace, Rout
11501108
$factory = new BuilderFactory();
11511109
$stmt = $factory->namespace(Utils::dirname($namespace . $className));
11521110
$class = $factory->class(Utils::basename($namespace . $className))->makeFinal()->addStmt(
1153-
$factory->property('hydrator')->setType('array')->setDefault([])->makePrivate()->setDocComment(new Doc(implode(PHP_EOL, [
1154-
'/**',
1155-
' * @var array<class-string, \\' . ObjectMapper::class . '>',
1156-
' */',
1157-
]))),
1158-
)->addStmt(
11591111
$constructor,
11601112
);
11611113

tests/app/etc/openapi-client-generator.state

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,23 @@
208208
},
209209
{
210210
"name": ".\/tests\/app\/src\/\/Router\/Get\/Pets.php",
211-
"hash": "68179fe011002d1b456b8f3598380a67"
211+
"hash": "51682197401f5f61739d1ae126d04e81"
212212
},
213213
{
214214
"name": ".\/tests\/app\/src\/\/Router\/Get\/PetsList.php",
215-
"hash": "61aa5045f5b53d6edfd8eded49fcde80"
215+
"hash": "789dd8b770f3725e3d00073322705e19"
216216
},
217217
{
218218
"name": ".\/tests\/app\/src\/\/Router\/List\/Pets.php",
219-
"hash": "5bc8638e735e4e3a16359b144b03b640"
219+
"hash": "2275eaa5b79fe7e404e44307c7bd611e"
220220
},
221221
{
222222
"name": ".\/tests\/app\/src\/\/Router\/List\/PetsList.php",
223-
"hash": "2e4dc5732c808e2d2047203c6f881c05"
223+
"hash": "24686950c441b8eda04aebbe3b2ffd24"
224224
},
225225
{
226226
"name": ".\/tests\/app\/src\/\/Router\/Post\/Pets.php",
227-
"hash": "e25d1cae2ac66fc5c4081ac3655fb413"
227+
"hash": "4f12671bfe69bab76001414bef941837"
228228
},
229229
{
230230
"name": ".\/tests\/app\/src\/\/Router\/Get\/Two.php",
@@ -284,7 +284,7 @@
284284
},
285285
{
286286
"name": ".\/tests\/app\/src\/\/Router\/Get.php",
287-
"hash": "64270922a47a8ea29f38370be61608bc"
287+
"hash": "0c8b86bc66e7540692ee4266e1c2ea95"
288288
}
289289
]
290290
},

tests/app/src/Router/Get.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use ApiClients\Contracts;
1616
final class Get
1717
{
18-
/**
19-
* @var array<class-string, \EventSauce\ObjectHydrator\ObjectMapper>
20-
*/
21-
private array $hydrator = array();
2218
public function __construct(private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators, private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication)
2319
{
2420
}
@@ -27,10 +23,7 @@ public function __construct(private \League\OpenAPIValidation\Schema\SchemaValid
2723
*/
2824
public function showPetById(array $params) : \ApiClients\Client\PetStore\Schema\Cat|\ApiClients\Client\PetStore\Schema\Dog|\ApiClients\Client\PetStore\Schema\Bird|\ApiClients\Client\PetStore\Schema\Fish|array
2925
{
30-
if (\array_key_exists(Hydrator\Operation\Pets\PetId::class, $this->hydrator) == false) {
31-
$this->hydrator[Hydrator\Operation\Pets\PetId::class] = $this->hydrators->getObjectMapperOperation🌀Pets🌀PetId();
32-
}
33-
$operator = new Operator\ShowPetById($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets\PetId::class]);
26+
$operator = new Operator\ShowPetById($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀PetId());
3427
return $operator->call();
3528
}
3629
}

tests/app/src/Router/Get/Pets.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use ApiClients\Contracts;
1616
final class Pets
1717
{
18-
/**
19-
* @var array<class-string, \EventSauce\ObjectHydrator\ObjectMapper>
20-
*/
21-
private array $hydrator = array();
2218
public function __construct(private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators, private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication)
2319
{
2420
}
@@ -38,10 +34,7 @@ public function list(array $params) : iterable
3834
}
3935
$arguments['page'] = $params['page'];
4036
unset($params['page']);
41-
if (\array_key_exists(Hydrator\Operation\Pets::class, $this->hydrator) == false) {
42-
$this->hydrator[Hydrator\Operation\Pets::class] = $this->hydrators->getObjectMapperOperation🌀Pets();
43-
}
44-
$operator = new Operator\Pets\List_($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets::class]);
37+
$operator = new Operator\Pets\List_($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets());
4538
return $operator->call($arguments['per_page'], $arguments['page']);
4639
}
4740
/**
@@ -60,10 +53,7 @@ public function names(array $params) : iterable
6053
}
6154
$arguments['page'] = $params['page'];
6255
unset($params['page']);
63-
if (\array_key_exists(Hydrator\Operation\Pets\Names::class, $this->hydrator) == false) {
64-
$this->hydrator[Hydrator\Operation\Pets\Names::class] = $this->hydrators->getObjectMapperOperation🌀Pets🌀Names();
65-
}
66-
$operator = new Operator\Pets\Names($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets\Names::class]);
56+
$operator = new Operator\Pets\Names($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀Names());
6757
return $operator->call($arguments['per_page'], $arguments['page']);
6858
}
6959
}

tests/app/src/Router/Get/PetsList.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use ApiClients\Contracts;
1616
final class PetsList
1717
{
18-
/**
19-
* @var array<class-string, \EventSauce\ObjectHydrator\ObjectMapper>
20-
*/
21-
private array $hydrator = array();
2218
public function __construct(private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators, private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication)
2319
{
2420
}
@@ -38,10 +34,7 @@ public function gatos(array $params) : iterable
3834
}
3935
$arguments['page'] = $params['page'];
4036
unset($params['page']);
41-
if (\array_key_exists(Hydrator\Operation\Pets\Gatos::class, $this->hydrator) == false) {
42-
$this->hydrator[Hydrator\Operation\Pets\Gatos::class] = $this->hydrators->getObjectMapperOperation🌀Pets🌀Gatos();
43-
}
44-
$operator = new Operator\Pets\List_\Gatos($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets\Gatos::class]);
37+
$operator = new Operator\Pets\List_\Gatos($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀Gatos());
4538
return $operator->call($arguments['per_page'], $arguments['page']);
4639
}
4740
}

tests/app/src/Router/List/Pets.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use ApiClients\Contracts;
1616
final class Pets
1717
{
18-
/**
19-
* @var array<class-string, \EventSauce\ObjectHydrator\ObjectMapper>
20-
*/
21-
private array $hydrator = array();
2218
public function __construct(private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators, private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication)
2319
{
2420
}
@@ -38,12 +34,9 @@ public function listListing(array $params) : iterable
3834
}
3935
$arguments['page'] = $params['page'];
4036
unset($params['page']);
41-
if (\array_key_exists(Hydrator\Operation\Pets::class, $this->hydrator) == false) {
42-
$this->hydrator[Hydrator\Operation\Pets::class] = $this->hydrators->getObjectMapperOperation🌀Pets();
43-
}
4437
$arguments['page'] = 1;
4538
do {
46-
$operator = new Operator\Pets\ListListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets::class]);
39+
$operator = new Operator\Pets\ListListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets());
4740
$items = $operator->call($arguments['per_page'], $arguments['page']);
4841
yield from $items;
4942
$arguments['page']++;
@@ -65,12 +58,9 @@ public function namesListing(array $params) : iterable
6558
}
6659
$arguments['page'] = $params['page'];
6760
unset($params['page']);
68-
if (\array_key_exists(Hydrator\Operation\Pets\Names::class, $this->hydrator) == false) {
69-
$this->hydrator[Hydrator\Operation\Pets\Names::class] = $this->hydrators->getObjectMapperOperation🌀Pets🌀Names();
70-
}
7161
$arguments['page'] = 1;
7262
do {
73-
$operator = new Operator\Pets\NamesListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets\Names::class]);
63+
$operator = new Operator\Pets\NamesListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀Names());
7464
$items = $operator->call($arguments['per_page'], $arguments['page']);
7565
yield from $items;
7666
$arguments['page']++;

tests/app/src/Router/List/PetsList.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use ApiClients\Contracts;
1616
final class PetsList
1717
{
18-
/**
19-
* @var array<class-string, \EventSauce\ObjectHydrator\ObjectMapper>
20-
*/
21-
private array $hydrator = array();
2218
public function __construct(private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators, private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication)
2319
{
2420
}
@@ -38,12 +34,9 @@ public function gatosListing(array $params) : iterable
3834
}
3935
$arguments['page'] = $params['page'];
4036
unset($params['page']);
41-
if (\array_key_exists(Hydrator\Operation\Pets\Gatos::class, $this->hydrator) == false) {
42-
$this->hydrator[Hydrator\Operation\Pets\Gatos::class] = $this->hydrators->getObjectMapperOperation🌀Pets🌀Gatos();
43-
}
4437
$arguments['page'] = 1;
4538
do {
46-
$operator = new Operator\Pets\List_\GatosListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets\Gatos::class]);
39+
$operator = new Operator\Pets\List_\GatosListing($this->browser, $this->authentication, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets🌀Gatos());
4740
$items = $operator->call($arguments['per_page'], $arguments['page']);
4841
yield from $items;
4942
$arguments['page']++;

tests/app/src/Router/Post/Pets.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
use ApiClients\Contracts;
1616
final class Pets
1717
{
18-
/**
19-
* @var array<class-string, \EventSauce\ObjectHydrator\ObjectMapper>
20-
*/
21-
private array $hydrator = array();
2218
public function __construct(private \League\OpenAPIValidation\Schema\SchemaValidator $requestSchemaValidator, private \League\OpenAPIValidation\Schema\SchemaValidator $responseSchemaValidator, private \ApiClients\Client\PetStore\Hydrators $hydrators, private \React\Http\Browser $browser, private \ApiClients\Contracts\HTTP\Headers\AuthenticationInterface $authentication)
2319
{
2420
}
@@ -27,10 +23,7 @@ public function __construct(private \League\OpenAPIValidation\Schema\SchemaValid
2723
*/
2824
public function create(array $params) : array
2925
{
30-
if (\array_key_exists(Hydrator\Operation\Pets::class, $this->hydrator) == false) {
31-
$this->hydrator[Hydrator\Operation\Pets::class] = $this->hydrators->getObjectMapperOperation🌀Pets();
32-
}
33-
$operator = new Operator\Pets\Create($this->browser, $this->authentication, $this->requestSchemaValidator, $this->responseSchemaValidator, $this->hydrator[Hydrator\Operation\Pets::class]);
26+
$operator = new Operator\Pets\Create($this->browser, $this->authentication, $this->requestSchemaValidator, $this->responseSchemaValidator, $this->hydrators->getObjectMapperOperation🌀Pets());
3427
return $operator->call($params);
3528
}
3629
}

0 commit comments

Comments
 (0)