vendor/sensio/framework-extra-bundle/EventListener/ControllerListener.php line 51

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 Doctrine\Common\Annotations\Reader;
  12. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
  16. use Doctrine\Common\Util\ClassUtils;
  17. /**
  18.  * The ControllerListener class parses annotation blocks located in
  19.  * controller classes.
  20.  *
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. class ControllerListener implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var Reader
  27.      */
  28.     protected $reader;
  29.     /**
  30.      * Constructor.
  31.      *
  32.      * @param Reader $reader An Reader instance
  33.      */
  34.     public function __construct(Reader $reader)
  35.     {
  36.         $this->reader $reader;
  37.     }
  38.     /**
  39.      * Modifies the Request object to apply configuration information found in
  40.      * controllers annotations like the template to render or HTTP caching
  41.      * configuration.
  42.      *
  43.      * @param FilterControllerEvent $event A FilterControllerEvent instance
  44.      */
  45.     public function onKernelController(FilterControllerEvent $event)
  46.     {
  47.         $controller $event->getController();
  48.         if (!is_array($controller) && method_exists($controller'__invoke')) {
  49.             $controller = array($controller'__invoke');
  50.         }
  51.         if (!is_array($controller)) {
  52.             return;
  53.         }
  54.         $className class_exists('Doctrine\Common\Util\ClassUtils') ? ClassUtils::getClass($controller[0]) : get_class($controller[0]);
  55.         $object = new \ReflectionClass($className);
  56.         $method $object->getMethod($controller[1]);
  57.         $classConfigurations $this->getConfigurations($this->reader->getClassAnnotations($object));
  58.         $methodConfigurations $this->getConfigurations($this->reader->getMethodAnnotations($method));
  59.         $configurations = array();
  60.         foreach (array_merge(array_keys($classConfigurations), array_keys($methodConfigurations)) as $key) {
  61.             if (!array_key_exists($key$classConfigurations)) {
  62.                 $configurations[$key] = $methodConfigurations[$key];
  63.             } elseif (!array_key_exists($key$methodConfigurations)) {
  64.                 $configurations[$key] = $classConfigurations[$key];
  65.             } else {
  66.                 if (is_array($classConfigurations[$key])) {
  67.                     if (!is_array($methodConfigurations[$key])) {
  68.                         throw new \UnexpectedValueException('Configurations should both be an array or both not be an array');
  69.                     }
  70.                     $configurations[$key] = array_merge($classConfigurations[$key], $methodConfigurations[$key]);
  71.                 } else {
  72.                     // method configuration overrides class configuration
  73.                     $configurations[$key] = $methodConfigurations[$key];
  74.                 }
  75.             }
  76.         }
  77.         $request $event->getRequest();
  78.         foreach ($configurations as $key => $attributes) {
  79.             $request->attributes->set($key$attributes);
  80.         }
  81.     }
  82.     protected function getConfigurations(array $annotations)
  83.     {
  84.         $configurations = array();
  85.         foreach ($annotations as $configuration) {
  86.             if ($configuration instanceof ConfigurationInterface) {
  87.                 if ($configuration->allowArray()) {
  88.                     $configurations['_'.$configuration->getAliasName()][] = $configuration;
  89.                 } elseif (!isset($configurations['_'.$configuration->getAliasName()])) {
  90.                     $configurations['_'.$configuration->getAliasName()] = $configuration;
  91.                 } else {
  92.                     throw new \LogicException(sprintf('Multiple "%s" annotations are not allowed.'$configuration->getAliasName()));
  93.                 }
  94.             }
  95.         }
  96.         return $configurations;
  97.     }
  98.     public static function getSubscribedEvents()
  99.     {
  100.         return array(
  101.             KernelEvents::CONTROLLER => 'onKernelController',
  102.         );
  103.     }
  104. }