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

Skip to content

Commit 205497b

Browse files
committed
[FrameworkBundle][Routing] Add a new tag to be able to use a private service as a service route loader
1 parent b82b09e commit 205497b

File tree

11 files changed

+183
-4
lines changed

11 files changed

+183
-4
lines changed

UPGRADE-4.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Routing
123123
options have been deprecated.
124124
* Implementing `Serializable` for `Route` and `CompiledRoute` is deprecated; if you serialize them, please
125125
ensure your unserialization logic can recover from a failure related to an updated serialization format
126+
* Not tagging the router loader services with `routing.router_loader` has been deprecated.
126127

127128
Security
128129
--------

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ Routing
284284
options have been removed.
285285
* `Route` and `CompiledRoute` don't implement `Serializable` anymore; if you serialize them, please
286286
ensure your unserialization logic can recover from a failure related to an updated serialization format
287+
* The router loader services must be tagged with `routing.router_loader`.
287288

288289
Security
289290
--------

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.router_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
@@ -92,6 +92,7 @@
9292
use Symfony\Component\Routing\Generator\UrlGenerator;
9393
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
9494
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
95+
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoaderInterface;
9596
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
9697
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
9798
use Symfony\Component\Security\Core\Security;
@@ -428,6 +429,9 @@ public function load(array $configs, ContainerBuilder $container)
428429
if (!$config['disallow_search_engine_index'] ?? false) {
429430
$container->removeDefinition('disallow_search_engine_index_response_listener');
430431
}
432+
433+
$container->registerForAutoconfiguration(ServiceRouterLoaderInterface::class)
434+
->addTag('routing.router_loader');
431435
}
432436

433437
/**

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,23 @@ 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+
81+
$kernelDefinition->addTag('routing.router_loader', [
82+
'service_id' => 'kernel',
83+
]);
84+
85+
if ($this instanceof EventSubscriberInterface) {
86+
$kernelDefinition->addTag('kernel.event_subscriber');
87+
}
88+
8089
$this->configureContainer($container, $loader);
8190

8291
$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
@@ -40,9 +40,14 @@
4040
<argument type="service" id="file_locator" />
4141
</service>
4242

43+
<service id="routing.loader.service.container" class="Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoaderContainer">
44+
<argument type="service" id="service_container" />
45+
<argument type="tagged_locator" tag="routing.router_loader" index-by="service_id" />
46+
</service>
47+
4348
<service id="routing.loader.service" class="Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoader">
4449
<tag name="routing.loader" />
45-
<argument type="service" id="service_container" />
50+
<argument type="service" id="routing.loader.service.container" />
4651
</service>
4752

4853
<service id="routing.loader" class="Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader" public="true">

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

Lines changed: 20 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,21 @@ 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+
54+
$container = new ContainerBuilder();
55+
$container->registerExtension($frameworkExtension);
56+
57+
$kernel = new ConcreteMicroKernel('test', false);
58+
$kernel->registerContainerConfiguration(new ClosureLoader($container));
59+
60+
$this->assertTrue($container->getDefinition('kernel')->hasTag('routing.router_loader'));
61+
}
4262
}

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CHANGELOG
1111
* deprecated implementing `Serializable` for `Route` and `CompiledRoute`; if you serialize them, please
1212
ensure your unserialization logic can recover from a failure related to an updated serialization format
1313
* exposed `utf8` Route option, defaults "locale" and "format" in configuration loaders and configurators
14-
* added support for invokable route loader services
14+
* added support for invokable router loader services
1515

1616
4.2.0
1717
-----
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\Component\Routing\Loader\DependencyInjection;
13+
14+
use Psr\Container\ContainerInterface;
15+
16+
/**
17+
* @internal
18+
*
19+
* @deprecated since Symfony 4.3, to be removed in 5.0
20+
*/
21+
class ServiceRouterLoaderContainer 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 routing loader "%s" without tagging it with the "routing.router_loader" tag is deprecated since Symfony 4.3 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\Component\Routing\Loader\DependencyInjection;
13+
14+
/**
15+
* Marker interface for router loader services.
16+
*/
17+
interface ServiceRouterLoaderInterface
18+
{
19+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\Component\Routing\Tests\Loader;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Container\ContainerInterface;
16+
use Symfony\Component\DependencyInjection\Container;
17+
use Symfony\Component\Routing\Loader\DependencyInjection\ServiceRouterLoaderContainer;
18+
19+
class ServiceRouterLoaderContainerTest extends TestCase
20+
{
21+
/**
22+
* @var ContainerInterface
23+
*/
24+
private $container;
25+
26+
/**
27+
* @var ContainerInterface
28+
*/
29+
private $serviceLocator;
30+
31+
/**
32+
* @var ServiceRouterLoaderContainer
33+
*/
34+
private $serviceRouterLoaderContainer;
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected function setUp()
40+
{
41+
$this->container = new Container();
42+
$this->container->set('foo', new \stdClass());
43+
44+
$this->serviceLocator = new Container();
45+
$this->serviceLocator->set('bar', new \stdClass());
46+
47+
$this->serviceRouterLoaderContainer = new ServiceRouterLoaderContainer($this->container, $this->serviceLocator);
48+
}
49+
50+
/**
51+
* @group legacy
52+
* @expectedDeprecation Registering the routing loader "foo" without tagging it with the "routing.router_loader" tag is deprecated since Symfony 4.3 and will be required in Symfony 5.0.
53+
*/
54+
public function testGet()
55+
{
56+
$this->assertSame($this->container->get('foo'), $this->serviceRouterLoaderContainer->get('foo'));
57+
$this->assertSame($this->serviceLocator->get('bar'), $this->serviceRouterLoaderContainer->get('bar'));
58+
}
59+
60+
public function testHas()
61+
{
62+
$this->assertTrue($this->serviceRouterLoaderContainer->has('foo'));
63+
$this->assertTrue($this->serviceRouterLoaderContainer->has('bar'));
64+
$this->assertFalse($this->serviceRouterLoaderContainer->has('ccc'));
65+
}
66+
}

0 commit comments

Comments
 (0)