Skip to content

Commit

Permalink
Agnostic routes loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 24, 2021
1 parent 1da6f2b commit e62324b
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ services:
arguments:
- '@routing.loader'
- '%kernel.project_dir%/%router.config.file%'
- cache_dir: '%kernel.cache_dir%'
- cache_dir: '%router.cache.path%'
debug: '%kernel.debug%'
generator_class: Symfony\Component\Routing\Generator\CompiledUrlGenerator
generator_dumper_class: Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper
Expand Down
89 changes: 89 additions & 0 deletions Services/Agnostic/RoutesLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Services\Agnostic;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Loader\XmlFileLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouterInterface;

/**
* Class RoutesLoader
* Независимый от контейнера загрузчик роутов.
* @package Prokl\BitrixSymfonyRouterBundle\Services\Agnostic
*
* @since 24.07.2021
*/
class RoutesLoader
{
/**
* @var RouterInterface $router Роутер.
*/
private $router;

/**
* AgnosticRouteLoader constructor.
*
* @param string $configFile Yaml/php/xml файл с конфигурацией роутов.
* @param string|null $cacheDir Путь к кэшу. Null -> не кэшировать.
* @param boolean $debug Режим отладки.
*/
public function __construct(
string $configFile,
?string $cacheDir = null,
bool $debug = true
) {
$resolver = new LoaderResolver(
[
new YamlFileLoader(new FileLocator()),
new PhpFileLoader(new FileLocator()),
new XmlFileLoader(new FileLocator()),
]
);

$delegatingLoader = new DelegatingLoader($resolver);

$requestContext = new RequestContext();
$request = Request::createFromGlobals();

$checker = new SelfCheckingResourceChecker();
$cacheFactory = new ResourceCheckerConfigCacheFactory([$checker]);

$this->router = new Router(
$delegatingLoader,
$configFile,
[
'cache_dir' => $cacheDir,
'debug' => $debug,
'generator_class' => 'Symfony\Component\Routing\Generator\CompiledUrlGenerator',
'generator_dumper_class' => 'Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper',
'matcher_class' => 'Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher',
'matcher_dumper_class' => 'Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper'
],
$requestContext->fromRequest($request)
);

if ($cacheDir) {
$this->router->setConfigCacheFactory($cacheFactory);
}
}

/**
* Роуты.
*
* @return RouteCollection
*/
public function getRoutes() : RouteCollection
{
return $this->router->getRouteCollection();
}
}
48 changes: 48 additions & 0 deletions Tests/Cases/AgnosticRoutesLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Tests\Cases;

use Prokl\BitrixSymfonyRouterBundle\Services\Agnostic\RoutesLoader;
use Prokl\TestingTools\Base\BaseTestCase;

/**
* Class AgnosticRoutesLoaderTest
* @package Prokl\BitrixSymfonyRouterBundle\Tests
* @coversDefaultClass RoutesLoader
*
* @since 24.07.2021
*/
class AgnosticRoutesLoaderTest extends BaseTestCase
{
/**
* @var RoutesLoader $obTestObject Тестируемый объект.
*/
protected $obTestObject;

/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->obTestObject = new RoutesLoader(
__DIR__ . '/../Fixture/bitrix_routes.yaml',
null,
true
);
}

/**
* getRoutes().
*
* @return void
*/
public function testGetRoutes() : void
{
$result = $this->obTestObject->getRoutes();

$routes = $result->get('first_bitrix_route');

$this->assertSame($routes->getPath(), '/foo/{param}/');
}
}
40 changes: 40 additions & 0 deletions Tests/Fixture/ExampleBitrixActionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Prokl\BitrixSymfonyRouterBundle\Tests\Fixture;

/**
* Class ExampleBitrixActionController
* @package Prokl\BitrixSymfonyRouterBundle\Tests\Fixture
*
* @since 24.07.2021
*/
class ExampleBitrixActionController
{
/**
* @return string
*/
public static function getControllerClass() {
return ExampleBitrixActionController::class;
}

/**
* @return string
*/
public static function getDefaultName() {
return 'testingAction';
}

public function cacheAction(string $country)
{
return ['cacheDir' => 'test', 'country' => $country];
}

public function configureActions()
{
return [
'cache' => [
'prefilters' => [], 'postfilters' => [],
],
];
}
}
10 changes: 10 additions & 0 deletions Tests/Fixture/bitrix_routes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
first_bitrix_route:
path: /foo/{param}/
controller: 'Prokl\BitrixSymfonyRouterBundle\Tests\Fixture::cacheAction'
methods: GET|POST
requirements:
param: '\d+'
defaults:
param: 'Russia'


0 comments on commit e62324b

Please sign in to comment.