diff --git a/catalyst/menu-bundle/Annotation/Menu.php b/catalyst/menu-bundle/Annotation/Menu.php new file mode 100644 index 00000000..e8001aeb --- /dev/null +++ b/catalyst/menu-bundle/Annotation/Menu.php @@ -0,0 +1,13 @@ +annot_reader = $annot_reader; + $this->menu_gen = $menu_gen; + $this->twig = $twig; + $this->menu_name = $menu_name; + } + + public function onKernelController(FilterControllerEvent $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->selected); + } + + protected function selectMenu($selected) + { + // get menu + $menu = $this->menu_gen->getMenu($this->menu_name); + + // set menu selected + $sel = $menu['index']->get($selected); + if ($sel != null) + $sel->setSelected(); + + // create twig global variable + $this->twig->addGlobal($this->menu_name, $menu); + } +} diff --git a/config/services.yaml b/config/services.yaml index 83d03a2a..487d8d13 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -118,4 +118,10 @@ services: $cache_dir: "%kernel.cache_dir%" $config_dir: "%kernel.root_dir%/../config" $acl_file: "%api_acl_file%" + + Catalyst\MenuBundle\Listener\MenuAnnotationListener: + arguments: + $menu_name: "main_menu" + tags: + - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }