Skip to content

Commit

Permalink
Fixed compatibility with sf5/6/7
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper committed Jan 21, 2024
1 parent 7a1930b commit d4a74ee
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 42 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ parameters:

excludePaths:
- '%currentWorkingDirectory%/src/DependencyInjection/Configuration.php'
- '%currentWorkingDirectory%/src/Routing/RoutingLoader.php'

ignoreErrors:
95 changes: 69 additions & 26 deletions src/Routing/RoutingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Check warning on line 33 in src/Routing/RoutingLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Routing/RoutingLoader.php#L33

Added line #L33 was not covered by tests
}

$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');

Check warning on line 72 in src/Routing/RoutingLoader.php

View check run for this annotation

Codecov / codecov/patch

src/Routing/RoutingLoader.php#L72

Added line #L72 was not covered by tests
}

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;
}
}
}
21 changes: 6 additions & 15 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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');
}
}
}
2 changes: 1 addition & 1 deletion tests/App/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ services:
autowire: true
autoconfigure: true

DH\AuditorBundle\Tests\App\Command\CreatePostCommand:
DH\AuditorBundle\Tests\App\Command\CreatePostCommand:
13 changes: 13 additions & 0 deletions tests/App/config/services_legacy.yaml
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions tests/DHAuditorBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit d4a74ee

Please # to comment.