vendor/friendsofsymfony/rest-bundle/EventListener/ParamFetcherListener.php line 52

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 FOS\RestBundle\Request\ParamFetcherInterface;
  13. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  14. /**
  15.  * This listener handles various setup tasks related to the query fetcher.
  16.  *
  17.  * Setting the controller callable on the query fetcher
  18.  * Setting the query fetcher as a request attribute
  19.  *
  20.  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  21.  *
  22.  * @internal
  23.  */
  24. class ParamFetcherListener
  25. {
  26.     private $paramFetcher;
  27.     private $setParamsAsAttributes;
  28.     /**
  29.      * Constructor.
  30.      *
  31.      * @param ParamFetcherInterface $paramFetcher
  32.      * @param bool                  $setParamsAsAttributes
  33.      */
  34.     public function __construct(ParamFetcherInterface $paramFetcher$setParamsAsAttributes false)
  35.     {
  36.         $this->paramFetcher $paramFetcher;
  37.         $this->setParamsAsAttributes $setParamsAsAttributes;
  38.     }
  39.     /**
  40.      * Core controller handler.
  41.      *
  42.      * @param FilterControllerEvent $event
  43.      *
  44.      * @throws \InvalidArgumentException
  45.      */
  46.     public function onKernelController(FilterControllerEvent $event)
  47.     {
  48.         $request $event->getRequest();
  49.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  50.             return;
  51.         }
  52.         $controller $event->getController();
  53.         if (is_callable($controller) && method_exists($controller'__invoke')) {
  54.             $controller = [$controller'__invoke'];
  55.         }
  56.         $this->paramFetcher->setController($controller);
  57.         $attributeName $this->getAttributeName($controller);
  58.         $request->attributes->set($attributeName$this->paramFetcher);
  59.         if ($this->setParamsAsAttributes) {
  60.             $params $this->paramFetcher->all();
  61.             foreach ($params as $name => $param) {
  62.                 if ($request->attributes->has($name) && null !== $request->attributes->get($name)) {
  63.                     $msg sprintf("ParamFetcher parameter conflicts with a path parameter '$name' for route '%s'"$request->attributes->get('_route'));
  64.                     throw new \InvalidArgumentException($msg);
  65.                 }
  66.                 $request->attributes->set($name$param);
  67.             }
  68.         }
  69.     }
  70.     /**
  71.      * Determines which attribute the ParamFetcher should be injected as.
  72.      *
  73.      * @param callable $controller The controller action as an "array" callable
  74.      *
  75.      * @return string
  76.      */
  77.     private function getAttributeName(callable $controller)
  78.     {
  79.         list($object$name) = $controller;
  80.         $method = new \ReflectionMethod($object$name);
  81.         foreach ($method->getParameters() as $param) {
  82.             if ($this->isParamFetcherType($param)) {
  83.                 return $param->getName();
  84.             }
  85.         }
  86.         // If there is no typehint, inject the ParamFetcher using a default name.
  87.         return 'paramFetcher';
  88.     }
  89.     /**
  90.      * Returns true if the given controller parameter is type-hinted as
  91.      * an instance of ParamFetcher.
  92.      *
  93.      * @param \ReflectionParameter $controllerParam A parameter of the controller action
  94.      *
  95.      * @return bool
  96.      */
  97.     private function isParamFetcherType(\ReflectionParameter $controllerParam)
  98.     {
  99.         $type $controllerParam->getClass();
  100.         if (null === $type) {
  101.             return false;
  102.         }
  103.         return $type->implementsInterface(ParamFetcherInterface::class);
  104.     }
  105. }