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 Oct 2, 2022
1 parent b1351c6 commit c1bf5bd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/State/CreateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
}

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

return $resource;
Expand Down
33 changes: 33 additions & 0 deletions src/State/ObjectProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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
{
$refl = new \ReflectionClass($operation->getClass());

return $refl->newInstanceWithoutConstructor();
}
}
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 c1bf5bd

Please sign in to comment.