vendor/friendsofsymfony/rest-bundle/EventListener/ParamFetcherListener.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 FOS\RestBundle\Request\ParamFetcherInterface;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  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.     public function __construct(ParamFetcherInterface $paramFetcherbool $setParamsAsAttributes false)
  29.     {
  30.         $this->paramFetcher $paramFetcher;
  31.         $this->setParamsAsAttributes $setParamsAsAttributes;
  32.     }
  33.     /**
  34.      * @param ControllerEvent $event
  35.      */
  36.     public function onKernelController($event)
  37.     {
  38.         $request $event->getRequest();
  39.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  40.             return;
  41.         }
  42.         $controller $event->getController();
  43.         if (is_callable($controller) && (is_object($controller) || is_string($controller)) && method_exists($controller'__invoke')) {
  44.             $controller = [$controller'__invoke'];
  45.         }
  46.         $this->paramFetcher->setController($controller);
  47.         $attributeName $this->getAttributeName($controller);
  48.         $request->attributes->set($attributeName$this->paramFetcher);
  49.         if ($this->setParamsAsAttributes) {
  50.             $params $this->paramFetcher->all();
  51.             foreach ($params as $name => $param) {
  52.                 if ($request->attributes->has($name) && null !== $request->attributes->get($name)) {
  53.                     $msg sprintf("ParamFetcher parameter conflicts with a path parameter '$name' for route '%s'"$request->attributes->get('_route'));
  54.                     throw new \InvalidArgumentException($msg);
  55.                 }
  56.                 $request->attributes->set($name$param);
  57.             }
  58.         }
  59.     }
  60.     private function getAttributeName(callable $controller): string
  61.     {
  62.         list($object$name) = $controller;
  63.         $method = new \ReflectionMethod($object$name);
  64.         foreach ($method->getParameters() as $param) {
  65.             if ($this->isParamFetcherType($param)) {
  66.                 return $param->getName();
  67.             }
  68.         }
  69.         // If there is no typehint, inject the ParamFetcher using a default name.
  70.         return 'paramFetcher';
  71.     }
  72.     private function isParamFetcherType(\ReflectionParameter $controllerParam): bool
  73.     {
  74.         $type $controllerParam->getClass();
  75.         if (null === $type) {
  76.             return false;
  77.         }
  78.         return $type->implementsInterface(ParamFetcherInterface::class);
  79.     }
  80. }