8000 [Routing] Deprecate RouteCollectionBuilder · symfony/symfony@e641cbd · GitHub
[go: up one dir, main page]

Skip to content

Commit e641cbd

Browse files
vudaltsovfabpot
authored andcommitted
[Routing] Deprecate RouteCollectionBuilder
1 parent 9aa7492 commit e641cbd

12 files changed

+200
-12
lines changed

UPGRADE-5.1.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
UPGRADE FROM 5.0 to 5.1
2+
=======================
3+
4+
FrameworkBundle
5+
---------------
6+
7+
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
8+
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.
9+
10+
Routing
11+
-------
12+
13+
* Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`.

UPGRADE-6.0.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
UPGRADE FROM 5.x to 6.0
2+
=======================
3+
4+
FrameworkBundle
5+
---------------
6+
7+
* Removed `MicroKernelTrait::configureRoutes()`.
8+
* Made `MicroKernelTrait::configureRouting()` abstract.
9+
10+
Routing
11+
-------
12+
13+
* Removed `RouteCollectionBuilder`.

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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)

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +1 E377 9,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+
}

src/Symfony/Bundle/FrameworkBundle/composer.json

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

src/Symfony/Component/Routing/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+
* Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`.
8+
* Added support for a generic loader to `RoutingConfigurator`.
9+
410
5.0.0
511
-----
612

src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\Routing\Loader\Configurator;
1313

14-
use Symfony\Component\Routing\Loader\PhpFileLoader;
14+
use Symfony\Component\Config\Exception\LoaderLoadException;
15+
use Symfony\Component\Config\Loader\FileLoader;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
1517
use Symfony\Component\Routing\RouteCollection;
1618

1719
/**
@@ -25,7 +27,7 @@ class RoutingConfigurator
2527
private $path;
2628
private $file;
2729

28-
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file)
30+
public function __construct(RouteCollection $collection, LoaderInterface $loader, ?string $path, string $file)
2931
{
3032
$this->collection = $collection;
3133
$this->loader = $loader;
@@ -38,9 +40,7 @@ public function __construct(RouteCollection $collection, PhpFileLoader $loader,
3840
*/
3941
final public function import($resource, string $type = null, bool $ignoreErrors = false, $exclude = null): ImportConfigurator
4042
{
41-
$this->loader->setCurrentDir(\dirname($this->path));
42-
43-
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude) ?: [];
43+
$imported = $this->load($resource, $type, $ignoreErrors, $exclude) ?: [];
4444
if (!\is_array($imported)) {
4545
return new ImportConfigurator($this->collection, $imported);
4646
}
@@ -57,4 +57,34 @@ final public function collection(string $name = ''): CollectionConfigurator
5757
{
5858
return new CollectionConfigurator($this->collection, $name);
5959
}
60+
61+
/**
62+
* @param string|string[]|null $exclude
63+
*
64+
* @return RouteCollection|RouteCollection[]|null
65+
*/
66+
private function load($resource, ?string $type, bool $ignoreErrors, $exclude)
67+
{
68+
$loader = $this->loader;
69+
70+
if (!$loader->supports($resource, $type)) {
71+
if (null === $resolver = $loader->getResolver()) {
72+
throw new LoaderLoadException($resource, $this->file, null, null, $type);
73+
}
74+
75+
if (false === $loader = $resolver->resolve($resource, $type)) {
76+
throw new LoaderLoadException($resource, $this->file, null, null, $type);
77+
}
78+
}
79+
80+
if (!$loader instanceof FileLoader) {
81+
return $loader->load($resource, $type);
82+
}
83+
84+
if (null !== $this->path) {
85+
$this->loader->setCurrentDir(\dirname($this->path));
86+
}
87+
88+
return $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude);
89+
}
6090
}

src/Symfony/Component/Routing/RouteCollectionBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Helps add and import routes into a RouteCollection.
2020
*
2121
* @author Ryan Weaver <ryan@knpuniversity.com>
22+
*
23+
* @deprecated since Symfony 5.1, use RoutingConfigurator instead
2224
*/
2325
class RouteCollectionBuilder
2426
{

src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use Symfony\Component\Routing\RouteCollection;
2020
use Symfony\Component\Routing\RouteCollectionBuilder;
2121

22+
/**
23+
* @group legacy
24+
*/
2225
class RouteCollectionBuilderTest extends TestCase
2326
{
2427
public function testImport()

0 commit comments

Comments
 (0)
0