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 c628a9c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 39 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');
}

$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;
}
}
}
16 changes: 4 additions & 12 deletions tests/App/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,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 +83,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
{
$confDir = $this->getProjectDir().'/config';

$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 }

0 comments on commit c628a9c

Please # to comment.