Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically load routes in app controllers #6513

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions manager-bundle/src/Routing/RouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public function loadFromPlugins(): RouteCollection
if ($configFile = $this->getConfigFile()) {
$routes = $this->loader->getResolver()->resolve($configFile)->load($configFile);

if ($routes instanceof RouteCollection) {
$collection->addCollection($routes);
}
} elseif (is_dir($path = Path::join($this->projectDir, 'src/Controller'))) {
$routes = $this->loader->getResolver()->resolve($path)->load($path);

if ($routes instanceof RouteCollection) {
$collection->addCollection($routes);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route(path: '/bar', name: 'bar')]
class BarController
{
public function __invoke(): Response
{
return new Response('bar');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
App:
resource: ../src/Controller
84 changes: 84 additions & 0 deletions manager-bundle/tests/Routing/RouteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,95 @@
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class RouteLoaderTest extends ContaoTestCase
{
public function testLoadsRoutesYaml(): void
{
$loader = $this->createMock(YamlFileLoader::class);
$loader
->expects($this->once())
->method('load')
->with(\dirname(__DIR__).'/Fixtures/Routing/WithRoutingYaml/config/routes.yaml')
;

$loaderResolver = $this->createMock(LoaderResolverInterface::class);
$loaderResolver
->expects($this->once())
->method('resolve')
->willReturn($loader)
;

$loader = $this->createMock(LoaderInterface::class);
$loader
->expects($this->exactly(1))
->method('getResolver')
->willReturn($loaderResolver)
;

$pluginLoader = $this->createMock(PluginLoader::class);
$pluginLoader
->expects($this->once())
->method('getInstancesOf')
->with(PluginLoader::ROUTING_PLUGINS, true)
->willReturn([])
;

$routeLoader = new RouteLoader(
$loader,
$pluginLoader,
$this->createMock(ContaoKernel::class),
__DIR__.'/../Fixtures/Routing/WithRoutingYaml',
);

$routeLoader->loadFromPlugins();
}

public function testLoadsAppController(): void
{
$loader = $this->createMock(AnnotationDirectoryLoader::class);
$loader
->expects($this->once())
->method('load')
->with(\dirname(__DIR__).'/Fixtures/Routing/WithAppController/src/Controller')
;

$loaderResolver = $this->createMock(LoaderResolverInterface::class);
$loaderResolver
->expects($this->once())
->method('resolve')
->willReturn($loader)
;

$loader = $this->createMock(LoaderInterface::class);
$loader
->expects($this->exactly(1))
->method('getResolver')
->willReturn($loaderResolver)
;

$pluginLoader = $this->createMock(PluginLoader::class);
$pluginLoader
->expects($this->once())
->method('getInstancesOf')
->with(PluginLoader::ROUTING_PLUGINS, true)
->willReturn([])
;

$routeLoader = new RouteLoader(
$loader,
$pluginLoader,
$this->createMock(ContaoKernel::class),
__DIR__.'/../Fixtures/Routing/WithAppController',
);

$routeLoader->loadFromPlugins();
}

public function testLoadFromPlugins(): void
{
$loaderResolver = $this->createMock(LoaderResolverInterface::class);
Expand Down