vendor/sensio/framework-extra-bundle/EventListener/ParamConverterListener.php line 78

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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 Sensio\Bundle\FrameworkExtraBundle\EventListener;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  12. use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Event\KernelEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. /**
  18.  * The ParamConverterListener handles the ParamConverter annotation.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  */
  22. class ParamConverterListener implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var ParamConverterManager
  26.      */
  27.     private $manager;
  28.     private $autoConvert;
  29.     /**
  30.      * @var bool
  31.      */
  32.     private $isParameterTypeSupported;
  33.     /**
  34.      * @param bool $autoConvert Auto convert non-configured objects
  35.      */
  36.     public function __construct(ParamConverterManager $manager$autoConvert true)
  37.     {
  38.         $this->manager $manager;
  39.         $this->autoConvert $autoConvert;
  40.         $this->isParameterTypeSupported method_exists('ReflectionParameter''getType');
  41.     }
  42.     /**
  43.      * Modifies the ParamConverterManager instance.
  44.      */
  45.     public function onKernelController(KernelEvent $event)
  46.     {
  47.         $controller $event->getController();
  48.         $request $event->getRequest();
  49.         $configurations = [];
  50.         if ($configuration $request->attributes->get('_converters')) {
  51.             foreach (\is_array($configuration) ? $configuration : [$configuration] as $configuration) {
  52.                 $configurations[$configuration->getName()] = $configuration;
  53.             }
  54.         }
  55.         if (\is_array($controller)) {
  56.             $r = new \ReflectionMethod($controller[0], $controller[1]);
  57.         } elseif (\is_object($controller) && \is_callable([$controller'__invoke'])) {
  58.             $r = new \ReflectionMethod($controller'__invoke');
  59.         } else {
  60.             $r = new \ReflectionFunction($controller);
  61.         }
  62.         // automatically apply conversion for non-configured objects
  63.         if ($this->autoConvert) {
  64.             $configurations $this->autoConfigure($r$request$configurations);
  65.         }
  66.         $this->manager->apply($request$configurations);
  67.     }
  68.     private function autoConfigure(\ReflectionFunctionAbstract $rRequest $request$configurations)
  69.     {
  70.         foreach ($r->getParameters() as $param) {
  71.             if ($param->getClass() && $param->getClass()->isInstance($request)) {
  72.                 continue;
  73.             }
  74.             $name $param->getName();
  75.             $class $param->getClass();
  76.             $hasType $this->isParameterTypeSupported && $param->hasType();
  77.             if ($class || $hasType) {
  78.                 if (!isset($configurations[$name])) {
  79.                     $configuration = new ParamConverter([]);
  80.                     $configuration->setName($name);
  81.                     $configurations[$name] = $configuration;
  82.                 }
  83.                 if ($class && null === $configurations[$name]->getClass()) {
  84.                     $configurations[$name]->setClass($class->getName());
  85.                 }
  86.             }
  87.             if (isset($configurations[$name])) {
  88.                 $configurations[$name]->setIsOptional($param->isOptional() || $param->isDefaultValueAvailable() || $hasType && $param->getType()->allowsNull());
  89.             }
  90.         }
  91.         return $configurations;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public static function getSubscribedEvents()
  97.     {
  98.         return [
  99.             KernelEvents::CONTROLLER => 'onKernelController',
  100.         ];
  101.     }
  102. }