Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 16d5a6d

Browse files
committed
Deprecated RouteCollectionBuilder
1 parent 9aa7492 commit 16d5a6d

File tree

7 files changed

+184
-13
lines changed

7 files changed

+184
-13
lines changed

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 29 additions & 4 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
/**
@@ -30,7 +31,23 @@ trait MicroKernelTrait
3031
* $routes->import('config/routing.yml');
3132
* $routes->add('/admin', 'App\Controller\AdminController::dashboard', 'admin_dashboard');
3233
*/
33-
abstract protected function configureRoutes(RouteCollectionBuilder $routes);
34+
protected function configureRoutes(RouteCollectionBuilder $routes)
35+
{
36+
}
37+
38+
/**
39+
* Add or import routes into your application.
40+
*
41+
* $routes->import($this->getProjectDir().'/config/*.{yaml,php}');
42+
* $routes
43+
* ->add('admin_dashboard', '/admin')
44+
* ->controller('App\Controller\AdminController::dashboard')
45+
* ;
46+
*/
47+
protected function configureRouting(RoutingConfigurator $routes): void
48+
{
49+
@trigger_error(sprintf('Not overriding the "%s()" method is deprecated since Symfony 5.0 and will trigger a fatal error in 6.0.', __METHOD__), E_USER_DEPRECATED);
50+
}
3451

3552
/**
3653
* Configures the container.
@@ -89,9 +106,17 @@ public function registerContainerConfiguration(LoaderInterface $loader)
89106
*/
90107
public function loadRoutes(LoaderInterface $loader)
91108
{
92-
$routes = new RouteCollectionBuilder($loader);
93-
$this->configureRoutes($routes);
109+
$builder = new RouteCollectionBuilder($loader);
110+
$this->configureRoutes($builder);
111+
$collection = $builder->build();
112+
113+
if (0 !== \count($collection)) {
114+
@trigger_error(sprintf('Adding routes via the "%s:configureRoutes()" method is deprecated since Symfony 5.0 and will have no effect in 6.0. Use configureRouting() instead.', self::class), E_USER_DEPRECATED);
115+
}
116+
117+
$file = (new \ReflectionObject($this))->getFileName();
118+
$this->configureRouting(new RoutingConfigurator($collection, $loader, null, $file));
94119

95-
return $routes->build();
120+
return $collection;
96121
}
97122
}

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 +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.0 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.0 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 <[email protected]>
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/Component/Routing/Loader/Configurator/RoutingConfigurator.php

Lines changed: 30 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 = null, string $file = null)
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,29 @@ 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+
* @return RouteCollection|RouteCollection[]|null
64+
*/
65+
private function load($resource, ?string $type, bool $ignoreErrors, $exclude)
66+
{
67+
if (null === $resolver = $this->loader->getResolver()) {
68+
throw new LoaderLoadException($resource, $this->file, null, null, $type);
69+
}
70+
71+
if (false === $loader = $resolver->resolve($resource, $type)) {
72+
throw new LoaderLoadException($resource, $this->file, null, null, $type);
73+
}
74+
75+
if ($loader instanceof FileLoader) {
76+
if (null !== $this->path) {
77+
$this->loader->setCurrentDir(\dirname($this->path));
78+
}
79+
80+
return $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude);
81+
}
82+
83+
return $loader->load($resource, $type);
84+
}
6085
}

src/Symfony/Component/Routing/RouteCollectionBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Helps add and import routes into a RouteCollection.
2020
*
2121
* @author Ryan Weaver <[email protected]>
22+
* @deprecated since Symfony 5.0, to be removed in 6.0; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator instead
2223
*/
2324
class RouteCollectionBuilder
2425
{

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
class RouteCollectionBuilderTest extends TestCase
2323
{
24+
/**
25+
* @group legacy
26+
*/
2427
public function testImport()
2528
{
2629
$resolvedLoader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
@@ -66,6 +69,9 @@ public function testImport()
6669
$this->assertCount(1, $routeCollection->getResources());
6770
}
6871

72+
/**
73+
* @group legacy
74+
*/
6975
public function testImportAddResources()
7076
{
7177
$routeCollectionBuilder = new RouteCollectionBuilder(new YamlFileLoader(new FileLocator([__DIR__.'/Fixtures/'])));
@@ -75,13 +81,19 @@ public function testImportAddResources()
7581
$this->assertCount(1, $routeCollection->getResources());
7682
}
7783

84+
/**
85+
* @group legacy
86+
*/
7887
public function testImportWithoutLoaderThrowsException()
7988
{
8089
$this->expectException('BadMethodCallException');
8190
$collectionBuilder = new RouteCollectionBuilder();
8291
$collectionBuilder->import('routing.yml');
8392
}
8493

94+
/**
95+
* @group legacy
96+
*/
8597
public function testAdd()
8698
{
8799
$collectionBuilder = new RouteCollectionBuilder();
@@ -95,6 +107,9 @@ public function testAdd()
95107
$this->assertSame($addedRoute2, $finalCollection->get('blog_list'));
96108
}
97109

110+
/**
111+
* @group legacy
112+
*/
98113
public function testFlushOrdering()
99114
{
100115
$importedCollection = new RouteCollection();
@@ -144,6 +159,9 @@ public function testFlushOrdering()
144159
$this->assertEquals('fr', $defaults['_locale']);
145160
}
146161

162+
/**
163+
* @group legacy
164+
*/
147165
public function testFlushSetsRouteNames()
148166
{
149167
$collectionBuilder = new RouteCollectionBuilder();
@@ -166,6 +184,9 @@ public function testFlushSetsRouteNames()
166184
], $actualRouteNames);
167185
}
168186

187+
/**
188+
* @group legacy
189+
*/
169190
public function testFlushSetsDetailsOnChildrenRoutes()
170191
{
171192
$routes = new RouteCollectionBuilder();
@@ -230,6 +251,7 @@ public function testFlushSetsDetailsOnChildrenRoutes()
230251
}
231252

232253
/**
254+
* @group legacy
233255
* @dataProvider providePrefixTests
234256
*/
235257
public function testFlushPrefixesPaths($collectionPrefix, $routePath, $expectedPath)
@@ -246,6 +268,9 @@ public function testFlushPrefixesPaths($collectionPrefix, $routePath, $expectedP
246268
$this->assertEquals($expectedPath, $collection->get('test_route')->getPath());
247269
}
248270

271+
/**
272+
* @group legacy
273+
*/
249274
public function providePrefixTests()
250275
{
251276
$tests = [];
@@ -262,6 +287,9 @@ public function providePrefixTests()
262287
return $tests;
263288
}
264289

290+
/**
291+
* @group legacy
292+
*/
265293
public function testFlushSetsPrefixedWithMultipleLevels()
266294
{
267295
$loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
@@ -310,6 +338,9 @@ public function testFlushSetsPrefixedWithMultipleLevels()
310338
$this->assertEquals('/admin/imported/foo', $collection->get('imported_route')->getPath(), 'Normal RouteCollections are also prefixed properly');
311339
}
312340

341+
/**
342+
* @group legacy
343+
*/
313344
public function testAutomaticRouteNamesDoNotConflict()
314345
{
315346
$routes = new RouteCollectionBuilder();
@@ -334,6 +365,9 @@ public function testAutomaticRouteNamesDoNotConflict()
334365
$this->assertCount(3, $collection->all());
335366
}
336367

368+
/**
369+
* @group legacy
370+
*/
337371
public function testAddsThePrefixOnlyOnceWhenLoadingMultipleCollections()
338372
{
339373
$firstCollection = new RouteCollection();

0 commit comments

Comments
 (0)