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

Skip to content

Commit 55fe67c

Browse files
committed
[FrameworkBundle][Routing] Private service route loaders
1 parent 3d68427 commit 55fe67c

File tree

11 files changed

+183
-4
lines changed

11 files changed

+183
-4
lines changed

UPGRADE-4.4.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ PropertyAccess
118118

119119
* Deprecated passing `null` as 2nd argument of `PropertyAccessor::createCache()` method (`$defaultLifetime`), pass `0` instead.
120120

121+
Routing
122+
-------
123+
124+
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
125+
121126
Security
122127
--------
123128

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Routing
360360
* `Serializable` implementing methods for `Route` and `CompiledRoute` are final.
361361
Instead of overwriting them, use `__serialize` and `__unserialize` as extension points which are forward compatible
362362
with the new serialization methods in PHP 7.4.
363+
* Service route loaders must be tagged with `routing.router_loader`.
363364

364365
Security
365366
--------

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
@@ -42,7 +42,7 @@
4242

4343
<service id="routing.loader.service" class="Symfony\Component\Routing\Loader\ContainerLoader">
4444
<tag name="routing.loader" />
45-
<argument type="service" id="service_container" />
45+
<argument type="service" id="routing.route_loader_container" />
4646
</service>
4747

4848
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">
@@ -116,5 +116,10 @@
116116
<argument type="service" id="twig" on-invalid="ignore" />
117117
<argument type="service" id="templating" on-invalid="ignore" />
118118
</service>
119+
120+
<service id="routing.route_loader_container" class="Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderContainer">
121+
<argument type="service" id="service_container" />
122+
<argument type="tagged_locator" tag="routing.route_loader" />
123+
</service>
119124
</services>
120125
</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 <[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\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 <[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\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 <[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\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+
}

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CHANGELOG
1818
Instead of overwriting them, use `__serialize` and `__unserialize` as extension points which are forward compatible
1919
with the new serialization methods in PHP 7.4.
2020
* exposed `utf8` Route option, defaults "locale" and "format" in configuration loaders and configurators
21-
* added support for invokable route loader services
21+
* added support for invokable service route loaders
2222

2323
4.2.0
2424
-----

0 commit comments

Comments
 (0)