Skip to content

Commit 6dcc642

Browse files
committed
Agnostic routes loader.
1 parent e950470 commit 6dcc642

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

Services/Agnostic/RoutesLoader.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Services\Agnostic;
4+
5+
use Symfony\Component\Config\FileLocator;
6+
use Symfony\Component\Config\Loader\DelegatingLoader;
7+
use Symfony\Component\Config\Loader\LoaderResolver;
8+
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
9+
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\Routing\Loader\PhpFileLoader;
12+
use Symfony\Component\Routing\Loader\XmlFileLoader;
13+
use Symfony\Component\Routing\Loader\YamlFileLoader;
14+
use Symfony\Component\Routing\RequestContext;
15+
use Symfony\Component\Routing\RouteCollection;
16+
use Symfony\Component\Routing\Router;
17+
use Symfony\Component\Routing\RouterInterface;
18+
19+
/**
20+
* Class RoutesLoader
21+
* Независимый от контейнера загрузчик роутов.
22+
* @package Prokl\WpSymfonyRouterBundle\Services\Agnostic
23+
*
24+
* @since 24.07.2021
25+
*/
26+
class RoutesLoader
27+
{
28+
/**
29+
* @var RouterInterface $router Роутер.
30+
*/
31+
private $router;
32+
33+
/**
34+
* AgnosticRouteLoader constructor.
35+
*
36+
* @param string $configFile Yaml/php/xml файл с конфигурацией роутов.
37+
* @param string|null $cacheDir Путь к кэшу. Null -> не кэшировать.
38+
* @param boolean $debug Режим отладки.
39+
*/
40+
public function __construct(
41+
string $configFile,
42+
?string $cacheDir = null,
43+
bool $debug = true
44+
) {
45+
$resolver = new LoaderResolver(
46+
[
47+
new YamlFileLoader(new FileLocator()),
48+
new PhpFileLoader(new FileLocator()),
49+
new XmlFileLoader(new FileLocator()),
50+
]
51+
);
52+
53+
$delegatingLoader = new DelegatingLoader($resolver);
54+
55+
$requestContext = new RequestContext();
56+
$request = Request::createFromGlobals();
57+
58+
$checker = new SelfCheckingResourceChecker();
59+
$cacheFactory = new ResourceCheckerConfigCacheFactory([$checker]);
60+
61+
$this->router = new Router(
62+
$delegatingLoader,
63+
$configFile,
64+
[
65+
'cache_dir' => $cacheDir,
66+
'debug' => $debug,
67+
'generator_class' => 'Symfony\Component\Routing\Generator\CompiledUrlGenerator',
68+
'generator_dumper_class' => 'Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper',
69+
'matcher_class' => 'Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher',
70+
'matcher_dumper_class' => 'Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper'
71+
],
72+
$requestContext->fromRequest($request)
73+
);
74+
75+
if ($cacheDir) {
76+
$this->router->setConfigCacheFactory($cacheFactory);
77+
}
78+
}
79+
80+
/**
81+
* Роуты.
82+
*
83+
* @return RouteCollection
84+
*/
85+
public function getRoutes() : RouteCollection
86+
{
87+
return $this->router->getRouteCollection();
88+
}
89+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Tests\Cases;
4+
5+
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesLoader;
6+
use Prokl\TestingTools\Base\BaseTestCase;
7+
8+
/**
9+
* Class AgnosticRoutesLoaderTest
10+
* @package Prokl\BitrixSymfonyRouterBundle\Tests
11+
* @coversDefaultClass RoutesLoader
12+
*
13+
* @since 24.07.2021
14+
*/
15+
class AgnosticRoutesLoaderTest extends BaseTestCase
16+
{
17+
/**
18+
* @var RoutesLoader $obTestObject Тестируемый объект.
19+
*/
20+
protected $obTestObject;
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
protected function setUp(): void
26+
{
27+
parent::setUp();
28+
$this->obTestObject = new RoutesLoader(
29+
__DIR__ . '/../Fixture/agnostic_routes.yaml',
30+
null,
31+
true
32+
);
33+
}
34+
35+
/**
36+
* getRoutes().
37+
*
38+
* @return void
39+
*/
40+
public function testGetRoutes() : void
41+
{
42+
$result = $this->obTestObject->getRoutes();
43+
44+
$routes = $result->get('first_agnostic_route');
45+
46+
$this->assertSame($routes->getPath(), '/foo/{param}/');
47+
}
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Prokl\WpSymfonyRouterBundle\Tests\Fixture;
4+
5+
/**
6+
* Class ExampleBitrixActionController
7+
* @package Prokl\WpSymfonyRouterBundle\Tests\Fixture
8+
*
9+
* @since 24.07.2021
10+
*/
11+
class ExampleAgnosticController
12+
{
13+
/**
14+
* @return string
15+
*/
16+
public static function getControllerClass() {
17+
return ExampleAgnosticController::class;
18+
}
19+
20+
/**
21+
* @return string
22+
*/
23+
public static function getDefaultName() {
24+
return 'testingAction';
25+
}
26+
27+
public function cacheAction(string $country)
28+
{
29+
return ['cacheDir' => 'test', 'country' => $country];
30+
}
31+
32+
public function configureActions()
33+
{
34+
return [
35+
'cache' => [
36+
'prefilters' => [], 'postfilters' => [],
37+
],
38+
];
39+
}
40+
}

Tests/Fixture/agnostic_routes.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
first_agnostic_route:
2+
path: /foo/{param}/
3+
controller: 'Prokl\WpSymfonyRouterBundle\Tests\Fixture::cacheAction'
4+
methods: GET|POST
5+
requirements:
6+
param: '\d+'
7+
defaults:
8+
param: 'Russia'
9+
10+

0 commit comments

Comments
 (0)