diff --git a/phpstan.neon b/phpstan.neon index 265d1dd4..4bf5719a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -10,5 +10,6 @@ parameters: excludePaths: - '%currentWorkingDirectory%/src/DependencyInjection/Configuration.php' + - '%currentWorkingDirectory%/src/Routing/RoutingLoader.php' ignoreErrors: diff --git a/src/Routing/RoutingLoader.php b/src/Routing/RoutingLoader.php index c1a43922..80a4f057 100644 --- a/src/Routing/RoutingLoader.php +++ b/src/Routing/RoutingLoader.php @@ -6,45 +6,88 @@ use DH\AuditorBundle\Controller\ViewerController; use RuntimeException; +use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader; use Symfony\Bundle\FrameworkBundle\Routing\AttributeRouteControllerLoader; use Symfony\Component\Config\Loader\Loader; +use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Routing\RouteCollection; -class RoutingLoader extends Loader -{ - private AttributeRouteControllerLoader $annotatedRouteControllerLoader; +if (BaseKernel::MAJOR_VERSION >= 6) { + class RoutingLoader extends Loader + { + private AttributeRouteControllerLoader $annotatedRouteControllerLoader; - private bool $isLoaded = false; + private bool $isLoaded = false; - private array $configuration; + private array $configuration; - public function __construct(AttributeRouteControllerLoader $annotatedRouteController, array $configuration) - { - $this->annotatedRouteControllerLoader = $annotatedRouteController; - $this->configuration = $configuration; - } + public function __construct(AttributeRouteControllerLoader $annotatedRouteController, array $configuration) + { + $this->annotatedRouteControllerLoader = $annotatedRouteController; + $this->configuration = $configuration; + } - public function load(mixed $resource, ?string $type = null): RouteCollection - { - if ($this->isLoaded) { - throw new RuntimeException('Do not add the "audit" loader twice'); + public function load(mixed $resource, ?string $type = null): RouteCollection + { + if ($this->isLoaded) { + throw new RuntimeException('Do not add the "audit" loader twice'); + } + + $routeCollection = new RouteCollection(); + if (true === $this->configuration['viewer']) { + $routeCollection = $this->annotatedRouteControllerLoader->load(ViewerController::class); + } + + $this->isLoaded = true; + + return $routeCollection; + } + + /** + * @param ?string $type + */ + public function supports(mixed $resource, ?string $type = null): bool + { + return 'auditor' === $type; } + } +} else { + class RoutingLoader extends Loader + { + private AnnotatedRouteControllerLoader $annotatedRouteControllerLoader; + + private bool $isLoaded = false; - $routeCollection = new RouteCollection(); - if (true === $this->configuration['viewer']) { - $routeCollection = $this->annotatedRouteControllerLoader->load(ViewerController::class); + private array $configuration; + + public function __construct(AnnotatedRouteControllerLoader $annotatedRouteController, array $configuration) + { + $this->annotatedRouteControllerLoader = $annotatedRouteController; + $this->configuration = $configuration; } - $this->isLoaded = true; + public function load(mixed $resource, ?string $type = null): RouteCollection + { + if ($this->isLoaded) { + throw new RuntimeException('Do not add the "audit" loader twice'); + } - return $routeCollection; - } + $routeCollection = new RouteCollection(); + if (true === $this->configuration['viewer']) { + $routeCollection = $this->annotatedRouteControllerLoader->load(ViewerController::class); + } - /** - * @param ?string $type - */ - public function supports(mixed $resource, ?string $type = null): bool - { - return 'auditor' === $type; + $this->isLoaded = true; + + return $routeCollection; + } + + /** + * @param ?string $type + */ + public function supports(mixed $resource, ?string $type = null): bool + { + return 'auditor' === $type; + } } } diff --git a/tests/App/Kernel.php b/tests/App/Kernel.php index a2f2d666..94978ddf 100644 --- a/tests/App/Kernel.php +++ b/tests/App/Kernel.php @@ -10,7 +10,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel as BaseKernel; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; -use Symfony\Component\Routing\RouteCollectionBuilder; if (BaseKernel::MAJOR_VERSION >= 6) { class Kernel extends BaseKernel @@ -51,12 +50,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void $confDir = $this->getProjectDir().'/config'; $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, 'glob'); - - if (BaseKernel::MAJOR_VERSION < 6) { - $routes->import($confDir.'/routes/sf4_5/*'.self::CONFIG_EXTS, 'glob'); - } else { - $routes->import($confDir.'/routes/sf6_7/*'.self::CONFIG_EXTS, 'glob'); - } + $routes->import($confDir.'/routes/sf6_7/*'.self::CONFIG_EXTS, 'glob'); } } } else { @@ -88,20 +82,17 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $container->setParameter('container.dumper.inline_factories', true); $confDir = $this->getProjectDir().'/config'; - $loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob'); + $loader->load($confDir.'/services_legacy'.self::CONFIG_EXTS, 'glob'); $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob'); - if (BaseKernel::MAJOR_VERSION < 6) { - $loader->load($confDir.'/{packages}/sf4_5/*'.self::CONFIG_EXTS, 'glob'); - } else { - $loader->load($confDir.'/{packages}/sf6_7/*'.self::CONFIG_EXTS, 'glob'); - } + $loader->load($confDir.'/{packages}/sf4_5/*'.self::CONFIG_EXTS, 'glob'); } - protected function configureRoutes(RouteCollectionBuilder $routes): void + protected function configureRoutes(RoutingConfigurator $routes): void { $confDir = $this->getProjectDir().'/config'; - $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob'); + $routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, 'glob'); + $routes->import($confDir.'/routes/sf4_5/*'.self::CONFIG_EXTS, 'glob'); } } } diff --git a/tests/App/config/services.yaml b/tests/App/config/services.yaml index 0c94e5eb..220c6f08 100644 --- a/tests/App/config/services.yaml +++ b/tests/App/config/services.yaml @@ -3,4 +3,4 @@ services: autowire: true autoconfigure: true - DH\AuditorBundle\Tests\App\Command\CreatePostCommand: \ No newline at end of file + DH\AuditorBundle\Tests\App\Command\CreatePostCommand: diff --git a/tests/App/config/services_legacy.yaml b/tests/App/config/services_legacy.yaml new file mode 100644 index 00000000..2c322276 --- /dev/null +++ b/tests/App/config/services_legacy.yaml @@ -0,0 +1,13 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + DH\AuditorBundle\Tests\App\Command\CreatePostCommand: + + DH\AuditorBundle\Routing\RoutingLoader: + arguments: + - '@routing.loader.annotation' + - '%dh_auditor.provider.doctrine.configuration%' + tags: + - { name: routing.loader } diff --git a/tests/DHAuditorBundleTest.php b/tests/DHAuditorBundleTest.php index 851b3691..e98d90d7 100644 --- a/tests/DHAuditorBundleTest.php +++ b/tests/DHAuditorBundleTest.php @@ -42,6 +42,7 @@ public function testInitBundle(): void $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/dh_auditor.yaml'); $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/doctrine.yaml'); if (Kernel::MAJOR_VERSION < 6) { + $kernel->addTestConfig(__DIR__.'/App/config/services_legacy.yaml'); $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/sf4_5/security.yaml'); } else { $kernel->addTestConfig(__DIR__.'/Fixtures/Resources/config/sf6_7/security.yaml');