Skip to content

Commit 58f11c0

Browse files
committed
feature #32937 [Routing] Deprecate RouteCollectionBuilder (vudaltsov)
This PR was squashed before being merged into the 5.1-dev branch (closes #32937). Discussion ---------- [Routing] Deprecate RouteCollectionBuilder | Q | A | ------------- | --- | Branch? | 5.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #32240 | License | MIT | Doc PR | symfony/symfony-docs#12688 | Recipe PR | symfony/recipes#690 A lot to be done here after the implementation is accepted: - [x] finish deprecations in the MicroKernelTrait - [x] deprecate the class - [x] mention in the CHANGELOG file - [x] mention in the UPGRADE file - [x] mark tests as legacy - [x] add a doc PR - [x] update the recipe Ping @Tobion , @nicolas-grekas . Commits ------- e641cbdd46 [Routing] Deprecate RouteCollectionBuilder
2 parents 21c3f17 + 00c4855 commit 58f11c0

File tree

6 files changed

+128
-7
lines changed

6 files changed

+128
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
8+
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.
9+
410
5.0.0
511
-----
612

Kernel/MicroKernelTrait.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Loader\LoaderInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
1718
use Symfony\Component\Routing\RouteCollectionBuilder;
1819

1920
/**
@@ -29,8 +30,28 @@ trait MicroKernelTrait
2930
*
3031
* $routes->import('config/routing.yml');
3132
* $routes->add('/admin', 'App\Controller\AdminController::dashboard', 'admin_dashboard');
33+
*
34+
* @final since Symfony 5.1, override configureRouting() instead
35+
*
36+
* @internal since Symfony 5.1, use configureRouting() instead
37+
*/
38+
protected function configureRoutes(RouteCollectionBuilder $routes)
39+
{
40+
}
41+
42+
/**
43+
* Adds or imports routes into your application.
44+
*
45+
* $routes->import($this->getProjectDir().'/config/*.{yaml,php}');
46+
* $routes
47+
* ->add('admin_dashboard', '/admin')
48+
* ->controller('App\Controller\AdminController::dashboard')
49+
* ;
3250
*/
33-
abstract protected function configureRoutes(RouteCollectionBuilder $routes);
51+
protected function configureRouting(RoutingConfigurator $routes): void
52+
{
53+
@trigger_error(sprintf('Not overriding the "%s()" method is deprecated since Symfony 5.1 and will trigger a fatal error in 6.0.', __METHOD__), E_USER_DEPRECATED);
54+
}
3455

3556
/**
3657
* Configures the container.
@@ -91,7 +112,15 @@ public function loadRoutes(LoaderInterface $loader)
91112
{
92113
$routes = new RouteCollectionBuilder($loader);
93114
$this->configureRoutes($routes);
115+
$collection = $routes->build();
116+
117+
if (0 !== \count($collection)) {
118+
@trigger_error(sprintf('Adding routes via the "%s:configureRoutes()" method is deprecated since Symfony 5.1 and will have no effect in 6.0; use "configureRouting()" instead.', self::class), E_USER_DEPRECATED);
119+
}
120+
121+
$file = (new \ReflectionObject($this))->getFileName();
122+
$this->configureRouting(new RoutingConfigurator($collection, $loader, null, $file));
94123

95-
return $routes->build();
124+
return $collection;
96125
}
97126
}

Tests/Kernel/ConcreteMicroKernel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2323
use Symfony\Component\HttpKernel\Kernel;
2424
use Symfony\Component\HttpKernel\KernelEvents;
25-
use Symfony\Component\Routing\RouteCollectionBuilder;
25+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
2626

2727
class ConcreteMicroKernel extends Kernel implements EventSubscriberInterface
2828
{
@@ -80,10 +80,10 @@ public function __destruct()
8080
$fs->remove($this->cacheDir);
8181
}
8282

83-
protected function configureRoutes(RouteCollectionBuilder $routes)
83+
protected function configureRouting(RoutingConfigurator $routes): void
8484
{
85-
$routes->add('/', 'kernel::halloweenAction');
86-
$routes->add('/danger', 'kernel::dangerousAction');
85+
$routes->add('halloween', '/')->controller('kernel::halloweenAction');
86+
$routes->add('danger', '/danger')->controller('kernel::dangerousAction');
8787
}
8888

8989
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)

Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919

2020
class MicroKernelTraitTest extends TestCase
2121
{
22+
/**
23+
* @group legacy
24+
* @expectedDeprecation Adding routes via the "Symfony\Bundle\FrameworkBundle\Tests\Kernel\MicroKernelWithConfigureRoutes:configureRoutes()" method is deprecated since Symfony 5.1 and will have no effect in 6.0; use "configureRouting()" instead.
25+
* @expectedDeprecation Not overriding the "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait::configureRouting()" method is deprecated since Symfony 5.1 and will trigger a fatal error in 6.0.
26+
*/
27+
public function testConfigureRoutingDeprecated()
28+
{
29+
$kernel = new MicroKernelWithConfigureRoutes('test', false);
30+
$kernel->boot();
31+
$kernel->handle(Request::create('/'));
32+
}
33+
2234
public function test()
2335
{
2436
$kernel = new ConcreteMicroKernel('test', false);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;
13+
14+
use Psr\Log\NullLogger;
15+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
16+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
17+
use Symfony\Component\Config\Loader\LoaderInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\Filesystem\Filesystem;
20+
use Symfony\Component\HttpKernel\Kernel;
21+
use Symfony\Component\Routing\RouteCollectionBuilder;
22+
23+
class MicroKernelWithConfigureRoutes extends Kernel
24+
{
25+
use MicroKernelTrait;
26+
27+
private $cacheDir;
28+
29+
public function registerBundles(): iterable
30+
{
31+
return [
32+
new FrameworkBundle(),
33+
];
34+
}
35+
36+
public function getCacheDir(): string
37+
{
38+
return $this->cacheDir = sys_get_temp_dir().'/sf_micro_kernel_with_configured_routes';
39+
}
40+
41+
public function getLogDir(): string
42+
{
43+
return $this->cacheDir;
44+
}
45+
46+
public function __sleep(): array
47+
{
48+
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
49+
}
50+
51+
public function __wakeup()
52+
{
53+
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
54+
}
55+
56+
public function __destruct()
57+
{
58+
$fs = new Filesystem();
59+
$fs->remove($this->cacheDir);
60+
}
61+
62+
protected function configureRoutes(RouteCollectionBuilder $routes)
63+
{
64+
$routes->add('/', 'kernel::halloweenAction');
65+
}
66+
67+
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
68+
{
69+
$c->register('logger', NullLogger::class);
70+
$c->loadFromExtension('framework', [
71+
'secret' => '$ecret',
72+
]);
73+
}
74+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"symfony/polyfill-mbstring": "~1.0",
2727
"symfony/filesystem": "^4.4|^5.0",
2828
"symfony/finder": "^4.4|^5.0",
29-
"symfony/routing": "^5.0"
29+
"symfony/routing": "^5.1"
3030
},
3131
"require-dev": {
3232
"doctrine/annotations": "~1.7",

0 commit comments

Comments
 (0)