MenuBuilder.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Packagist\WebBundle\Menu;
  3. use Knp\Menu\FactoryInterface;
  4. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  5. class MenuBuilder
  6. {
  7. private $factory;
  8. private $username;
  9. /**
  10. * @param FactoryInterface $factory
  11. * @param TokenStorageInterface $tokenStorage
  12. */
  13. public function __construct(FactoryInterface $factory, TokenStorageInterface $tokenStorage)
  14. {
  15. $this->factory = $factory;
  16. if ($tokenStorage->getToken() && $tokenStorage->getToken()->getUser()) {
  17. $this->username = $tokenStorage->getToken()->getUser()->getUsername();
  18. }
  19. }
  20. public function createUserMenu()
  21. {
  22. $menu = $this->factory->createItem('root');
  23. $menu->setChildrenAttribute('class', 'list-unstyled');
  24. $this->addProfileMenu($menu);
  25. $menu->addChild('hr', array('label' => '<hr>', 'labelAttributes' => array('class' => 'normal'), 'extras' => array('safe_label' => true)));
  26. $menu->addChild('Logout', array('label' => '<span class="icon-off"></span>Logout', 'route' => 'logout', 'extras' => array('safe_label' => true)));
  27. return $menu;
  28. }
  29. public function createProfileMenu()
  30. {
  31. $menu = $this->factory->createItem('root');
  32. $menu->setChildrenAttribute('class', 'nav nav-tabs nav-stacked');
  33. $this->addProfileMenu($menu);
  34. return $menu;
  35. }
  36. private function addProfileMenu($menu)
  37. {
  38. $menu->addChild('Profile', array('label' => '<span class="icon-vcard"></span>Profile', 'route' => 'fos_user_profile_show', 'extras' => array('safe_label' => true)));
  39. $menu->addChild('Settings', array('label' => '<span class="icon-tools"></span>Settings', 'route' => 'fos_user_profile_edit', 'extras' => array('safe_label' => true)));
  40. $menu->addChild('Change password', array('label' => '<span class="icon-key"></span>Change password', 'route' => 'fos_user_change_password', 'extras' => array('safe_label' => true)));
  41. $menu->addChild('My packages', array('label' => '<span class="icon-box"></span>My packages', 'route' => 'user_packages', 'routeParameters' => array('name' => $this->username), 'extras' => array('safe_label' => true)));
  42. $menu->addChild('My favorites', array('label' => '<span class="icon-leaf"></span>My favorites', 'route' => 'user_favorites', 'routeParameters' => array('name' => $this->username), 'extras' => array('safe_label' => true)));
  43. }
  44. }