-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}/'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => [], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
|