lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Component/Menu.php line 175

Open in your IDE?
  1. <?php
  2. /**
  3. * Menu
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\LisaBundle\Component;
  8. use Exception;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Translation\TranslatorInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use VisualMedia\LisaBundle\Component\Interfaces\MenuInterface;
  13. use VisualMedia\LisaBundle\Component\Interfaces\MenuItemInterface;
  14. use VisualMedia\LisaBundle\Helper\Helper;
  15. /**
  16. * Menu
  17. */
  18. class Menu implements MenuInterface
  19. {
  20. /**
  21. * @var array
  22. */
  23. protected $items = array();
  24. /**
  25. * @var bool
  26. */
  27. protected $initialized = false;
  28. /**
  29. * @var RequestStack
  30. */
  31. protected $requestStack;
  32. /**
  33. * @var TranslatorInterface
  34. */
  35. protected $translator;
  36. /**
  37. * @var AuthorizationCheckerInterface
  38. */
  39. protected $authorizationChecker;
  40. /**
  41. * @var active
  42. */
  43. protected $active;
  44. /**
  45. * @param RequestStack $requestStack
  46. * @param TranslatorInterface $translator
  47. * @param AuthorizationCheckerInterface $authorizationChecker
  48. */
  49. public function __construct(?RequestStack $requestStack, ?TranslatorInterface $translator, ?AuthorizationCheckerInterface $authorizationChecker)
  50. {
  51. $this->requestStack = $requestStack;
  52. $this->translator = $translator;
  53. $this->authorizationChecker = $authorizationChecker;
  54. }
  55. /**
  56. * @param array $items
  57. * @return void
  58. */
  59. public function setItems(array $items): void
  60. {
  61. $this->items = $items;
  62. }
  63. /**
  64. * @param string $key
  65. * @param MenuItem $item
  66. * @return MenuItemInterface
  67. */
  68. public function setItem($key, MenuItem $item): MenuItemInterface
  69. {
  70. $this->items[$key] = $item;
  71. return $item;
  72. }
  73. /**
  74. * @param MenuItem $item
  75. * @return MenuItemInterface
  76. */
  77. public function addItem(MenuItem $item): MenuItemInterface
  78. {
  79. $this->items[] = $item;
  80. return $item;
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function getItems(): array
  86. {
  87. return $this->items;
  88. }
  89. /**
  90. * @return boolean
  91. */
  92. public function getActive(): ?bool
  93. {
  94. return $this->active;
  95. }
  96. /**
  97. * @param bool $active
  98. * @return void
  99. */
  100. public function setActive(bool $active): void
  101. {
  102. $this->active = $active;
  103. }
  104. /**
  105. * @return void
  106. */
  107. public function setActiveItems(): void
  108. {
  109. $request = $this->requestStack->getMasterRequest();
  110. $uri = $request !== null ? $request->getUri() : '';
  111. $setActiveFunction = function(&$menuItem) {
  112. $menuItem->setActive(true);
  113. $this->setActive(true);
  114. $parent = $menuItem->getParent();
  115. while ($parent !== null) {
  116. $parent = $this->items[$parent];
  117. $parent->setActive(true);
  118. $parent = $parent->getParent();
  119. }
  120. };
  121. foreach ($this->items as $menuItem) {
  122. // Active Regex.
  123. if ($menuItem->getActiveRegex() !== null) {
  124. if (preg_match(Helper::phpRegex($menuItem->getActiveRegex()), $uri, $matches) === 1) {
  125. $setActiveFunction($menuItem);
  126. }
  127. }
  128. // Active Singular Regex
  129. if(null !== $singularActiveRegex = $menuItem->getSingularActiveRegex()) {
  130. if (preg_match(Helper::phpRegex($singularActiveRegex), $uri, $matches) === 1) {
  131. $setActiveFunction($menuItem);
  132. }
  133. }
  134. // Active Route (& parameters).
  135. $requestedRouteName = $request->attributes->get('_route');
  136. $routeName = $menuItem->getActiveRoute();
  137. if (null !== $routeName && $requestedRouteName !== null) {
  138. if ($routeName === $requestedRouteName) {
  139. $setActiveFunction($menuItem);
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * @return MenuInterface
  146. */
  147. public function initialize(): MenuInterface
  148. {
  149. // Menu mag maar 1 keer worden opgebouwd.
  150. if ($this->initialized === true) {
  151. throw new Exception($this->translator->trans('menu.already_initialized', array(
  152. '%menu%' => get_class($this),
  153. ), 'exception'));
  154. }
  155. // Initialize Menu Items.
  156. foreach ($this->items as $menuItem) {
  157. $menuItem->initialize();
  158. }
  159. // Set active.
  160. $this->setActiveItems();
  161. // Set children.
  162. $items = array();
  163. foreach ($this->items as $key=>$menuItem) {
  164. if ($menuItem->getParent() === null) {
  165. $items[$menuItem->getKey() ?? $key] = $menuItem;
  166. }
  167. else {
  168. $parentMenuItem = $this->items[$menuItem->getParent()];
  169. $parentMenuItem->addChild($menuItem);
  170. }
  171. }
  172. $this->items = $items;
  173. // Transform
  174. $this->transform();
  175. // Initialized.
  176. $this->initialized = true;
  177. return $this;
  178. }
  179. /**
  180. * @return bool
  181. */
  182. public function isInitialized(): bool
  183. {
  184. return $this->initialized;
  185. }
  186. /**
  187. * @param bool $initialized
  188. * @return void
  189. */
  190. public function setInitialized(bool $initialized = true): void
  191. {
  192. $this->initialized = $initialized;
  193. }
  194. /**
  195. * @return void
  196. */
  197. public function transform(): void
  198. {
  199. $this->cleanItems($this->items);
  200. }
  201. /**
  202. * @param array $items
  203. * @return void
  204. */
  205. protected function cleanItems(array &$items): void
  206. {
  207. foreach ($items as $key=>&$menuItem) {
  208. if ($menuItem !== null) {
  209. $children = $menuItem->getChildren();
  210. $this->cleanItems($children);
  211. $menuItem->setChildren($children);
  212. }
  213. else {
  214. unset($items[$key]);
  215. }
  216. }
  217. }
  218. /**
  219. * @param string|array $key
  220. * @return MenuItemInterface
  221. */
  222. protected function extractMenuItem($key): ?MenuItemInterface
  223. {
  224. $menuItem = null;
  225. if (is_array($key)) {
  226. if (array_key_exists(current($key), $this->items)) {
  227. $items = $this->items;
  228. while (isset($items[current($key)])) {
  229. $menuItem = $items[current($key)];
  230. $items = $menuItem->getChildren();
  231. next($key);
  232. }
  233. }
  234. if ($menuItem->getKey() !== end($key)) {
  235. return null;
  236. }
  237. }
  238. else {
  239. if (array_key_exists($key, $this->items)) {
  240. $menuItem = $this->items[$key];
  241. }
  242. }
  243. if ($menuItem !== null && $menuItem->getPermission() !== null) {
  244. if (!$this->authorizationChecker->isGranted($menuItem->getPermission())) {
  245. return null;
  246. }
  247. }
  248. return $menuItem;
  249. }
  250. }