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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony framework.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  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\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  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.     protected $manager;
  28.     protected $autoConvert;
  29.     /**
  30.      * @var bool
  31.      */
  32.     private $isParameterTypeSupported;
  33.     /**
  34.      * Constructor.
  35.      *
  36.      * @param ParamConverterManager $manager     A ParamConverterManager instance
  37.      * @param bool                  $autoConvert Auto convert non-configured objects
  38.      */
  39.     public function __construct(ParamConverterManager $manager$autoConvert true)
  40.     {
  41.         $this->manager $manager;
  42.         $this->autoConvert $autoConvert;
  43.         $this->isParameterTypeSupported method_exists('ReflectionParameter''getType');
  44.     }
  45.     /**
  46.      * Modifies the ParamConverterManager instance.
  47.      *
  48.      * @param FilterControllerEvent $event A FilterControllerEvent instance
  49.      */
  50.     public function onKernelController(FilterControllerEvent $event)
  51.     {
  52.         $controller $event->getController();
  53.         $request $event->getRequest();
  54.         $configurations = array();
  55.         if ($configuration $request->attributes->get('_converters')) {
  56.             foreach (is_array($configuration) ? $configuration : array($configuration) as $configuration) {
  57.                 $configurations[$configuration->getName()] = $configuration;
  58.             }
  59.         }
  60.         if (is_array($controller)) {
  61.             $r = new \ReflectionMethod($controller[0], $controller[1]);
  62.         } elseif (is_object($controller) && is_callable($controller'__invoke')) {
  63.             $r = new \ReflectionMethod($controller'__invoke');
  64.         } else {
  65.             $r = new \ReflectionFunction($controller);
  66.         }
  67.         // automatically apply conversion for non-configured objects
  68.         if ($this->autoConvert) {
  69.             $configurations $this->autoConfigure($r$request$configurations);
  70.         }
  71.         $this->manager->apply($request$configurations);
  72.     }
  73.     private function autoConfigure(\ReflectionFunctionAbstract $rRequest $request$configurations)
  74.     {
  75.         foreach ($r->getParameters() as $param) {
  76.             if ($param->getClass() && $param->getClass()->isInstance($request)) {
  77.                 continue;
  78.             }
  79.             $name $param->getName();
  80.             $class $param->getClass();
  81.             $hasType $this->isParameterTypeSupported && $param->hasType();
  82.             if ($class || $hasType) {
  83.                 if (!isset($configurations[$name])) {
  84.                     $configuration = new ParamConverter(array());
  85.                     $configuration->setName($name);
  86.                     $configurations[$name] = $configuration;
  87.                 }
  88.                 if ($class && null === $configurations[$name]->getClass()) {
  89.                     $configurations[$name]->setClass($class->getName());
  90.                 }
  91.             }
  92.             if (isset($configurations[$name])) {
  93.                 $configurations[$name]->setIsOptional($param->isOptional() || $param->isDefaultValueAvailable() || $hasType && $param->getType()->allowsNull());
  94.             }
  95.         }
  96.         return $configurations;
  97.     }
  98.     /**
  99.      * Get subscribed events.
  100.      *
  101.      * @return array Subscribed events
  102.      */
  103.     public static function getSubscribedEvents()
  104.     {
  105.         return array(
  106.             KernelEvents::CONTROLLER => 'onKernelController',
  107.         );
  108.     }
  109. }