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

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