Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class Config implements ConfigInterface
13 {
17  protected $_reader;
18 
22  protected $_cache;
23 
27  protected $_cacheId;
28 
32  protected $_configScope;
33 
37  protected $_areaList;
38 
42  protected $_routes;
43 
47  private $serializer;
48 
56  public function __construct(
57  Config\Reader $reader,
59  \Magento\Framework\Config\ScopeInterface $configScope,
60  \Magento\Framework\App\AreaList $areaList,
61  $cacheId = 'RoutesConfig'
62  ) {
63  $this->_reader = $reader;
64  $this->_cache = $cache;
65  $this->_cacheId = $cacheId;
66  $this->_configScope = $configScope;
67  $this->_areaList = $areaList;
68  }
69 
76  protected function _getRoutes($scope = null)
77  {
78  $scope = $scope ?: $this->_configScope->getCurrentScope();
79  if (isset($this->_routes[$scope])) {
80  return $this->_routes[$scope];
81  }
82  $cacheId = $scope . '::' . $this->_cacheId;
83  $cachedRoutes = $this->_cache->load($cacheId);
84  if ($cachedRoutes) {
85  $cachedRoutes = $this->getSerializer()->unserialize($cachedRoutes);
86  if (is_array($cachedRoutes)) {
87  $this->_routes[$scope] = $cachedRoutes;
88  return $cachedRoutes;
89  }
90  }
91 
92  $routers = $this->_reader->read($scope);
93  $routes = $routers[$this->_areaList->getDefaultRouter($scope)]['routes'];
94  $routesData = $this->getSerializer()->serialize($routes);
95  $this->_cache->save($routesData, $cacheId);
96  $this->_routes[$scope] = $routes;
97  return $routes;
98  }
99 
107  public function getRouteFrontName($routeId, $scope = null)
108  {
109  $routes = $this->_getRoutes($scope);
110  return isset($routes[$routeId]) ? $routes[$routeId]['frontName'] : $routeId;
111  }
112 
118  public function getRouteByFrontName($frontName, $scope = null)
119  {
120  foreach ($this->_getRoutes($scope) as $routeId => $routeData) {
121  if ($routeData['frontName'] == $frontName) {
122  return $routeId;
123  }
124  }
125 
126  return false;
127  }
128 
134  public function getModulesByFrontName($frontName, $scope = null)
135  {
136  $routes = $this->_getRoutes($scope);
137  $modules = [];
138  foreach ($routes as $routeData) {
139  if ($routeData['frontName'] == $frontName && isset($routeData['modules'])) {
140  $modules = $routeData['modules'];
141  break;
142  }
143  }
144 
145  return array_unique($modules);
146  }
147 
154  private function getSerializer()
155  {
156  if ($this->serializer === null) {
158  ->get(SerializerInterface::class);
159  }
160  return $this->serializer;
161  }
162 }
getModulesByFrontName($frontName, $scope=null)
Definition: Config.php:134
getRouteFrontName($routeId, $scope=null)
Definition: Config.php:107
getRouteByFrontName($frontName, $scope=null)
Definition: Config.php:118
__construct(Config\Reader $reader, \Magento\Framework\Config\CacheInterface $cache, \Magento\Framework\Config\ScopeInterface $configScope, \Magento\Framework\App\AreaList $areaList, $cacheId='RoutesConfig')
Definition: Config.php:56