* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\MainBundle\Routing; use Chill\MainBundle\Routing\MenuComposer; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Twig\Environment; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; /** * Add the filter 'chill_menu' * * @author Julien Fastré */ class MenuTwig extends AbstractExtension implements ContainerAwareInterface { /** * * @var MenuComposer */ private $menuComposer; /** * * @var \Symfony\Component\DependencyInjection\ContainerInterface */ private $container; /** * the default parameters for chillMenu * * @var mixed[] */ private $defaultParams = array( 'layout' => '@ChillMain/Menu/defaultMenu.html.twig', 'args' => array(), 'activeRouteKey' => null ); public function __construct(MenuComposer $menuComposer) { $this->menuComposer = $menuComposer; } public function getFunctions() { return [new TwigFunction('chill_menu', array($this, 'chillMenu'), array( 'is_safe' => array('html'), 'needs_environment' => true )) ]; } /** * Render a Menu corresponding to $menuId * * Expected params : * - args: the arguments to build the path (i.e: if pattern is /something/{bar}, args must contain {'bar': 'foo'} * - layout: the layout. Absolute path needed (i.e.: ChillXyzBundle:section:foo.html.twig) * - activeRouteKey : the key active, will render the menu differently. * * @deprecated link: see https://redmine.champs-libres.coop/issues/179 for more informations * * @param string $menuId * @param mixed[] $params */ public function chillMenu(Environment $env, $menuId, array $params = array()) { $resolvedParams = array_merge($this->defaultParams, $params); $layout = $resolvedParams['layout']; unset($resolvedParams['layout']); if ($this->menuComposer->hasLocalMenuBuilder($menuId) === false) { $resolvedParams['routes'] = $this->menuComposer->getRoutesFor($menuId, $resolvedParams); return $env->render($layout, $resolvedParams); } else { $resolvedParams['menus'] = $this->menuComposer->getMenuFor($menuId, $resolvedParams); return $env->render($layout, $resolvedParams); } } public function getName() { return 'chill_menu'; } public function setContainer(ContainerInterface $container = null) { $this->container = $container; } }