Config.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Routes configuration model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Route;
  9. use Magento\Framework\Serialize\SerializerInterface;
  10. class Config implements ConfigInterface
  11. {
  12. /**
  13. * @var \Magento\Framework\App\Route\Config\Reader
  14. */
  15. protected $_reader;
  16. /**
  17. * @var \Magento\Framework\Cache\FrontendInterface
  18. */
  19. protected $_cache;
  20. /**
  21. * @var string
  22. */
  23. protected $_cacheId;
  24. /**
  25. * @var \Magento\Framework\Config\ScopeInterface
  26. */
  27. protected $_configScope;
  28. /**
  29. * @var \Magento\Framework\App\AreaList
  30. */
  31. protected $_areaList;
  32. /**
  33. * @var array
  34. */
  35. protected $_routes;
  36. /**
  37. * @var SerializerInterface
  38. */
  39. private $serializer;
  40. /**
  41. * @param Config\Reader $reader
  42. * @param \Magento\Framework\Config\CacheInterface $cache
  43. * @param \Magento\Framework\Config\ScopeInterface $configScope
  44. * @param \Magento\Framework\App\AreaList $areaList
  45. * @param string $cacheId
  46. */
  47. public function __construct(
  48. Config\Reader $reader,
  49. \Magento\Framework\Config\CacheInterface $cache,
  50. \Magento\Framework\Config\ScopeInterface $configScope,
  51. \Magento\Framework\App\AreaList $areaList,
  52. $cacheId = 'RoutesConfig'
  53. ) {
  54. $this->_reader = $reader;
  55. $this->_cache = $cache;
  56. $this->_cacheId = $cacheId;
  57. $this->_configScope = $configScope;
  58. $this->_areaList = $areaList;
  59. }
  60. /**
  61. * Fetch routes from configs by area code and router id
  62. *
  63. * @param string $scope
  64. * @return array
  65. */
  66. protected function _getRoutes($scope = null)
  67. {
  68. $scope = $scope ?: $this->_configScope->getCurrentScope();
  69. if (isset($this->_routes[$scope])) {
  70. return $this->_routes[$scope];
  71. }
  72. $cacheId = $scope . '::' . $this->_cacheId;
  73. $cachedRoutes = $this->_cache->load($cacheId);
  74. if ($cachedRoutes) {
  75. $cachedRoutes = $this->getSerializer()->unserialize($cachedRoutes);
  76. if (is_array($cachedRoutes)) {
  77. $this->_routes[$scope] = $cachedRoutes;
  78. return $cachedRoutes;
  79. }
  80. }
  81. $routers = $this->_reader->read($scope);
  82. $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes'];
  83. $routesData = $this->getSerializer()->serialize($routes);
  84. $this->_cache->save($routesData, $cacheId);
  85. $this->_routes[$scope] = $routes;
  86. return $routes;
  87. }
  88. /**
  89. * Retrieve route front name
  90. *
  91. * @param string $routeId
  92. * @param null $scope
  93. * @return string
  94. */
  95. public function getRouteFrontName($routeId, $scope = null)
  96. {
  97. $routes = $this->_getRoutes($scope);
  98. return isset($routes[$routeId]) ? $routes[$routeId]['frontName'] : $routeId;
  99. }
  100. /**
  101. * @param string $frontName
  102. * @param string $scope
  103. * @return bool|int|string
  104. */
  105. public function getRouteByFrontName($frontName, $scope = null)
  106. {
  107. foreach ($this->_getRoutes($scope) as $routeId => $routeData) {
  108. if ($routeData['frontName'] == $frontName) {
  109. return $routeId;
  110. }
  111. }
  112. return false;
  113. }
  114. /**
  115. * @param string $frontName
  116. * @param string $scope
  117. * @return string[]
  118. */
  119. public function getModulesByFrontName($frontName, $scope = null)
  120. {
  121. $routes = $this->_getRoutes($scope);
  122. $modules = [];
  123. foreach ($routes as $routeData) {
  124. if ($routeData['frontName'] == $frontName && isset($routeData['modules'])) {
  125. $modules = $routeData['modules'];
  126. break;
  127. }
  128. }
  129. return array_unique($modules);
  130. }
  131. /**
  132. * Get serializer
  133. *
  134. * @return \Magento\Framework\Serialize\SerializerInterface
  135. * @deprecated 101.0.0
  136. */
  137. private function getSerializer()
  138. {
  139. if ($this->serializer === null) {
  140. $this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
  141. ->get(SerializerInterface::class);
  142. }
  143. return $this->serializer;
  144. }
  145. }