vendor/api-platform/core/src/Symfony/EventListener/WriteListener.php line 53

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\EventListener;
  12. use ApiPlatform\Api\IriConverterInterface;
  13. use ApiPlatform\Api\ResourceClassResolverInterface;
  14. use ApiPlatform\Api\UriVariablesConverterInterface;
  15. use ApiPlatform\Exception\InvalidIdentifierException;
  16. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  17. use ApiPlatform\State\ProcessorInterface;
  18. use ApiPlatform\State\UriVariablesResolverTrait;
  19. use ApiPlatform\Util\ClassInfoTrait;
  20. use ApiPlatform\Util\OperationRequestInitiatorTrait;
  21. use ApiPlatform\Util\RequestAttributesExtractor;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\HttpKernel\Event\ViewEvent;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. /**
  26.  * Bridges persistence and the API system.
  27.  *
  28.  * @author Kévin Dunglas <dunglas@gmail.com>
  29.  * @author Baptiste Meyer <baptiste.meyer@gmail.com>
  30.  */
  31. final class WriteListener
  32. {
  33.     use ClassInfoTrait;
  34.     use OperationRequestInitiatorTrait;
  35.     use UriVariablesResolverTrait;
  36.     public const OPERATION_ATTRIBUTE_KEY 'write';
  37.     public function __construct(private readonly ProcessorInterface $processor, private readonly IriConverterInterface $iriConverter, private readonly ResourceClassResolverInterface $resourceClassResolver, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null, ?UriVariablesConverterInterface $uriVariablesConverter null)
  38.     {
  39.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  40.         $this->uriVariablesConverter $uriVariablesConverter;
  41.     }
  42.     /**
  43.      * Persists, updates or delete data return by the controller if applicable.
  44.      */
  45.     public function onKernelView(ViewEvent $event): void
  46.     {
  47.         $controllerResult $event->getControllerResult();
  48.         $request $event->getRequest();
  49.         $operation $this->initializeOperation($request);
  50.         if (
  51.             $controllerResult instanceof Response
  52.             || $request->isMethodSafe()
  53.             || !($attributes RequestAttributesExtractor::extractAttributes($request))
  54.         ) {
  55.             return;
  56.         }
  57.         if (!($operation?->canWrite() ?? true) || !$attributes['persist']) {
  58.             return;
  59.         }
  60.         if (!$operation?->getProcessor()) {
  61.             return;
  62.         }
  63.         $context = ['operation' => $operation'resource_class' => $attributes['resource_class'], 'previous_data' => $attributes['previous_data'] ?? null];
  64.         try {
  65.             $uriVariables $this->getOperationUriVariables($operation$request->attributes->all(), $attributes['resource_class']);
  66.         } catch (InvalidIdentifierException $e) {
  67.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  68.         }
  69.         switch ($request->getMethod()) {
  70.             case 'PUT':
  71.             case 'PATCH':
  72.             case 'POST':
  73.                 $persistResult $this->processor->process($controllerResult$operation$uriVariables$context);
  74.                 if ($persistResult) {
  75.                     $controllerResult $persistResult;
  76.                     $event->setControllerResult($controllerResult);
  77.                 }
  78.                 if ($controllerResult instanceof Response) {
  79.                     break;
  80.                 }
  81.                 $outputMetadata $operation->getOutput() ?? ['class' => $attributes['resource_class']];
  82.                 $hasOutput \is_array($outputMetadata) && \array_key_exists('class'$outputMetadata) && null !== $outputMetadata['class'];
  83.                 if (!$hasOutput) {
  84.                     break;
  85.                 }
  86.                 if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
  87.                     $request->attributes->set('_api_write_item_iri'$this->iriConverter->getIriFromResource($controllerResult));
  88.                 }
  89.                 break;
  90.             case 'DELETE':
  91.                 $this->processor->process($controllerResult$operation$uriVariables$context);
  92.                 $event->setControllerResult(null);
  93.                 break;
  94.         }
  95.     }
  96. }