diff --git a/Services/Agnostic/RoutesLoader.php b/Services/Agnostic/RoutesConfigurator.php similarity index 88% rename from Services/Agnostic/RoutesLoader.php rename to Services/Agnostic/RoutesConfigurator.php index e462a4d..19a9751 100644 --- a/Services/Agnostic/RoutesLoader.php +++ b/Services/Agnostic/RoutesConfigurator.php @@ -19,18 +19,18 @@ use Symfony\Component\Routing\RouterInterface; /** - * Class RoutesLoader + * Class RoutesConfigurator * Независимый от контейнера загрузчик роутов. * @package Prokl\BitrixSymfonyRouterBundle\Services\Agnostic * * @since 24.07.2021 */ -class RoutesLoader +class RoutesConfigurator { /** * @var RouterInterface $router Роутер. */ - private $router; + protected static$router; /** * @var ResourceCheckerConfigCacheFactory $cacheFactory @@ -82,7 +82,7 @@ public function __construct( $this->checker = new SelfCheckingResourceChecker(); $this->cacheFactory = new ResourceCheckerConfigCacheFactory([$this->checker]); - $this->router = new Router( + static::$router = new Router( $delegatingLoader, $configFile, [ @@ -129,7 +129,7 @@ public function getRoutes() : RouteCollection } } - return $this->router->getRouteCollection(); + return static::$router->getRouteCollection(); } /** @@ -153,11 +153,23 @@ public function purgeCache() : void } /** + * Экземпляр роутера. + * + * @return RouterInterface|null + */ + public static function getInstance(): ?RouterInterface + { + return static::$router; + } + + /** + * Экземпляр роутера. + * * @return RouterInterface */ public function getRouter(): RouterInterface { - return $this->router; + return static::$router; } /** @@ -172,7 +184,7 @@ private function warmUpCache() : void } /** @psalm-suppress UndefinedInterfaceMethod */ - $this->router->setConfigCacheFactory($this->cacheFactory); + static::$router->setConfigCacheFactory($this->cacheFactory); if ($this->cacheFreshChecker !== null && !$this->cacheFreshChecker->isFresh()) { if (!@file_exists($this->cacheDir)) { @@ -181,10 +193,10 @@ private function warmUpCache() : void file_put_contents( $this->cacheDir . '/route_collection.json', - serialize($this->router->getRouteCollection()) + serialize(static::$router->getRouteCollection()) ); } - $this->router->getGenerator(); // Трюк по созданию кэша. + static::$router->getGenerator(); // Трюк по созданию кэша. } } \ No newline at end of file diff --git a/Services/Utils/BitrixRouteConvertor.php b/Services/Utils/BitrixRouteConvertor.php index ce8267f..174d01c 100644 --- a/Services/Utils/BitrixRouteConvertor.php +++ b/Services/Utils/BitrixRouteConvertor.php @@ -126,6 +126,14 @@ public function convertRoute(string $name, Route $route, RoutingConfigurator $ro } } + /** + * @return RouteCollection + */ + public function getRouteCollection(): RouteCollection + { + return $this->routeCollection; + } + /** * Парсинг строки _controller. * diff --git a/Tests/Cases/AgnosticRoutesLoaderTest.php b/Tests/Cases/AgnosticRoutesLoaderTest.php index 4e6e36e..26073d2 100644 --- a/Tests/Cases/AgnosticRoutesLoaderTest.php +++ b/Tests/Cases/AgnosticRoutesLoaderTest.php @@ -2,21 +2,21 @@ namespace Prokl\BitrixSymfonyRouterBundle\Tests\Cases; -use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesLoader; +use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator; use Prokl\TestingTools\Base\BaseTestCase; use Symfony\Component\Filesystem\Filesystem; /** * Class AgnosticRoutesLoaderTest * @package Prokl\BitrixSymfonyRouterBundle\Tests - * @coversDefaultClass RoutesLoader + * @coversDefaultClass RoutesConfigurator * * @since 24.07.2021 */ class AgnosticRoutesLoaderTest extends BaseTestCase { /** - * @var RoutesLoader $obTestObject Тестируемый объект. + * @var RoutesConfigurator $obTestObject Тестируемый объект. */ protected $obTestObject; @@ -44,7 +44,7 @@ protected function setUp(): void $this->filesystem = new Filesystem(); - $this->obTestObject = new RoutesLoader( + $this->obTestObject = new RoutesConfigurator( $this->routesConfig, null, true @@ -85,7 +85,7 @@ public function testGetRoutes() : void */ public function testCaching() : void { - $this->obTestObject = new RoutesLoader( + $this->obTestObject = new RoutesConfigurator( $this->routesConfig, $this->cacheDir, true @@ -103,7 +103,7 @@ public function testCaching() : void */ public function testPurgeCache(): void { - $this->obTestObject = new RoutesLoader( + $this->obTestObject = new RoutesConfigurator( $this->routesConfig, $this->cacheDir, true diff --git a/readme.MD b/readme.MD index a51ed27..c84acea 100644 --- a/readme.MD +++ b/readme.MD @@ -37,14 +37,14 @@ ```php use Prokl\ServiceProvider\ServiceProvider; use Bitrix\Main\Routing\Controllers\PublicPageController; -use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesLoader; +use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator; use Prokl\BitrixSymfonyRouterBundle\Services\Utils\BitrixRouteConvertor; use Bitrix\Main\Routing\RoutingConfigurator; // Не обязательно. Смотри ниже. $container = ServiceProvider::instance(); -$agnosticRouter = new RoutesLoader( +$agnosticRouter = new RoutesConfigurator( $_SERVER['DOCUMENT_ROOT'] . '/local/configs/bitrix_routes.yaml', // Конфиг роутов $_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/routes', // Кэш; если null - без кэширования. $_ENV['DEBUG'] @@ -119,11 +119,11 @@ public_page: `init.php`: ```php -use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesLoader; +use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator; use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\Router; use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\BitrixInitializerRouter; -$agnosticRouter = new RoutesLoader( +$agnosticRouter = new RoutesConfigurator( $_SERVER['DOCUMENT_ROOT'] . '/local/configs/standalone_routes.yaml', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/cache/routes_agnostic', // Кэш; если null - без кэширования. $_ENV['DEBUG'] // Режим отладки или нет @@ -137,4 +137,12 @@ $agnosticRouterInstance = new Router( Все. Подтянутся роуты из `/local/configs/standalone_routes.yaml`. Автоматически подцепятся события. -Допускается наличие нескольких таких "агностических" роутеров в один момент. \ No newline at end of file +Допускается наличие нескольких таких "агностических" роутеров в один момент. + +## Прочее + +1) Экземпляр `Symfony\Component\Routing\Router` можно получить снаружи так: + +```php +$router = \Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator::getInstance(); +``` \ No newline at end of file