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

Open in your IDE?
  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 as LegacyIriConverterInterface;
  13. use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
  14. use ApiPlatform\Api\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
  15. use ApiPlatform\Exception\InvalidIdentifierException;
  16. use ApiPlatform\Metadata\IriConverterInterface;
  17. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  18. use ApiPlatform\Metadata\ResourceClassResolverInterface;
  19. use ApiPlatform\Metadata\UriVariablesConverterInterface;
  20. use ApiPlatform\Metadata\Util\ClassInfoTrait;
  21. use ApiPlatform\State\ProcessorInterface;
  22. use ApiPlatform\State\UriVariablesResolverTrait;
  23. use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
  24. use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
  25. use Symfony\Component\HttpFoundation\Response;
  26. use Symfony\Component\HttpKernel\Event\ViewEvent;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. /**
  29.  * Bridges persistence and the API system.
  30.  *
  31.  * @author Kévin Dunglas <dunglas@gmail.com>
  32.  * @author Baptiste Meyer <baptiste.meyer@gmail.com>
  33.  */
  34. final class WriteListener
  35. {
  36.     use ClassInfoTrait;
  37.     use OperationRequestInitiatorTrait;
  38.     use UriVariablesResolverTrait;
  39.     public function __construct(
  40.         private readonly ProcessorInterface $processor,
  41.         private readonly LegacyIriConverterInterface|IriConverterInterface $iriConverter,
  42.         private readonly ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver,
  43.         ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory null,
  44.         LegacyUriVariablesConverterInterface|UriVariablesConverterInterface|null $uriVariablesConverter null,
  45.     ) {
  46.         $this->resourceMetadataCollectionFactory $resourceMetadataCollectionFactory;
  47.         $this->uriVariablesConverter $uriVariablesConverter;
  48.     }
  49.     /**
  50.      * Persists, updates or delete data return by the controller if applicable.
  51.      */
  52.     public function onKernelView(ViewEvent $event): void
  53.     {
  54.         $controllerResult $event->getControllerResult();
  55.         $request $event->getRequest();
  56.         $operation $this->initializeOperation($request);
  57.         // API Platform 3.2 has a MainController where everything is handled by processors/providers
  58.         if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
  59.             return;
  60.         }
  61.         if (
  62.             $controllerResult instanceof Response
  63.             || $request->isMethodSafe()
  64.             || !($attributes RequestAttributesExtractor::extractAttributes($request))
  65.         ) {
  66.             return;
  67.         }
  68.         if (!$attributes['persist'] || !($operation?->canWrite() ?? true)) {
  69.             return;
  70.         }
  71.         if (!$operation?->getProcessor()) {
  72.             return;
  73.         }
  74.         $context = [
  75.             'operation' => $operation,
  76.             'resource_class' => $attributes['resource_class'],
  77.             'previous_data' => $attributes['previous_data'] ?? null,
  78.         ];
  79.         try {
  80.             $uriVariables $this->getOperationUriVariables($operation$request->attributes->all(), $attributes['resource_class']);
  81.         } catch (InvalidIdentifierException $e) {
  82.             throw new NotFoundHttpException('Invalid identifier value or configuration.'$e);
  83.         }
  84.         switch ($request->getMethod()) {
  85.             case 'PUT':
  86.             case 'PATCH':
  87.             case 'POST':
  88.                 $persistResult $this->processor->process($controllerResult$operation$uriVariables$context);
  89.                 if ($persistResult) {
  90.                     $controllerResult $persistResult;
  91.                     $event->setControllerResult($controllerResult);
  92.                 }
  93.                 if ($controllerResult instanceof Response) {
  94.                     break;
  95.                 }
  96.                 $outputMetadata $operation->getOutput() ?? ['class' => $attributes['resource_class']];
  97.                 $hasOutput \is_array($outputMetadata) && \array_key_exists('class'$outputMetadata) && null !== $outputMetadata['class'];
  98.                 if (!$hasOutput) {
  99.                     break;
  100.                 }
  101.                 if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
  102.                     $request->attributes->set('_api_write_item_iri'$this->iriConverter->getIriFromResource($controllerResult));
  103.                 }
  104.                 break;
  105.             case 'DELETE':
  106.                 $this->processor->process($controllerResult$operation$uriVariables$context);
  107.                 $event->setControllerResult(null);
  108.                 break;
  109.         }
  110.     }
  111. }