Skip to content

Commit 043d812

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

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/State/ObjectProvider.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
return new ($operation->getClass());
30+
}
31+
}

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

+4
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

+9-3
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)