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

  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 function __construct(
  37.         private readonly ProcessorInterface $processor,
  38.         private readonly IriConverterInterface $iriConverter,
  39.         private readonly ResourceClassResolverInterface $resourceClassResolver,
  40.         ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null,
  41.         ?UriVariablesConverterInterface $uriVariablesConverter null,
  42.     ) {
  43.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  44.         $this->uriVariablesConverter $uriVariablesConverter;
  45.     }
  46.     /**
  47.      * Persists, updates or delete data return by the controller if applicable.
  48.      */
  49.     public function onKernelView(ViewEvent $event): void
  50.     {
  51.         $controllerResult $event->getControllerResult();
  52.         $request $event->getRequest();
  53.         $operation $this->initializeOperation($request);
  54.         if (
  55.             $controllerResult instanceof Response
  56.             || $request->isMethodSafe()
  57.             || !($attributes RequestAttributesExtractor::extractAttributes($request))
  58.         ) {
  59.             return;
  60.         }
  61.         if (!$attributes['persist'] || !($operation?->canWrite() ?? true)) {
  62.             return;
  63.         }
  64.         if (!$operation?->getProcessor()) {
  65.             return;
  66.         }
  67.         $context = [
  68.             'operation' => $operation,
  69.             'resource_class' => $attributes['resource_class'],
  70.             'previous_data' => $attributes['previous_data'] ?? null,
  71.         ];
  72.         try {
  73.             $uriVariables $this->getOperationUriVariables($operation$request->attributes->all(), $attributes['resource_class']);
  74.         } catch (InvalidIdentifierException $e) {
  75.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  76.         }
  77.         switch ($request->getMethod()) {
  78.             case 'PUT':
  79.             case 'PATCH':
  80.             case 'POST':
  81.                 $persistResult $this->processor->process($controllerResult$operation$uriVariables$context);
  82.                 if ($persistResult) {
  83.                     $controllerResult $persistResult;
  84.                     $event->setControllerResult($controllerResult);
  85.                 }
  86.                 if ($controllerResult instanceof Response) {
  87.                     break;
  88.                 }
  89.                 $outputMetadata $operation->getOutput() ?? ['class' => $attributes['resource_class']];
  90.                 $hasOutput \is_array($outputMetadata) && \array_key_exists('class'$outputMetadata) && null !== $outputMetadata['class'];
  91.                 if (!$hasOutput) {
  92.                     break;
  93.                 }
  94.                 if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
  95.                     $request->attributes->set('_api_write_item_iri'$this->iriConverter->getIriFromResource($controllerResult));
  96.                 }
  97.                 break;
  98.             case 'DELETE':
  99.                 $this->processor->process($controllerResult$operation$uriVariables$context);
  100.                 $event->setControllerResult(null);
  101.                 break;
  102.         }
  103.     }
  104. }