Skip to content

Commit fbf1e07

Browse files
committed
[FrameworkBundle][Routing] Private service route loaders
1 parent 1548101 commit fbf1e07

File tree

11 files changed

+179
-4
lines changed

11 files changed

+179
-4
lines changed

UPGRADE-4.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ FrameworkBundle
8484
* The `ControllerResolver` and `DelegatingLoader` classes have been marked as `final`.
8585
* The `controller_name_converter` and `resolve_controller_name_subscriber` services have been deprecated.
8686
* Deprecated `routing.loader.service`, use `routing.loader.container` instead.
87+
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
8788

8889
HttpClient
8990
----------

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 8 additions & 2 deletions
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
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" id="routing.route_loader_container" />
5151
</service>
5252

5353
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">
@@ -121,5 +121,10 @@
121121
<argument type="service" id="twig" on-invalid="ignore" />
122122
<argument type="service" id="templating" on-invalid="ignore" />
123123
</service>
124+
125+
<service id="routing.route_loader_container" class="Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderContainer">
126+
<argument type="service" id="service_container" />
127+
<argument type="tagged_locator" tag="routing.route_loader" />
128+
</service>
124129
</services>
125130
</container>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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
18+
*
19+
* @deprecated since Symfony 4.4
20+
*/
21+
class RouteLoaderContainer implements ContainerInterface
22+
{
23+
private $container;
24+
private $serviceLocator;
25+
26+
public function __construct(ContainerInterface $container, ContainerInterface $serviceLocator)
27+
{
28+
$this->container = $container;
29+
$this->serviceLocator = $serviceLocator;
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function get($id)
36+
{
37+
if ($this->serviceLocator->has($id)) {
38+
return $this->serviceLocator->get($id);
39+
}
40+
41+
@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);
42+
43+
return $this->container->get($id);
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function has($id)
50+
{
51+
return $this->serviceLocator->has($id) || $this->container->has($id);
52+
}
53+
}
Lines changed: 19 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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
}
Lines changed: 68 additions & 0 deletions
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\RouteLoaderContainer;
17+
use Symfony\Component\DependencyInjection\Container;
18+
19+
/**
20+
* @group legacy
21+
*/
22+
class RouteLoaderContainerTest extends TestCase
23+
{
24+
/**
25+
* @var ContainerInterface
26+
*/
27+
private $container;
28+
29+
/**
30+
* @var ContainerInterface
31+
*/
32+
private $serviceLocator;
33+
34+
/**
35+
* @var RouteLoaderContainer
36+
*/
37+
private $routeLoaderContainer;
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->routeLoaderContainer = new RouteLoaderContainer($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->routeLoaderContainer->get('foo'));
59+
$this->assertSame($this->serviceLocator->get('bar'), $this->routeLoaderContainer->get('bar'));
60+
}
61+
62+
public function testHas()
63+
{
64+
$this->assertTrue($this->routeLoaderContainer->has('foo'));
65+
$this->assertTrue($this->routeLoaderContainer->has('bar'));
66+
$this->assertFalse($this->routeLoaderContainer->has('ccc'));
67+
}
68+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy