/** * inject our config and custom matcher. */ public function __construct() { $matcher = new Matcher(); $matcher->addVoter(new UriVoter(app('url')->current())); $matcher->addVoter(new RouteVoter(app('request'))); $options = config('menu.render'); parent::__construct($matcher, $options); }
/** * @param boolean $value * * @dataProvider provideBoolean */ public function testFirstVoterWins($value) { $item = $this->getMock('Knp\\Menu\\ItemInterface'); $item->expects($this->any())->method('isCurrent')->will($this->returnValue(null)); $voter1 = $this->getMock('Knp\\Menu\\Matcher\\Voter\\VoterInterface'); $voter1->expects($this->once())->method('matchItem')->with($this->equalTo($item))->will($this->returnValue($value)); $voter2 = $this->getMock('Knp\\Menu\\Matcher\\Voter\\VoterInterface'); $voter2->expects($this->never())->method('matchItem'); $matcher = new Matcher(); $matcher->addVoter($voter1); $matcher->addVoter($voter2); $this->assertSame($value, $matcher->isCurrent($item)); }
/** * @return string html */ public function render() { if ($this->getMenu() instanceof ItemInterface === false) { throw new \InvalidArgumentException('Menu needs to conform to the ItemInterface (Knp\\Menu\\ItemInterface)'); } $matcher = new Matcher(); $matcher->addVoter(new UriVoter((string) $this->getBag()->get('url')->getPath())); $renderer = new ListRenderer($matcher); return $renderer->render($this->getMenu()); }
/** * @param NavbarMenuEvent $event */ public function onNavbarMenuInit(NavbarMenuEvent $event) { $menu = $event->getMenu(); $factory = $event->getFactory(); // Home link $item = $factory->createItem($this->translator->trans('Home', array(), 'asf_backend'), array('route' => 'asf_backend_homepage')); $menu->addChild($item); $matcher = new Matcher(); $matcher->addVoter(new RouteVoter($this->request->getCurrentRequest())); $item->setCurrent($matcher->isCurrent($item)); }
/** * Constructor. * * @param ContainerInterface $container * @param FactoryInterface $factory */ public function __construct(ContainerInterface $container, FactoryInterface $factory) { $this->container = $container; $this->factory = $factory; $matcher = new Matcher(); try { $request = $this->container->get('request_stack')->getCurrentRequest(); $voter = new RouteVoter($request); $matcher->addVoter($voter); } catch (InactiveScopeException $e) { // We are most probably running from the command line, which means there is no 'request' service. // We just gracefully continue } parent::__construct($matcher, array('allow_safe_labels' => true, 'currentClass' => 'active')); }
/** * @param FactoryInterface $factory * @param SecurityContextInterface $securityContext * @param RequestStack $request * @param RouterInterface $router * @param MenuItemManagerInterface $menuManager * @param TranslatorInterface $translator * @param Matcher $matcher */ public function __construct(FactoryInterface $factory, SecurityContextInterface $securityContext, RequestStack $request, RouterInterface $router, MenuItemManagerInterface $menuManager, TranslatorInterface $translator, Matcher $matcher) { $this->securityContext = $securityContext; if ($this->securityContext->getToken() && ($this->securityContext->isGranted('IS_AUTHENTICATED_FULLY') || $this->securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED'))) { $this->isLoggedIn = true; } $this->factory = $factory; $this->router = $router; $this->request = $request->getCurrentRequest(); $this->menuManager = $menuManager; $this->translator = $translator; $this->matcher = $matcher; $this->viewStatus = $this->request->getSession()->get('_viewStatus') ? $this->request->getSession()->get('_viewStatus') : Page::STATUS_PUBLISHED; $this->currentPath = substr($this->request->getPathInfo(), -1) != '/' ? $this->request->getPathInfo() . '/' : $this->request->getPathInfo(); $this->currentUri = $this->request->getBaseUrl() . $this->currentPath; $voter = new UriVoter($this->request->getBaseUrl() . $this->request->getPathInfo()); $this->matcher->addVoter($voter); $voter = new UriVoter($this->currentUri); $this->matcher->addVoter($voter); }
/** * * @param array $breadcrumbs */ protected function setBreadcrumb($breadcrumbs) { $course = $this->getCourse(); //$session = $this->getSession(); // Adding course breadcrumb. if (!empty($course)) { $courseBreadcrumb = array('name' => \Display::return_icon('home.png') . ' ' . $course->getTitle(), 'url' => array('route' => 'course', 'routeParameters' => array('cidReq' => $course->getCode(), 'id_session' => api_get_session_id()))); array_unshift($breadcrumbs, $courseBreadcrumb); } $app = $this->app; $app['main_breadcrumb'] = function ($app) use($breadcrumbs) { /** @var \Knp\Menu\MenuItem $menu */ $menu = $app['knp_menu.factory']->createItem('root', array('childrenAttributes' => array('class' => 'breadcrumb', 'currentClass' => 'active'))); if (!empty($breadcrumbs)) { foreach ($breadcrumbs as $item) { if (empty($item['url'])) { $item['url'] = array(); } $menu->addChild($item['name'], $item['url']); } } return $menu; }; $matcher = new Matcher(); $voter = new \Knp\Menu\Silex\Voter\RouteVoter(); $voter->setRequest($this->getRequest()); $matcher->addVoter($voter); $renderer = new \Knp\Menu\Renderer\TwigRenderer($this->get('twig'), 'bread.tpl', $matcher); $bread = $renderer->render($this->get('main_breadcrumb'), array('template' => 'default/layout/bread.tpl')); $app['breadcrumbs'] = $bread; }
/** * @param $path string */ public function addVoter($path) { $voter = new UriVoter($path); $this->matcher->addVoter($voter); }
/** * @param ContentEntity $contentEntity * @param array $options * @return string */ public function render($contentEntity, $options = array()) { $contentMenuItems = $this->cq()->getContentMenuItems($contentEntity); /** * @var \Knp\Menu\MenuItem $rootMenu * @var \Knp\Menu\MenuItem $menu * */ $menu = array(); $rootMenu = $options['factory']->createItem($contentEntity->getSlug()); $rootMenu->setChildrenAttribute('id', $options['id']); $rootMenu->setChildrenAttribute('class', $options['class']); $menu[$contentEntity->getId()] = $rootMenu; /** * @var ContentEntity $content */ foreach ($contentMenuItems as $content) { $menu[$content->getId()] = $menu[$content->getTreeParent()->getId()]->addChild($content->getSlug(), array('uri' => '#')); if (isset($options['emptyTitle']) && $options['emptyTitle'] === true) { $menu[$content->getId()]->setLabel(''); } else { $menu[$content->getId()]->setLabel($content->getTitle()); } $contentMeta = $this->cm()->getContentAllMeta($content); if (isset($contentMeta['linkType']) && $contentMeta['linkType'] == 'route') { if (isset($contentMeta['linkRoute']) && !empty($contentMeta['linkRoute'])) { $linkURL = $this->tp()->getCurrentSkin()->getNavigationRoute($contentMeta['linkRoute']); $menu[$content->getId()]->setUri($linkURL); } } if (isset($contentMeta['linkType']) && $contentMeta['linkType'] == 'link') { if (isset($contentMeta['linkExternal']) && !empty($contentMeta['linkExternal'])) { $menu[$content->getId()]->setUri($contentMeta['linkExternal']); } } if (isset($contentMeta['linkType']) && $contentMeta['linkType'] == 'content') { if (isset($contentMeta['linkContent']) && $contentMeta['linkContent'] instanceof ContentEntity) { $contentLinkURL = $this->cq()->getPublicURL($contentMeta['linkContent']); $menu[$content->getId()]->setUri($contentLinkURL); } } if (isset($contentMeta['linkTarget']) && !empty($contentMeta['linkTarget'])) { $menu[$content->getId()]->setLinkAttribute('target', $contentMeta['linkTarget']); } if (isset($contentMeta['linkClass']) && !empty($contentMeta['linkClass'])) { $menu[$content->getId()]->setLinkAttribute('class', $contentMeta['linkClass']); } } $requestURL = $this->getRequest()->getRequestUri(); $menuTemplate = $this->getContentTemplate($contentEntity); $matcher = new Matcher(); $voter = new UriVoter($requestURL); $matcher->addVoter($voter); $renderer = new TwigRenderer($options['environment'], $menuTemplate, $matcher); return $renderer->render($menu[$contentEntity->getId()]); }