Builder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Menu;
  7. /**
  8. * Menu builder object. Retrieves commands (\Magento\Backend\Model\Menu\Builder\AbstractCommand)
  9. * to build menu (\Magento\Backend\Model\Menu)
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Builder
  14. {
  15. /**
  16. * @var \Magento\Backend\Model\Menu\Builder\AbstractCommand[]
  17. */
  18. protected $_commands = [];
  19. /**
  20. * @var \Magento\Backend\Model\Menu\Item\Factory
  21. */
  22. protected $_itemFactory;
  23. /**
  24. * @param \Magento\Backend\Model\Menu\Item\Factory $menuItemFactory
  25. */
  26. public function __construct(\Magento\Backend\Model\Menu\Item\Factory $menuItemFactory)
  27. {
  28. $this->_itemFactory = $menuItemFactory;
  29. }
  30. /**
  31. * Process provided command object
  32. *
  33. * @param \Magento\Backend\Model\Menu\Builder\AbstractCommand $command
  34. * @return $this
  35. */
  36. public function processCommand(\Magento\Backend\Model\Menu\Builder\AbstractCommand $command)
  37. {
  38. if (!isset($this->_commands[$command->getId()])) {
  39. $this->_commands[$command->getId()] = $command;
  40. } else {
  41. $this->_commands[$command->getId()]->chain($command);
  42. }
  43. return $this;
  44. }
  45. /**
  46. * Populate menu object
  47. *
  48. * @param \Magento\Backend\Model\Menu $menu
  49. * @return \Magento\Backend\Model\Menu
  50. * @throws \OutOfRangeException in case given parent id does not exists
  51. */
  52. public function getResult(\Magento\Backend\Model\Menu $menu)
  53. {
  54. /** @var $items \Magento\Backend\Model\Menu\Item[] */
  55. $params = [];
  56. $items = [];
  57. // Create menu items
  58. foreach ($this->_commands as $id => $command) {
  59. $params[$id] = $command->execute();
  60. $item = $this->_itemFactory->create($params[$id]);
  61. $items[$id] = $item;
  62. }
  63. // Build menu tree based on "parent" param
  64. foreach ($items as $id => $item) {
  65. $sortOrder = $this->_getParam($params[$id], 'sortOrder');
  66. $parentId = $this->_getParam($params[$id], 'parent');
  67. $isRemoved = isset($params[$id]['removed']);
  68. if ($isRemoved) {
  69. continue;
  70. }
  71. if (!$parentId) {
  72. $menu->add($item, null, $sortOrder);
  73. } else {
  74. if (!isset($items[$parentId])) {
  75. throw new \OutOfRangeException(sprintf('Specified invalid parent id (%s)', $parentId));
  76. }
  77. if (isset($params[$parentId]['removed'])) {
  78. continue;
  79. }
  80. $items[$parentId]->getChildren()->add($item, null, $sortOrder);
  81. }
  82. }
  83. return $menu;
  84. }
  85. /**
  86. * Retrieve param by name or default value
  87. *
  88. * @param array $params
  89. * @param string $paramName
  90. * @param mixed $defaultValue
  91. * @return mixed
  92. */
  93. protected function _getParam($params, $paramName, $defaultValue = null)
  94. {
  95. return $params[$paramName] ?? $defaultValue;
  96. }
  97. }