RouterList.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Router list
  4. * Used as a container for list of routers
  5. *
  6. * Copyright © Magento, Inc. All rights reserved.
  7. * See COPYING.txt for license details.
  8. */
  9. namespace Magento\Framework\App;
  10. class RouterList implements RouterListInterface
  11. {
  12. /**
  13. * @var \Magento\Framework\ObjectManagerInterface
  14. */
  15. protected $objectManager;
  16. /**
  17. * List of routers
  18. *
  19. * @var RouterInterface[]
  20. */
  21. protected $routerList;
  22. /**
  23. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  24. * @param array $routerList
  25. */
  26. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, array $routerList)
  27. {
  28. $this->objectManager = $objectManager;
  29. $this->routerList = array_filter(
  30. $routerList,
  31. function ($item) {
  32. return (!isset($item['disable']) || !$item['disable']) && $item['class'];
  33. }
  34. );
  35. uasort($this->routerList, [$this, 'compareRoutersSortOrder']);
  36. }
  37. /**
  38. * Retrieve router instance by id
  39. *
  40. * @param string $routerId
  41. * @return RouterInterface
  42. */
  43. protected function getRouterInstance($routerId)
  44. {
  45. if (!isset($this->routerList[$routerId]['object'])) {
  46. $this->routerList[$routerId]['object'] = $this->objectManager->create(
  47. $this->routerList[$routerId]['class']
  48. );
  49. }
  50. return $this->routerList[$routerId]['object'];
  51. }
  52. /**
  53. * (PHP 5 &gt;= 5.0.0)<br/>
  54. * Return the current element
  55. * @link http://php.net/manual/en/iterator.current.php
  56. * @return RouterInterface
  57. */
  58. public function current()
  59. {
  60. return $this->getRouterInstance($this->key());
  61. }
  62. /**
  63. * (PHP 5 &gt;= 5.0.0)<br/>
  64. * Move forward to next element
  65. * @link http://php.net/manual/en/iterator.next.php
  66. * @return void Any returned value is ignored.
  67. */
  68. public function next()
  69. {
  70. next($this->routerList);
  71. }
  72. /**
  73. * (PHP 5 &gt;= 5.0.0)<br/>
  74. * Return the key of the current element
  75. * @link http://php.net/manual/en/iterator.key.php
  76. * @return string|int|null
  77. */
  78. public function key()
  79. {
  80. return key($this->routerList);
  81. }
  82. /**
  83. * (PHP 5 &gt;= 5.0.0)<br/>
  84. * Checks if current position is valid
  85. * @link http://php.net/manual/en/iterator.valid.php
  86. * @return boolean The return value will be casted to boolean and then evaluated.
  87. * Returns true on success or false on failure.
  88. */
  89. public function valid()
  90. {
  91. return !!current($this->routerList);
  92. }
  93. /**
  94. * (PHP 5 &gt;= 5.0.0)<br/>
  95. * Rewind the Iterator to the first element
  96. * @link http://php.net/manual/en/iterator.rewind.php
  97. * @return void Any returned value is ignored.
  98. */
  99. public function rewind()
  100. {
  101. reset($this->routerList);
  102. }
  103. /**
  104. * Compare routers sortOrder
  105. *
  106. * @param array $routerDataFirst
  107. * @param array $routerDataSecond
  108. * @return int
  109. */
  110. protected function compareRoutersSortOrder($routerDataFirst, $routerDataSecond)
  111. {
  112. return (int)$routerDataFirst['sortOrder'] <=> (int)$routerDataSecond['sortOrder'];
  113. }
  114. }