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 <[email protected]>
  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\GetResponseForControllerResultEvent;
  17. /**
  18.  * Builds the response object.
  19.  *
  20.  * @author Kévin Dunglas <[email protected]>
  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(GetResponseForControllerResultEvent $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.         if ($request->attributes->has('_api_write_item_iri')) {
  55.             $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
  56.             if ($request->isMethod('POST')) {
  57.                 $headers['Location'] = $request->attributes->get('_api_write_item_iri');
  58.             }
  59.         }
  60.         $status null;
  61.         if ($this->resourceMetadataFactory && $attributes) {
  62.             $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  63.             if ($sunset $resourceMetadata->getOperationAttribute($attributes'sunset'nulltrue)) {
  64.                 $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
  65.             }
  66.             $headers $this->addAcceptPatchHeader($headers$attributes$resourceMetadata);
  67.             $status $resourceMetadata->getOperationAttribute($attributes'status');
  68.         }
  69.         $event->setResponse(new Response(
  70.             $controllerResult,
  71.             $status ?? self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK,
  72.             $headers
  73.         ));
  74.     }
  75.     private function addAcceptPatchHeader(array $headers, array $attributesResourceMetadata $resourceMetadata): array
  76.     {
  77.         if (!isset($attributes['item_operation_name'])) {
  78.             return $headers;
  79.         }
  80.         $patchMimeTypes = [];
  81.         foreach ($resourceMetadata->getItemOperations() as $operation) {
  82.             if ('PATCH' !== ($operation['method'] ?? '') || !isset($operation['input_formats'])) {
  83.                 continue;
  84.             }
  85.             foreach ($operation['input_formats'] as $mimeTypes) {
  86.                 foreach ($mimeTypes as $mimeType) {
  87.                     $patchMimeTypes[] = $mimeType;
  88.                 }
  89.             }
  90.             $headers['Accept-Patch'] = implode(', '$patchMimeTypes);
  91.             return $headers;
  92.         }
  93.         return $headers;
  94.     }
  95. }