vendor/friendsofsymfony/rest-bundle/FOS/RestBundle/EventListener/ParamFetcherListener.php line 49

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