vendor/ezsystems/ezpublish-kernel/eZ/Bundle/EzPublishRestBundle/EventListener/RequestListener.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace eZ\Bundle\EzPublishRestBundle\EventListener;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  10. use Symfony\Component\HttpFoundation\Request;
  11. /**
  12.  * REST request listener.
  13.  *
  14.  * Flags a REST request as such using the is_rest_request attribute.
  15.  */
  16. class RequestListener implements EventSubscriberInterface
  17. {
  18.     const REST_PREFIX_PATTERN '/^\/api\/[a-zA-Z0-9-_]+\/v\d+(\.\d+)?\//';
  19.     /**
  20.      * @return array
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             // 10001 is to ensure that REST requests are tagged before CorsListener is called
  26.             KernelEvents::REQUEST => ['onKernelRequest'10001],
  27.         ];
  28.     }
  29.     /**
  30.      * If the request is a REST one, sets the is_rest_request request attribute.
  31.      *
  32.      * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
  33.      */
  34.     public function onKernelRequest(GetResponseEvent $event)
  35.     {
  36.         $isRestRequest true;
  37.         if (!$this->hasRestPrefix($event->getRequest())) {
  38.             $isRestRequest false;
  39.         }
  40.         $event->getRequest()->attributes->set('is_rest_request'$isRestRequest);
  41.     }
  42.     /**
  43.      * @param \Symfony\Component\HttpFoundation\Request $request
  44.      *
  45.      * @return bool
  46.      */
  47.     protected function hasRestPrefix(Request $request)
  48.     {
  49.         return preg_match(self::REST_PREFIX_PATTERN$request->getPathInfo());
  50.     }
  51. }