lib/boab/cms-bundle/src/View/EventListener/TemplateAnnotationListener.php line 23

Open in your IDE?
  1. <?php 
  2. namespace Boab\CmsBundle\View\EventListener;
  3. use Doctrine\Common\Annotations\Reader;
  4. use Doctrine\Common\Annotations\AnnotationReader;
  5. use Doctrine\Common\Util\ClassUtils;
  6. use Boab\CmsBundle\View\Annotation\Template;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Boab\CmsBundle\View\ThemeManagerInterface;
  9. class TemplateAnnotationListener
  10. {
  11.     private $reader;
  12.     private $themeManager;
  13.     public function __construct(Reader $readerThemeManagerInterface $themeManager)
  14.     {
  15.         $this->reader $reader;
  16.         $this->themeManager $themeManager;
  17.     }
  18.     public function onKernelController(ControllerEvent $event)
  19.     {
  20.         $controller $event->getController();
  21.         if (!is_array($controller)) {
  22.             return;
  23.         }
  24.         list($controllerObject$methodName) = $controller;
  25.  
  26.         // Using ClassUtils::getClass in case the controller is an proxy
  27.         $controllerClass ClassUtils::getClass($controllerObject);
  28.         $classAnnotation $this->reader->getClassAnnotation(new \ReflectionClass($controllerClass), Template::class);
  29.         // Get method annotation
  30.         $controllerReflectionObject = new \ReflectionObject($controllerObject);
  31.         $reflectionMethod $controllerReflectionObject->getMethod($methodName);
  32.         $methodAnnotation $this->reader->getMethodAnnotation($reflectionMethod,Template::class);
  33.         $this->setAnnotationconfiguration($methodAnnotation$classAnnotation);
  34.     }
  35.     public function setAnnotationconfiguration($methodAnnotation$classAnnotation)
  36.     {
  37.         if(null != $methodAnnotation){
  38.             $this->themeManager->setActiveTheme($methodAnnotation->getTheme());
  39.             return;
  40.         }
  41.         if(null != $classAnnotation){
  42.             $this->themeManager->setActiveTheme($classAnnotation->getTheme());
  43.         }       
  44.     }
  45. }