Skip to content

Commit c1bf5bd

Browse files
committed
fix(symfony): add default item provider
1 parent b1351c6 commit c1bf5bd

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

src/State/CreateProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5858
}
5959

6060
$relation = $this->decorated->provide(new Get(uriVariables: $relationUriVariables, class: $relationClass), $uriVariables);
61-
$resource = new ($operation->getClass());
61+
$refl = new \ReflectionClass($operation->getClass());
62+
$resource = $refl->newInstanceWithoutConstructor();
6263
$this->propertyAccessor->setValue($resource, $key, $relation);
6364

6465
return $resource;

src/State/ObjectProvider.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\State;
15+
16+
use ApiPlatform\Metadata\Operation;
17+
18+
/**
19+
* An ItemProvider that just create a new object.
20+
*
21+
* @author Antoine Bluchet <[email protected]>
22+
*
23+
* @experimental
24+
*/
25+
final class ObjectProvider implements ProviderInterface
26+
{
27+
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
28+
{
29+
$refl = new \ReflectionClass($operation->getClass());
30+
31+
return $refl->newInstanceWithoutConstructor();
32+
}
33+
}

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public function load(array $configs, ContainerBuilder $container): void
128128
->addTag('api_platform.state_provider');
129129
$container->registerForAutoconfiguration(ProcessorInterface::class)
130130
->addTag('api_platform.state_processor');
131+
132+
if (!$container->has('api_platform.state.item_provider')) {
133+
$container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
134+
}
131135
}
132136

133137
private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void

src/Symfony/Bundle/Resources/config/state.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@
4141
</service>
4242
<service id="ApiPlatform\State\Pagination\PaginationOptions" alias="api_platform.pagination_options" />
4343

44-
<service id="ApiPlatform\State\CreateProvider" class="ApiPlatform\State\CreateProvider">
44+
<service id="api_platform.state_provider.create" class="ApiPlatform\State\CreateProvider">
4545
<argument type="service" id="api_platform.state.item_provider" />
4646

47-
<tag name="api_platform.state_provider" />
47+
<tag name="api_platform.state_provider" key="ApiPlatform\State\CreateProvider" />
48+
<tag name="api_platform.state_provider" key="api_platform.state_provider.create" />
4849
</service>
49-
<service id="api_platform.state_provider.create" alias="ApiPlatform\State\CreateProvider" />
50+
<service id="ApiPlatform\State\CreateProvider" alias="api_platform.state_provider.create" />
5051

52+
<service id="api_platform.state_provider.object" class="ApiPlatform\State\ObjectProvider">
53+
<tag name="api_platform.state_provider" key="ApiPlatform\State\ObjectProvider" />
54+
<tag name="api_platform.state_provider" key="api_platform.state_provider.object" />
55+
</service>
56+
<service id="ApiPlatform\State\ObjectProvider" alias="api_platform.state_provider.object" />
5157
</services>
5258
</container>

0 commit comments

Comments
 (0)