Skip to content

Commit ab43370

Browse files
committed
[FrameworkBundle][Routing] Private service route loaders
1 parent 40fe161 commit ab43370

File tree

10 files changed

+176
-3
lines changed

10 files changed

+176
-3
lines changed

UPGRADE-4.4.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ FrameworkBundle
8686
* The `ControllerResolver` and `DelegatingLoader` classes have been marked as `final`.
8787
* The `controller_name_converter` and `resolve_controller_name_subscriber` services have been deprecated.
8888
* Deprecated `routing.loader.service`, use `routing.loader.container` instead.
89+
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
8990

9091
HttpClient
9192
----------

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Added support for configuring chained cache pools
1212
* Deprecated booting the kernel before running `WebTestCase::createClient()`
1313
* Deprecated `routing.loader.service`, use `routing.loader.container` instead.
14+
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
1415

1516
4.3.0
1617
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class UnusedTagsPass implements CompilerPassInterface
4949
'proxy',
5050
'routing.expression_language_provider',
5151
'routing.loader',
52+
'routing.route_loader',
5253
'security.expression_language_provider',
5354
'security.remember_me_aware',
5455
'security.voter',

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2525
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
2626
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
27+
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
2728
use Symfony\Bundle\FullStack;
2829
use Symfony\Component\Asset\PackageInterface;
2930
use Symfony\Component\BrowserKit\AbstractBrowser;
@@ -446,6 +447,9 @@ public function load(array $configs, ContainerBuilder $container)
446447
if (!$config['disallow_search_engine_index'] ?? false) {
447448
$container->removeDefinition('disallow_search_engine_index_response_listener');
448449
}
450+
451+
$container->registerForAutoconfiguration(RouteLoaderInterface::class)
452+
->addTag('routing.route_loader');
449453
}
450454

451455
/**

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,20 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6969
],
7070
]);
7171

72-
if ($this instanceof EventSubscriberInterface) {
72+
if (!$container->hasDefinition('kernel')) {
7373
$container->register('kernel', static::class)
7474
->setSynthetic(true)
7575
->setPublic(true)
76-
->addTag('kernel.event_subscriber')
7776
;
7877
}
7978

79+
$kernelDefinition = $container->getDefinition('kernel');
80+
$kernelDefinition->addTag('routing.route_loader');
81+
82+
if ($this instanceof EventSubscriberInterface) {
83+
$kernelDefinition->addTag('kernel.event_subscriber');
84+
}
85+
8086
$this->configureContainer($container, $loader);
8187

8288
$container->addObjectResource($this);

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747

4848
<service id="routing.loader.container" class="Symfony\Component\Routing\Loader\ContainerLoader">
4949
<tag name="routing.loader" />
50-
<argument type="service" id="service_container" />
50+
<argument type="service">
51+
<service class="Symfony\Bundle\FrameworkBundle\Routing\LegacyRouteLoaderContainer">
52+
<argument type="service" id="service_container" />
53+
<argument type="tagged_locator" tag="routing.route_loader" />
54+
</service>
55+
</argument>
5156
</service>
5257

5358
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Routing;
13+
14+
use Psr\Container\ContainerInterface;
15+
16+
/**
17+
* @internal to be removed in Symfony 5.0
18+
*/
19+
class LegacyRouteLoaderContainer implements ContainerInterface
20+
{
21+
private $container;
22+
private $serviceLocator;
23+
24+
public function __construct(ContainerInterface $container, ContainerInterface $serviceLocator)
25+
{
26+
$this->container = $container;
27+
$this->serviceLocator = $serviceLocator;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function get($id)
34+
{
35+
if ($this->serviceLocator->has($id)) {
36+
return $this->serviceLocator->get($id);
37+
}
38+
39+
@trigger_error(sprintf('Registering the service route loader "%s" without tagging it with the "routing.route_loader" tag is deprecated since Symfony 4.4 and will be required in Symfony 5.0.', $id), E_USER_DEPRECATED);
40+
41+
return $this->container->get($id);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function has($id)
48+
{
49+
return $this->serviceLocator->has($id) || $this->container->has($id);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Routing;
13+
14+
/**
15+
* Marker interface for service route loaders.
16+
*/
17+
interface RouteLoaderInterface
18+
{
19+
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
17+
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
1518
use Symfony\Component\HttpFoundation\Request;
1619

1720
class MicroKernelTraitTest extends TestCase
@@ -39,4 +42,18 @@ public function testAsEventSubscriber()
3942

4043
$this->assertSame('It\'s dangerous to go alone. Take this ⚔', $response->getContent());
4144
}
45+
46+
public function testRoutingRouteLoaderTagIsAdded()
47+
{
48+
$frameworkExtension = $this->createMock(ExtensionInterface::class);
49+
$frameworkExtension
50+
->expects($this->atLeastOnce())
51+
->method('getAlias')
52+
->willReturn('framework');
53+
$container = new ContainerBuilder();
54+
$container->registerExtension($frameworkExtension);
55+
$kernel = new ConcreteMicroKernel('test', false);
56+
$kernel->registerContainerConfiguration(new ClosureLoader($container));
57+
$this->assertTrue($container->getDefinition('kernel')->hasTag('routing.route_loader'));
58+
}
4259
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Routing;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Container\ContainerInterface;
16+
use Symfony\Bundle\FrameworkBundle\Routing\LegacyRouteLoaderContainer;
17+
use Symfony\Component\DependencyInjection\Container;
18+
19+
/**
20+
* @group legacy
21+
*/
22+
class LegacyRouteLoaderContainerTest extends TestCase
23+
{
24+
/**
25+
* @var ContainerInterface
26+
*/
27+
private $container;
28+
29+
/**
30+
* @var ContainerInterface
31+
*/
32+
private $serviceLocator;
33+
34+
/**
35+
* @var LegacyRouteLoaderContainer
36+
*/
37+
private $legacyRouteLoaderContainer;
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function setUp()
43+
{
44+
$this->container = new Container();
45+
$this->container->set('foo', new \stdClass());
46+
47+
$this->serviceLocator = new Container();
48+
$this->serviceLocator->set('bar', new \stdClass());
49+
50+
$this->legacyRouteLoaderContainer = new LegacyRouteLoaderContainer($this->container, $this->serviceLocator);
51+
}
52+
53+
/**
54+
* @expectedDeprecation Registering the service route loader "foo" without tagging it with the "routing.route_loader" tag is deprecated since Symfony 4.4 and will be required in Symfony 5.0.
55+
*/
56+
public function testGet()
57+
{
58+
$this->assertSame($this->container->get('foo'), $this->legacyRouteLoaderContainer->get('foo'));
59+
$this->assertSame($this->serviceLocator->get('bar'), $this->legacyRouteLoaderContainer->get('bar'));
60+
}
61+
62+
public function testHas()
63+
{
64+
$this->assertTrue($this->legacyRouteLoaderContainer->has('foo'));
65+
$this->assertTrue($this->legacyRouteLoaderContainer->has('bar'));
66+
$this->assertFalse($this->legacyRouteLoaderContainer->has('ccc'));
67+
}
68+
}

0 commit comments

Comments
 (0)