Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Created December 2, 2017 11:08
Show Gist options
  • Save VictorFursa/2770a32fc689c8b661a53fc9821459a8 to your computer and use it in GitHub Desktop.
Save VictorFursa/2770a32fc689c8b661a53fc9821459a8 to your computer and use it in GitHub Desktop.
<?php
namespace SadiSamBundle\DependencyInjection;
use SadiSamBundle\Menu\MenuInterface;
class ChainMenuItemProvider
{
private $menus;
public function __construct($menus)
{
/** @var $menu MenuInterface */
foreach ($menus as $menu)
{
dump($menu->getMenuItems()); // Как выбрать по menuName ?
}
}
public function addMenu(MenuInterface $menu, $menuName)
{
$this->menus[$menuName] = $menu;
}
public function getMenu($menuName)
{
if (array_key_exists($menuName, $this->menus)) {
return $this->menus[$menuName];
}
}
}
$menu = $this->container->get('chain.menu.item.provider');
$menu->addMenu(new SidebarMenu(), 'sidebar'); // это же не правильно? но ано работает :(
$menu->addMenu(new FooterMenu(), 'footer');
$menu->getMenu('sidebar')->getMenuItems();
$menu->getMenu('footer')->getMenuItems();
services:
_defaults:
autowire: true
autoconfigure: true
public: true
sidebar.menu:
class: SadiSamBundle\Service\SidebarMenu
tags:
- { name: menu.provider, menuName: sidebar }
chain.menu.item.provider:
class: SadiSamBundle\DependencyInjection\ChainMenuItemProvider
arguments: [!tagged menu.provider]
<?php
namespace SadiSamBundle\Service;
use SadiSamBundle\Menu\MenuInterface;
class SidebarMenu implements MenuInterface
{
public function getMenuItems()
{
return [
'php menu0',
'php menu1',
'php menu2',
'php menu3',
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment