src/Menu/Builder.php line 285
<?php/*** This file is part of the NautilePlus package.** (c) Nicolas SOBOLEV <nicolas.sobolev@nautile.sarl>** For the full copyright and license information, please view the LICENSE* file that was distributed with this source code.*/namespace App\Menu;use Knp\Menu\FactoryInterface;use Knp\Menu\ItemInterface;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Contracts\Translation\TranslatorInterface;/*** Menu builder engine.** @see https://symfony.com/doc/current/bundles/KnpMenuBundle/menu_builder_service.html** @author Nicolas SOBOLEV <nicolas.sobolev@nautile.sarl>*/class Builder{/*** Menu builder factory.** @var FactoryInterface*/private $factory;/*** Translation service.** @var TranslatorInterface*/private $translator;/*** Url generation service.** @var UrlGeneratorInterface*/private $urlGenerator;/*** Security service.** @var Security*/private $security;/*** Service constructor.** @param FactoryInterface $factory Knp menu factory* @param UrlGeneratorInterface $urlGenerator Url generation service* @param TranslatorInterface $translator Translation service* @param Security $security Security service*/public function __construct(FactoryInterface $factory, UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, Security $security){$this->factory = $factory;$this->urlGenerator = $urlGenerator;$this->translator = $translator;$this->security = $security;}/*** Parses and formats the menu attributes.** @param array $menuItem Reference of menu item** @return array Menu item attributes*/private function prepareMenuAttributes(&$menuItem): array{$menuAttr = [// default attribures here ...];if (isset($menuItem['name'])) {$menuAttr['name'] = $menuItem['name'];}// configured attributesif (isset($menuItem['attributes']) && is_array($menuItem['attributes'])) {foreach ($menuItem['attributes'] as $attrName => $attrValue) {$menuAttr[$attrName] = $attrValue;}}return $menuAttr;}/*** Creates child of menu object.** @param ItemInterface $menu Reference of the Menu object that is being generated* @param array $menuItem Reference of the menu child item to be addad to the menu object* @param array $menuParams Reference of the parameter array of child menu item** @return Builder Current menu builder instance*/private function processNode(ItemInterface &$menu, &$menuItem, &$menuParams): self{if (isset($menuItem['children'])) {$menuParams['extras']['expanded'] = true;if (isset($menuItem['expanded'])) {$menuParams['extras']['expanded'] = $menuItem['expanded'];}}$menu->addChild($menuItem['name'], $menuParams);if (isset($menuItem['children'])) {$this->fillMenuNodes($menu[$menuItem['name']], $menuItem['children']);}return $this;}/*** Transforms the table representation of menu to the randerable object.** @param ItemInterface $menu Reference of the Menu object* @param array $menuData Reference of the table representation of the menu** @return Builder Current menu builder instance*/private function fillMenuNodes(ItemInterface &$menu, array &$menuData): self{$itemIndex = 0;foreach ($menuData as $menuItem) {$menuParams = [];// whether this item is activatedif (isset($menuItem['current'])) {$menuParams['current'] = $menuItem['current'];}// @TODO$translationDomain = 'navigation';if (array_key_exists('translation_domain', $menuItem)) {$translationDomain = $menuItem['translation_domain'];}// menu action URL$menuParams['uri'] = null; // no action by default// generate the URI with the value of the "route" attributeif (isset($menuItem['route'])) {$routeParams = [];if (isset($menuItem['routeParams'])) {$routeParams = $menuItem['routeParams'];}$menuParams['uri'] = $this->urlGenerator->generate($menuItem['route'], $routeParams, UrlGeneratorInterface::ABSOLUTE_URL);}// the value of the "uri" attribute (if not empty) will override// the uri generated with the value of the "route" attributeif (isset($menuItem['uri'])) {$menuParams['uri'] = $menuItem['uri'];}// item [name] attribute (could be used as anchor, hashtag etc)if (isset($menuItem['name'])) {$menuParams['name'] = $menuItem['name'];}// menu item label$menuParams['label'] = '';if (isset($menuItem['label'])) {$menuParams['label'] = $this->translator->trans($menuItem['label'], [], $translationDomain);}// menu item attributes$menuParams['attributes'] = $this->prepareMenuAttributes($menuItem);// if (!isset($menuItem['iconClass'])) {// // set the default icon using the translation file// // the translation key should be the same as for label except for "label"// // is replaced by "icon"// // $menuParams['extras']['icon'] = $this->translator->trans(// // str_replace('label', 'icon', $menuItem['name']),// // [],// // $translationDomain// // );// icon propertyif (isset($menuItem['icon'])) {$menuParams['extras']['icon'] = $this->translator->trans($menuItem['icon'], [], $translationDomain);}// hint propertyif (isset($menuItem['hint'])) {$menuParams['extras']['hint'] = $this->translator->trans($menuItem['hint'], [], $translationDomain);}// // set the default uri hash// // the translation key should be the same as for label except for "label"// // is replaced by "hash"// $menuParams['extras']['hash'] = $this->translator->trans(// str_replace('label', 'hash', $menuItem['name']),// [],// $translationDomain// );// // hash property overrides the default behaviour// if (isset($menuItem['hash'])) {// $menuParams['extras']['hash'] = $menuItem['hash'];// }// if (isset($menuItem['options'])) {// $menuParams['extras']['options'] = $menuItem['options'];// }// $menuParams['extras']['index'] = $itemIndex++;$this->processNode($menu, $menuItem, $menuParams);}return $this;}/*** Menu node generator.** @param array $menuData Menu utems table* @param array $attributes Attributes for menu items** @return ItemInterface Processed menu object*/private function generateMenu(array &$menuData, array $attributes = []): ItemInterface{$menu = $this->factory->createItem('root');foreach ($attributes as $attrName => $attrValue) {$menu->setChildrenAttribute($attrName, $attrValue);}$this->fillMenuNodes($menu, $menuData);return $menu;}// /**// * Sidebar menu service.// *// * @param UrlGeneratorInterface $urlGenerator Url generation service// * @param TranslatorInterface $translator Translation service// * @param SidebarMenu $sidebarMenu Sidebar menu items mapping service// *// * @return ItemInterface generated menu item// */// public function createSidebarMenu(UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, SidebarMenu $sidebarMenu)// {// $this->urlGenerator = $urlGenerator;// $this->translator = $translator;// $items = $sidebarMenu->items;// $attributes = [// 'class' => 'mdc-list',// 'aria-hidden' => 'true',// 'aria-orientation' => 'vertical',// 'data-nautile-plus-list' => '',// ];// return $this->generateMenu($items, $attributes);// }/*** Main menu service.** @return ItemInterface generated menu item*/public function createMainMenu(array $options): ItemInterface{$menu = new MainMenu($options, $this->security);return $this->generateMenu($menu->items, $menu->attributes);}/*** Userbox menu service.** @return ItemInterface generated menu item*/public function createUserboxMenu(array $options): ItemInterface{$menu = new UserboxMenu($options, $this->security);return $this->generateMenu($menu->items, $menu->attributes);}// /**// * Settings menu service.// *// * @param UrlGeneratorInterface $urlGenerator Url generation service// * @param TranslatorInterface $translator Translation service// * @param BaseMenu $menu Menu items mapping service// *// * @return ItemInterface generated menu item// */// public function createContextMenu(UrlGeneratorInterface $urlGenerator, TranslatorInterface $translator, BaseMenu $menu)// {// $this->urlGenerator = $urlGenerator;// $this->translator = $translator;// $items = $menu->items;// $attributes = [// // 'class' => 'mdc-list',// // 'aria-hidden' => 'true',// // 'aria-orientation' => 'vertical',// // 'data-nautile-plus-list' => '',// // 'tabindex' => '-1',// ];// return $this->generateMenu($items, $attributes);// }}