vendor/can/rest/src/Can/RestBundle/EventListener/ZoneMatcherListener.php line 79

Open in your IDE?
  1. <?php
  2. namespace Can\RestBundle\EventListener;
  3. use Can\RestBundle\CanRestBundle;
  4. use Can\RestBundle\Token\Generator\UUIDGenerator;
  5. use Can\RestBundle\URI\ServiceNode;
  6. use Can\RestBundle\URI\ServiceNodeStorage;
  7. use Can\RestBundle\URI\URIParserInterface;
  8. use Can\URI\Exception\MalformedURIException;
  9. use Can\URI\URI;
  10. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. use Exception;
  14. /**
  15.  * Matches REST zones.
  16.  *
  17.  * @group service
  18.  *
  19.  * @package can/rest-bundle
  20.  * @author lechecacharro <lechecacharro@gmail.com>
  21.  */
  22. class ZoneMatcherListener
  23. {
  24.     /**
  25.      * @var ServiceNodeStorage
  26.      */
  27.     private $storage;
  28.     /**
  29.      * @var URIParserInterface
  30.      */
  31.     private $parser;
  32.     /**
  33.      * @var UUIDGenerator
  34.      */
  35.     private $generator;
  36.     /**
  37.      * @var RequestMatcherInterface[]
  38.      */
  39.     private $matchers = [];
  40.     /**
  41.      * ZoneMatcherListener constructor.
  42.      *
  43.      * @param ServiceNodeStorage $storage
  44.      * @param URIParserInterface $parser
  45.      * @param UUIDGenerator      $generator
  46.      */
  47.     public function __construct(ServiceNodeStorage $storageURIParserInterface $parserUUIDGenerator $generator)
  48.     {
  49.         $this->storage $storage;
  50.         $this->parser $parser;
  51.         $this->generator $generator;
  52.     }
  53.     /**
  54.      * @param RequestMatcherInterface $matcher
  55.      */
  56.     public function addRequestMatcher(RequestMatcherInterface $matcher): void
  57.     {
  58.         $this->matchers[] = $matcher;
  59.     }
  60.     /**
  61.      * @param RequestEvent $event
  62.      *
  63.      * @throws Exception
  64.      */
  65.     public function onRequest(RequestEvent $event): void
  66.     {
  67.         if (! $event->isMasterRequest()) {
  68.             return;
  69.         }
  70.         $request $event->getRequest();
  71.         try {
  72.             $requestUri = new URI($request->getUri());
  73.         } catch (MalformedURIException $e) {
  74.             throw new BadRequestHttpException(null$e);
  75.         }
  76.         foreach ($this->matchers as $matcher) {
  77.             if ($matcher->matches($request)) {
  78.                 $node $this->parser->parse($requestUri);
  79.                 $uuid $this->generator->uuid();
  80.                 $request->attributes->set(CanRestBundle::ATTR_SERVICE_NODE$node);
  81.                 $request->attributes->set(CanRestBundle::ATTR_SERVICE_VERSION$node->getVersionNumber());
  82.                 $request->attributes->set(CanRestBundle::ATTR_SERVICE_ZONEtrue);
  83.                 $request->attributes->set(CanRestBundle::ATTR_REQUEST_UUID$uuid);
  84.                 // Populate also the service node storage, so all services
  85.                 // which require access to the current service node (which
  86.                 // use the storage) have it available from now on...
  87.                 $this->storage->setServiceNode($node);
  88.                 return;
  89.             }
  90.         }
  91.         $request->attributes->set(CanRestBundle::ATTR_SERVICE_ZONEfalse);
  92.     }
  93. }