-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add payload controller value resolver
- Loading branch information
1 parent
e19c9be
commit 96b854b
Showing
11 changed files
with
507 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Bridge/Symfony/Bundle/ArgumentResolver/PayloadArgumentResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?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\Core\Bridge\Symfony\Bundle\ArgumentResolver; | ||
|
||
use ApiPlatform\Core\EventListener\ToggleableDeserializationTrait; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; | ||
use ApiPlatform\Core\Util\RequestAttributesExtractor; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
final class PayloadArgumentResolver implements ArgumentValueResolverInterface | ||
{ | ||
use ToggleableDeserializationTrait; | ||
|
||
private $serializationContextBuilder; | ||
|
||
public function __construct( | ||
ResourceMetadataFactoryInterface $resourceMetadataFactory, | ||
SerializerContextBuilderInterface $serializationContextBuilder | ||
) { | ||
$this->resourceMetadataFactory = $resourceMetadataFactory; | ||
$this->serializationContextBuilder = $serializationContextBuilder; | ||
} | ||
|
||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
if ($argument->isVariadic()) { | ||
return false; | ||
} | ||
|
||
$class = $argument->getType(); | ||
|
||
if (null === $class) { | ||
return false; | ||
} | ||
|
||
if (null === $request->attributes->get('data')) { | ||
return false; | ||
} | ||
|
||
$inputClass = $this->getExpectedInputClass($request); | ||
|
||
if (null === $inputClass) { | ||
return false; | ||
} | ||
|
||
return $inputClass === $class || is_subclass_of($inputClass, $class); | ||
} | ||
|
||
public function resolve(Request $request, ArgumentMetadata $argument): \Generator | ||
{ | ||
yield $request->attributes->get('data'); | ||
} | ||
|
||
private function getExpectedInputClass(Request $request): ?string | ||
{ | ||
$attributes = RequestAttributesExtractor::extractAttributes($request); | ||
|
||
if (!$this->isRequestToDeserialize($request, $attributes)) { | ||
return null; | ||
} | ||
|
||
$context = $this->serializationContextBuilder->createFromRequest($request, false, $attributes); | ||
|
||
return $context['input'] ?? $context['resource_class']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Bridge/Symfony/Bundle/Resources/config/argument_resolver.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="api_platform.argument_resolver.payload" class="ApiPlatform\Core\Bridge\Symfony\Bundle\ArgumentResolver\PayloadArgumentResolver" public="false"> | ||
<argument type="service" id="api_platform.metadata.resource.metadata_factory" /> | ||
<argument type="service" id="api_platform.serializer.context_builder" /> | ||
|
||
<tag name="controller.argument_value_resolver" /> | ||
</service> | ||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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\Core\EventListener; | ||
|
||
use ApiPlatform\Core\Metadata\Resource\ToggleableOperationAttributeTrait; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
trait ToggleableDeserializationTrait | ||
{ | ||
use ToggleableOperationAttributeTrait; | ||
|
||
private function isRequestToDeserialize(Request $request, array $attributes): bool | ||
{ | ||
return | ||
'DELETE' !== $request->getMethod() | ||
&& !$request->isMethodSafe() | ||
&& [] !== $attributes | ||
&& $attributes['receive'] | ||
&& !$this->isOperationAttributeDisabled($attributes, DeserializeListener::OPERATION_ATTRIBUTE_KEY); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
tests/Bridge/Symfony/Bundle/ArgumentResolver/NotResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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\Core\Tests\Bridge\Symfony\Bundle\ArgumentResolver; | ||
|
||
class NotResource | ||
{ | ||
} |
Oops, something went wrong.