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

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\Util\RequestAttributesExtractor;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  16. /**
  17.  * Builds the response object.
  18.  *
  19.  * @author Kévin Dunglas <dunglas@gmail.com>
  20.  */
  21. final class RespondListener
  22. {
  23.     public const METHOD_TO_CODE = [
  24.         'POST' => Response::HTTP_CREATED,
  25.         'DELETE' => Response::HTTP_NO_CONTENT,
  26.     ];
  27.     private $resourceMetadataFactory;
  28.     public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory null)
  29.     {
  30.         $this->resourceMetadataFactory $resourceMetadataFactory;
  31.     }
  32.     /**
  33.      * Creates a Response to send to the client according to the requested format.
  34.      */
  35.     public function onKernelView(GetResponseForControllerResultEvent $event): void
  36.     {
  37.         $controllerResult $event->getControllerResult();
  38.         $request $event->getRequest();
  39.         $attributes RequestAttributesExtractor::extractAttributes($request);
  40.         if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
  41.             $event->setResponse($controllerResult);
  42.             return;
  43.         }
  44.         if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond'false))) {
  45.             return;
  46.         }
  47.         $headers = [
  48.             'Content-Type' => sprintf('%s; charset=utf-8'$request->getMimeType($request->getRequestFormat())),
  49.             'Vary' => 'Accept',
  50.             'X-Content-Type-Options' => 'nosniff',
  51.             'X-Frame-Options' => 'deny',
  52.         ];
  53.         if ($request->attributes->has('_api_write_item_iri')) {
  54.             $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
  55.             if ($request->isMethod('POST')) {
  56.                 $headers['Location'] = $request->attributes->get('_api_write_item_iri');
  57.             }
  58.         }
  59.         $status null;
  60.         if ($this->resourceMetadataFactory && $attributes) {
  61.             $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  62.             if ($sunset $resourceMetadata->getOperationAttribute($attributes'sunset'nulltrue)) {
  63.                 $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
  64.             }
  65.             $status $resourceMetadata->getOperationAttribute($attributes'status');
  66.         }
  67.         $event->setResponse(new Response(
  68.             $controllerResult,
  69.             $status ?? self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK,
  70.             $headers
  71.         ));
  72.     }
  73. }