75 lines
2 KiB
PHP
75 lines
2 KiB
PHP
<?php
|
|
|
|
namespace Catalyst\MenuBundle\EventListener;
|
|
|
|
use Doctrine\Common\Annotations\Reader;
|
|
use ReflectionClass;
|
|
use ReflectionException;
|
|
use RuntimeException;
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
|
use Twig\Environment as TwigEnvironment;
|
|
|
|
use Catalyst\MenuBundle\Annotation\Menu as MenuAnnotation;
|
|
use Catalyst\MenuBundle\Service\Generator as MenuGenerator;
|
|
|
|
class MenuAnnotationListener
|
|
{
|
|
protected $annot_reader;
|
|
protected $menu_gen;
|
|
protected $twig;
|
|
|
|
public function __construct(Reader $annot_reader, MenuGenerator $menu_gen, TwigEnvironment $twig)
|
|
{
|
|
$this->annot_reader = $annot_reader;
|
|
$this->menu_gen = $menu_gen;
|
|
$this->twig = $twig;
|
|
}
|
|
|
|
public function onKernelController(ControllerEvent $event)
|
|
{
|
|
if (!$event->isMasterRequest())
|
|
return;
|
|
|
|
// get controller
|
|
$event_controller = $event->getController();
|
|
if (!is_array($event_controller))
|
|
return;
|
|
|
|
list($controller, $method_name) = $event_controller;
|
|
|
|
// get reflection class
|
|
try
|
|
{
|
|
$ref_controller = new ReflectionClass($controller);
|
|
}
|
|
catch (ReflectionException $e)
|
|
{
|
|
throw new RuntimeException('Cannot read menu annotation.');
|
|
}
|
|
|
|
// get method annotations
|
|
$ref_method = $ref_controller->getMethod($method_name);
|
|
$annotation = $this->annot_reader->getMethodAnnotation($ref_method, MenuAnnotation::class);
|
|
|
|
// check if we get anything
|
|
if ($annotation == null)
|
|
return;
|
|
|
|
$this->selectMenu($annotation->group, $annotation->selected);
|
|
}
|
|
|
|
protected function selectMenu($group_id, $selected)
|
|
{
|
|
$this->menu_gen->setSelected($group_id, $selected);
|
|
|
|
/*
|
|
// get menu
|
|
$menu = $this->menu_gen->getMenu($group_id);
|
|
|
|
// set menu selected
|
|
$sel = $menu['index']->get($selected);
|
|
if ($sel != null)
|
|
$sel->setSelected();
|
|
*/
|
|
}
|
|
}
|