Skip to content

Commit

Permalink
fix(symfony): add default item provider
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 30, 2022
1 parent b1351c6 commit 043d812
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/State/ObjectProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\State;

use ApiPlatform\Metadata\Operation;

/**
* An ItemProvider that just create a new object.
*
* @author Antoine Bluchet <[email protected]>
*
* @experimental
*/
final class ObjectProvider implements ProviderInterface
{
public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
{
return new ($operation->getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('api_platform.state_provider');
$container->registerForAutoconfiguration(ProcessorInterface::class)
->addTag('api_platform.state_processor');

if (!$container->has('api_platform.state.item_provider')) {
$container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
}
}

private function registerCommonConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader, array $formats, array $patchFormats, array $errorFormats): void
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Bundle/Resources/config/state.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@
</service>
<service id="ApiPlatform\State\Pagination\PaginationOptions" alias="api_platform.pagination_options" />

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

<tag name="api_platform.state_provider" />
<tag name="api_platform.state_provider" key="ApiPlatform\State\CreateProvider" />
<tag name="api_platform.state_provider" key="api_platform.state_provider.create" />
</service>
<service id="api_platform.state_provider.create" alias="ApiPlatform\State\CreateProvider" />
<service id="ApiPlatform\State\CreateProvider" alias="api_platform.state_provider.create" />

<service id="api_platform.state_provider.object" class="ApiPlatform\State\ObjectProvider">
<tag name="api_platform.state_provider" key="ApiPlatform\State\ObjectProvider" />
<tag name="api_platform.state_provider" key="api_platform.state_provider.object" />
</service>
<service id="ApiPlatform\State\ObjectProvider" alias="api_platform.state_provider.object" />
</services>
</container>

0 comments on commit 043d812

Please sign in to comment.