56 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace Catalyst\MenuBundle\Twig;
 | |
| 
 | |
| use Twig\Extension\AbstractExtension;
 | |
| use Twig\TwigFunction;
 | |
| 
 | |
| use Symfony\Component\Routing\Exception\RouteNotFoundException;
 | |
| use Catalyst\MenuBundle\Service\Generator;
 | |
| 
 | |
| class MenuExtension extends AbstractExtension
 | |
| {
 | |
|     protected $router;
 | |
|     protected $menu_gen;
 | |
| 
 | |
|     public function __construct($router, Generator $menu_gen)
 | |
|     {
 | |
|         $this->router = $router;
 | |
|         $this->menu_gen = $menu_gen;
 | |
|     }
 | |
| 
 | |
|     public function getFunctions()
 | |
|     {
 | |
|         return [
 | |
|             new TwigFunction('menu_get', [$this, 'getMenu']),
 | |
|             new TwigFunction('menu_link', [$this, 'getMenuLink']),
 | |
|         ];
 | |
|     }
 | |
| 
 | |
|     public function getMenu($menu_group)
 | |
|     {
 | |
|         $menu_data = $this->menu_gen->getMenu($menu_group);
 | |
| 
 | |
|         if (isset($menu_data['menu']))
 | |
|             return $menu_data['menu'];
 | |
| 
 | |
|         return [];
 | |
|     }
 | |
| 
 | |
|     public function getMenuLink($mi)
 | |
|     {
 | |
|         // generate a link URL from a menu item
 | |
|         // NOTE: right now we only use routes
 | |
| 
 | |
|         try
 | |
|         {
 | |
|             $link = $this->router->generate($mi->getID());
 | |
|         }
 | |
|         catch (RouteNotFoundException $e)
 | |
|         {
 | |
|             $link = '';
 | |
|         }
 | |
| 
 | |
|         return $link;
 | |
|     }
 | |
| }
 |