diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 5df8dfb..ccbadfa 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -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 diff --git a/Services/Agnostic/RoutesLoader.php b/Services/Agnostic/RoutesLoader.php new file mode 100644 index 0000000..264dfd2 --- /dev/null +++ b/Services/Agnostic/RoutesLoader.php @@ -0,0 +1,89 @@ + не кэшировать. + * @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(); + } +} \ No newline at end of file diff --git a/Tests/Cases/AgnosticRoutesLoaderTest.php b/Tests/Cases/AgnosticRoutesLoaderTest.php new file mode 100644 index 0000000..d5e60ff --- /dev/null +++ b/Tests/Cases/AgnosticRoutesLoaderTest.php @@ -0,0 +1,48 @@ +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}/'); + } +} diff --git a/Tests/Fixture/ExampleBitrixActionController.php b/Tests/Fixture/ExampleBitrixActionController.php new file mode 100644 index 0000000..7c6a143 --- /dev/null +++ b/Tests/Fixture/ExampleBitrixActionController.php @@ -0,0 +1,40 @@ + 'test', 'country' => $country]; + } + + public function configureActions() + { + return [ + 'cache' => [ + 'prefilters' => [], 'postfilters' => [], + ], + ]; + } +} \ No newline at end of file diff --git a/Tests/Fixture/bitrix_routes.yaml b/Tests/Fixture/bitrix_routes.yaml new file mode 100644 index 0000000..751eb2a --- /dev/null +++ b/Tests/Fixture/bitrix_routes.yaml @@ -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' + +