vendor/friendsofsymfony/rest-bundle/EventListener/ZoneMatcherListener.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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. namespace FOS\RestBundle\EventListener;
  11. use FOS\RestBundle\FOSRestBundle;
  12. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. /**
  15.  * Matches FOSRest's zones.
  16.  *
  17.  * @author Florian Voutzinos <florian@voutzinos.com>
  18.  *
  19.  * @internal
  20.  */
  21. class ZoneMatcherListener
  22. {
  23.     /**
  24.      * @var RequestMatcherInterface[]
  25.      */
  26.     private $requestMatchers = [];
  27.     public function addRequestMatcher(RequestMatcherInterface $requestMatcher)
  28.     {
  29.         $this->requestMatchers[] = $requestMatcher;
  30.     }
  31.     /**
  32.      * Adds an optional "_fos_rest_zone" request attribute to be checked for existence by other listeners.
  33.      *
  34.      * @param RequestEvent $event
  35.      */
  36.     public function onKernelRequest($event)
  37.     {
  38.         $request $event->getRequest();
  39.         foreach ($this->requestMatchers as $requestMatcher) {
  40.             if ($requestMatcher->matches($request)) {
  41.                 $request->attributes->set(FOSRestBundle::ZONE_ATTRIBUTEtrue);
  42.                 return;
  43.             }
  44.         }
  45.         $request->attributes->set(FOSRestBundle::ZONE_ATTRIBUTEfalse);
  46.     }
  47. }