vendor/api-platform/core/src/Symfony/EventListener/RespondListener.php line 51

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\Metadata\Exception\HttpExceptionInterface;
  14. use ApiPlatform\Metadata\IriConverterInterface;
  15. use ApiPlatform\Metadata\Put;
  16. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  17. use ApiPlatform\Metadata\UrlGeneratorInterface;
  18. use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
  19. use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Event\ViewEvent;
  22. /**
  23.  * Builds the response object.
  24.  *
  25.  * @author Kévin Dunglas <dunglas@gmail.com>
  26.  */
  27. final class RespondListener
  28. {
  29.     use OperationRequestInitiatorTrait;
  30.     public const METHOD_TO_CODE = [
  31.         'POST' => Response::HTTP_CREATED,
  32.         'DELETE' => Response::HTTP_NO_CONTENT,
  33.     ];
  34.     public function __construct(
  35.         ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory null,
  36.         private readonly IriConverterInterface|LegacyIriConverterInterface|null $iriConverter null,
  37.     ) {
  38.         $this->resourceMetadataCollectionFactory $resourceMetadataFactory;
  39.     }
  40.     /**
  41.      * Creates a Response to send to the client according to the requested format.
  42.      */
  43.     public function onKernelView(ViewEvent $event): void
  44.     {
  45.         $request $event->getRequest();
  46.         $controllerResult $event->getControllerResult();
  47.         $operation $this->initializeOperation($request);
  48.         if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
  49.             return;
  50.         }
  51.         $attributes RequestAttributesExtractor::extractAttributes($request);
  52.         if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
  53.             $event->setResponse($controllerResult);
  54.             return;
  55.         }
  56.         if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond'))) {
  57.             return;
  58.         }
  59.         $headers = [
  60.             'Content-Type' => sprintf('%s; charset=utf-8'$request->getMimeType($request->getRequestFormat())),
  61.             'Vary' => 'Accept',
  62.             'X-Content-Type-Options' => 'nosniff',
  63.             'X-Frame-Options' => 'deny',
  64.         ];
  65.         $status $operation?->getStatus();
  66.         if ($sunset $operation?->getSunset()) {
  67.             $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
  68.         }
  69.         if ($acceptPatch $operation?->getAcceptPatch()) {
  70.             $headers['Accept-Patch'] = $acceptPatch;
  71.         }
  72.         $method $request->getMethod();
  73.         if (
  74.             $this->iriConverter
  75.             && $operation
  76.             && ($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false)
  77.             && 301 === $operation->getStatus()
  78.         ) {
  79.             $status 301;
  80.             $headers['Location'] = $this->iriConverter->getIriFromResource($request->attributes->get('data'), UrlGeneratorInterface::ABS_PATH$operation);
  81.         } elseif ('PUT' === $method && !($attributes['previous_data'] ?? null) && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
  82.             $status Response::HTTP_CREATED;
  83.         }
  84.         $status ??= self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK;
  85.         if ($request->attributes->has('_api_write_item_iri')) {
  86.             $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
  87.             if ((Response::HTTP_CREATED === $status || (300 <= $status && $status 400)) && 'POST' === $method) {
  88.                 $headers['Location'] = $request->attributes->get('_api_write_item_iri');
  89.             }
  90.         }
  91.         if (($exception $request->attributes->get('data')) instanceof HttpExceptionInterface) {
  92.             $headers array_merge($headers$exception->getHeaders());
  93.         }
  94.         $event->setResponse(new Response(
  95.             $controllerResult,
  96.             $status,
  97.             $headers
  98.         ));
  99.     }
  100. }