Skip to content

Commit

Permalink
Доработки
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 26, 2021
1 parent 930d125 commit 59ca757
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
[
Expand Down Expand Up @@ -129,7 +129,7 @@ public function getRoutes() : RouteCollection
}
}

return $this->router->getRouteCollection();
return static::$router->getRouteCollection();
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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)) {
Expand All @@ -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(); // Трюк по созданию кэша.
}
}
8 changes: 8 additions & 0 deletions Services/Utils/BitrixRouteConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public function convertRoute(string $name, Route $route, RoutingConfigurator $ro
}
}

/**
* @return RouteCollection
*/
public function getRouteCollection(): RouteCollection
{
return $this->routeCollection;
}

/**
* Парсинг строки _controller.
*
Expand Down
12 changes: 6 additions & 6 deletions Tests/Cases/AgnosticRoutesLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -44,7 +44,7 @@ protected function setUp(): void

$this->filesystem = new Filesystem();

$this->obTestObject = new RoutesLoader(
$this->obTestObject = new RoutesConfigurator(
$this->routesConfig,
null,
true
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 13 additions & 5 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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'] // Режим отладки или нет
Expand All @@ -137,4 +137,12 @@ $agnosticRouterInstance = new Router(

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

Допускается наличие нескольких таких "агностических" роутеров в один момент.
Допускается наличие нескольких таких "агностических" роутеров в один момент.

## Прочее

1) Экземпляр `Symfony\Component\Routing\Router` можно получить снаружи так:

```php
$router = \Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator::getInstance();
```

0 comments on commit 59ca757

Please sign in to comment.