<?php
namespace Boab\CmsBundle\View\EventListener;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Util\ClassUtils;
use Boab\CmsBundle\View\Annotation\Template;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Boab\CmsBundle\View\ThemeManagerInterface;
class TemplateAnnotationListener
{
private $reader;
private $themeManager;
public function __construct(Reader $reader, ThemeManagerInterface $themeManager)
{
$this->reader = $reader;
$this->themeManager = $themeManager;
}
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
list($controllerObject, $methodName) = $controller;
// Using ClassUtils::getClass in case the controller is an proxy
$controllerClass = ClassUtils::getClass($controllerObject);
$classAnnotation = $this->reader->getClassAnnotation(new \ReflectionClass($controllerClass), Template::class);
// Get method annotation
$controllerReflectionObject = new \ReflectionObject($controllerObject);
$reflectionMethod = $controllerReflectionObject->getMethod($methodName);
$methodAnnotation = $this->reader->getMethodAnnotation($reflectionMethod,Template::class);
$this->setAnnotationconfiguration($methodAnnotation, $classAnnotation);
}
public function setAnnotationconfiguration($methodAnnotation, $classAnnotation)
{
if(null != $methodAnnotation){
$this->themeManager->setActiveTheme($methodAnnotation->getTheme());
return;
}
if(null != $classAnnotation){
$this->themeManager->setActiveTheme($classAnnotation->getTheme());
}
}
}