Skip to content

Commit 46b8cd2

Browse files
committed
Доработки
1 parent 31e3f4a commit 46b8cd2

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

Services/Agnostic/RoutesLoader.php renamed to Services/Agnostic/RoutesConfigurator.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
use Symfony\Component\Routing\RouterInterface;
2020

2121
/**
22-
* Class RoutesLoader
22+
* Class RoutesConfigurator
2323
* Независимый от контейнера загрузчик роутов.
2424
* @package Prokl\WpSymfonyRouterBundle\Services\Agnostic
2525
*
2626
* @since 24.07.2021
2727
*/
28-
class RoutesLoader
28+
class RoutesConfigurator
2929
{
3030
/**
3131
* @var RouterInterface $router Роутер.
3232
*/
33-
private $router;
33+
protected 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,24 @@ public function purgeCache() : void
153153
}
154154

155155
/**
156+
* Экземпляр роутера.
157+
*
158+
* @return RouterInterface|null
159+
*/
160+
public static function getInstance(): ?RouterInterface
161+
{
162+
return static::$router;
163+
}
164+
165+
/**
166+
*
167+
* Экземпляр роутера.
168+
*
156169
* @return RouterInterface
157170
*/
158171
public function getRouter(): RouterInterface
159172
{
160-
return $this->router;
173+
return static::$router;
161174
}
162175

163176
/**
@@ -172,7 +185,7 @@ private function warmUpCache() : void
172185
}
173186

174187
/** @psalm-suppress UndefinedInterfaceMethod */
175-
$this->router->setConfigCacheFactory($this->cacheFactory);
188+
static::$router->setConfigCacheFactory($this->cacheFactory);
176189

177190
if ($this->cacheFreshChecker !== null && !$this->cacheFreshChecker->isFresh()) {
178191
if (!@file_exists($this->cacheDir)) {
@@ -185,6 +198,6 @@ private function warmUpCache() : void
185198
);
186199
}
187200

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

Tests/Cases/AgnosticRoutesLoaderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace Prokl\WpSymfonyRouterBundle\Tests\Cases;
44

5-
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesLoader;
5+
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator;
66
use Prokl\TestingTools\Base\BaseTestCase;
77
use Symfony\Component\Filesystem\Filesystem;
88

99
/**
1010
* Class AgnosticRoutesLoaderTest
1111
* @package Prokl\BitrixSymfonyRouterBundle\Tests
12-
* @coversDefaultClass RoutesLoader
12+
* @coversDefaultClass RoutesConfigurator
1313
*
1414
* @since 24.07.2021
1515
*/
1616
class AgnosticRoutesLoaderTest extends BaseTestCase
1717
{
1818
/**
19-
* @var RoutesLoader $obTestObject Тестируемый объект.
19+
* @var RoutesConfigurator $obTestObject Тестируемый объект.
2020
*/
2121
protected $obTestObject;
2222

@@ -44,7 +44,7 @@ protected function setUp(): void
4444

4545
$this->filesystem = new Filesystem();
4646

47-
$this->obTestObject = new RoutesLoader(
47+
$this->obTestObject = new RoutesConfigurator(
4848
$this->routesConfig,
4949
null,
5050
true
@@ -85,7 +85,7 @@ public function testGetRoutes() : void
8585
*/
8686
public function testCaching() : void
8787
{
88-
$this->obTestObject = new RoutesLoader(
88+
$this->obTestObject = new RoutesConfigurator(
8989
$this->routesConfig,
9090
$this->cacheDir,
9191
true
@@ -103,7 +103,7 @@ public function testCaching() : void
103103
*/
104104
public function testpurgeCache(): void
105105
{
106-
$this->obTestObject = new RoutesLoader(
106+
$this->obTestObject = new RoutesConfigurator(
107107
$this->routesConfig,
108108
$this->cacheDir,
109109
true

readme.MD

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ class SampleAjaxController extends AbstractWPAjaxController
7373
`functions.php`:
7474

7575
```php
76-
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesLoader;
76+
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator;
7777
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\Router;
7878
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\WpInitializerRouter;
7979
80-
$agnosticRouter = new RoutesLoader(
80+
$agnosticRouter = new RoutesConfigurator(
8181
$_SERVER['DOCUMENT_ROOT'] . '/local/configs/standalone_routes.yaml',
8282
$_SERVER['DOCUMENT_ROOT'] . '/wp-content/cache/routes', // Кэш; если null - без кэширования.
8383
$_ENV['APP_DEBUG'] // Режим отладки или нет
@@ -91,4 +91,12 @@ $agnosticRouterInstance = new Router(
9191

9292
Все. Подтянутся роуты из `/local/configs/standalone_routes.yaml`. Автоматически подцепятся события.
9393

94-
Допускается наличие нескольких таких "агностических" роутеров в один момент.
94+
Допускается наличие нескольких таких "агностических" роутеров в один момент.
95+
96+
## Прочее
97+
98+
1) Экземпляр `Symfony\Component\Routing\Router` можно получить снаружи так:
99+
100+
```php
101+
$router = \Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator::getInstance();
102+
```

0 commit comments

Comments
 (0)