MenuBuilder.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Menu;
  11. use Knp\Menu\FactoryInterface;
  12. use Knp\Menu\ItemInterface;
  13. use Knp\Menu\Provider\MenuProviderInterface;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\AdminBundle\Event\ConfigureMenuEvent;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. /**
  18. * Sonata menu builder.
  19. *
  20. * @author Martin Hasoň <martin.hason@gmail.com>
  21. * @author Alexandru Furculita <alex@furculita.net>
  22. */
  23. class MenuBuilder
  24. {
  25. /**
  26. * @var Pool
  27. */
  28. private $pool;
  29. /**
  30. * @var FactoryInterface
  31. */
  32. private $factory;
  33. /**
  34. * @var MenuProviderInterface
  35. */
  36. private $provider;
  37. /**
  38. * @var EventDispatcherInterface
  39. */
  40. private $eventDispatcher;
  41. /**
  42. * @param Pool $pool
  43. * @param FactoryInterface $factory
  44. * @param MenuProviderInterface $provider
  45. * @param EventDispatcherInterface $eventDispatcher
  46. */
  47. public function __construct(Pool $pool, FactoryInterface $factory, MenuProviderInterface $provider, EventDispatcherInterface $eventDispatcher)
  48. {
  49. $this->pool = $pool;
  50. $this->factory = $factory;
  51. $this->provider = $provider;
  52. $this->eventDispatcher = $eventDispatcher;
  53. }
  54. /**
  55. * Builds sidebar menu.
  56. *
  57. * @return ItemInterface
  58. */
  59. public function createSidebarMenu()
  60. {
  61. $menu = $this->factory->createItem('root');
  62. foreach ($this->pool->getAdminGroups() as $name => $group) {
  63. $extras = array(
  64. 'icon' => $group['icon'],
  65. 'label_catalogue' => $group['label_catalogue'],
  66. 'roles' => $group['roles'],
  67. 'sonata_admin' => true,
  68. );
  69. $menuProvider = isset($group['provider']) ? $group['provider'] : 'sonata_group_menu';
  70. $subMenu = $this->provider->get(
  71. $menuProvider,
  72. array(
  73. 'name' => $name,
  74. 'group' => $group,
  75. )
  76. );
  77. $subMenu = $menu->addChild($subMenu);
  78. $subMenu->setExtras(array_merge($subMenu->getExtras(), $extras));
  79. }
  80. $event = new ConfigureMenuEvent($this->factory, $menu);
  81. $this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
  82. return $event->getMenu();
  83. }
  84. }