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

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\Core\EventListener;
  12. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  13. use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
  14. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\ViewEvent;
  17. /**
  18.  * Builds the response object.
  19.  *
  20.  * @author Kévin Dunglas <dunglas@gmail.com>
  21.  */
  22. final class RespondListener
  23. {
  24.     public const METHOD_TO_CODE = [
  25.         'POST' => Response::HTTP_CREATED,
  26.         'DELETE' => Response::HTTP_NO_CONTENT,
  27.     ];
  28.     private $resourceMetadataFactory;
  29.     public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory null)
  30.     {
  31.         $this->resourceMetadataFactory $resourceMetadataFactory;
  32.     }
  33.     /**
  34.      * Creates a Response to send to the client according to the requested format.
  35.      */
  36.     public function onKernelView(ViewEvent $event): void
  37.     {
  38.         $controllerResult $event->getControllerResult();
  39.         $request $event->getRequest();
  40.         $attributes RequestAttributesExtractor::extractAttributes($request);
  41.         if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
  42.             $event->setResponse($controllerResult);
  43.             return;
  44.         }
  45.         if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond'))) {
  46.             return;
  47.         }
  48.         $headers = [
  49.             'Content-Type' => sprintf('%s; charset=utf-8'$request->getMimeType($request->getRequestFormat())),
  50.             'Vary' => 'Accept',
  51.             'X-Content-Type-Options' => 'nosniff',
  52.             'X-Frame-Options' => 'deny',
  53.         ];
  54.         $status null;
  55.         if ($this->resourceMetadataFactory && $attributes) {
  56.             $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  57.             if ($sunset $resourceMetadata->getOperationAttribute($attributes'sunset'nulltrue)) {
  58.                 $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
  59.             }
  60.             $headers $this->addAcceptPatchHeader($headers$attributes$resourceMetadata);
  61.             $status $resourceMetadata->getOperationAttribute($attributes'status');
  62.         }
  63.         $status $status ?? self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK;
  64.         if ($request->attributes->has('_api_write_item_iri')) {
  65.             $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
  66.             if ((Response::HTTP_CREATED === $status || (300 <= $status && $status 400)) && $request->isMethod('POST')) {
  67.                 $headers['Location'] = $request->attributes->get('_api_write_item_iri');
  68.             }
  69.         }
  70.         $event->setResponse(new Response(
  71.             $controllerResult,
  72.             $status,
  73.             $headers
  74.         ));
  75.     }
  76.     private function addAcceptPatchHeader(array $headers, array $attributesResourceMetadata $resourceMetadata): array
  77.     {
  78.         if (!isset($attributes['item_operation_name'])) {
  79.             return $headers;
  80.         }
  81.         $patchMimeTypes = [];
  82.         foreach ($resourceMetadata->getItemOperations() as $operation) {
  83.             if ('PATCH' !== ($operation['method'] ?? '') || !isset($operation['input_formats'])) {
  84.                 continue;
  85.             }
  86.             foreach ($operation['input_formats'] as $mimeTypes) {
  87.                 foreach ($mimeTypes as $mimeType) {
  88.                     $patchMimeTypes[] = $mimeType;
  89.                 }
  90.             }
  91.             $headers['Accept-Patch'] = implode(', '$patchMimeTypes);
  92.             return $headers;
  93.         }
  94.         return $headers;
  95.     }
  96. }