Skip to content

Commit 76202a7

Browse files
committed
Получение экземпляра Router снаружи.
1 parent 0927c9f commit 76202a7

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/RoutesLoader.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RoutesLoader
3030
/**
3131
* @var RouterInterface $router Роутер.
3232
*/
33-
private $router;
33+
private static $router;
3434

3535
/**
3636
* @var ResourceCheckerConfigCacheFactory $cacheFactory
@@ -82,7 +82,7 @@ public function __construct(
8282
$this->checker = new SelfCheckingResourceChecker();
8383
$this->cacheFactory = new ResourceCheckerConfigCacheFactory([$this->checker]);
8484

85-
$this->router = new Router(
85+
static::$router = new Router(
8686
$delegatingLoader,
8787
$configFile,
8888
[
@@ -129,7 +129,7 @@ public function getRoutes() : RouteCollection
129129
}
130130
}
131131

132-
return $this->router->getRouteCollection();
132+
return static::$router->getRouteCollection();
133133
}
134134

135135
/**
@@ -153,11 +153,23 @@ public function purgeCache() : void
153153
}
154154

155155
/**
156+
* Экземпляр Symfony Router.
157+
*
158+
* @return RouterInterface|null
159+
*/
160+
public static function getInstance() : ?RouterInterface
161+
{
162+
return static::$router;
163+
}
164+
165+
/**
166+
* Экземпляр Symfony Router.
167+
*
156168
* @return RouterInterface
157169
*/
158170
public function getRouter(): RouterInterface
159171
{
160-
return $this->router;
172+
return static::$router;
161173
}
162174

163175
/**
@@ -172,7 +184,7 @@ private function warmUpCache() : void
172184
}
173185

174186
/** @psalm-suppress UndefinedInterfaceMethod */
175-
$this->router->setConfigCacheFactory($this->cacheFactory);
187+
static::$router->setConfigCacheFactory($this->cacheFactory);
176188

177189
if ($this->cacheFreshChecker !== null && !$this->cacheFreshChecker->isFresh()) {
178190
if (!@file_exists($this->cacheDir)) {
@@ -181,10 +193,10 @@ private function warmUpCache() : void
181193

182194
file_put_contents(
183195
$this->cacheDir . '/route_collection.json',
184-
serialize($this->router->getRouteCollection())
196+
serialize(static::$router->getRouteCollection())
185197
);
186198
}
187199

188-
$this->router->getGenerator(); // Трюк по созданию кэша.
200+
static::$router->getGenerator(); // Трюк по созданию кэша.
189201
}
190202
}

readme.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@ Loader::includeModule('proklung.symfony.router');
110110
Если пусто (или указанный файл не существует), то появится сообщение. Функционал работать не будет.
111111

112112
4) `Путь к папке с кэшом Yaml файлов нативных роутов Битрикс` - если не заполнено, то кэширования не будет.
113+
114+
## Прочее
115+
116+
1) Экземпляр `Symfony\Component\Routing\Router` можно получить снаружи так:
117+
118+
```php
119+
$router = \Proklung\Symfony\Router\RoutesLoader::getInstance();
120+
```

0 commit comments

Comments
 (0)