Skip to content

Commit 3a30564

Browse files
committed
[FrameworkBundle][Routing] Add a new tag to be able to use a private service as a service route loader
1 parent b817c6e commit 3a30564

File tree

10 files changed

+182
-3
lines changed

10 files changed

+182
-3
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 route loader services with `routing.route_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
@@ -288,6 +288,7 @@ Routing
288288
options have been removed.
289289
* `Route` and `CompiledRoute` don't implement `Serializable` anymore; if you serialize them, please
290290
ensure your unserialization logic can recover from a failure related to an updated serialization format
291+
* The route loader services must be tagged with `routing.route_loader`.
291292

292293
Security
293294
--------

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;
@@ -423,6 +424,9 @@ public function load(array $configs, ContainerBuilder $container)
423424
if (!$config['disallow_search_engine_index'] ?? false) {
424425
$container->removeDefinition('disallow_search_engine_index_response_listener');
425426
}
427+
428+
$container->registerForAutoconfiguration(ServiceRouterLoaderInterface::class)
429+
->addTag('routing.route_loader');
426430
}
427431

428432
/**

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
5050
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
5151
use Symfony\Component\Routing\DependencyInjection\RoutingResolverPass;
52+
use Symfony\Component\Routing\DependencyInjection\ServiceRouterLoaderPass;
5253
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
5354
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
5455
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;

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.route_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.route_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.route_loader'));
61+
}
4262
}
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\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.route_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 <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\Component\Routing\Loader\DependencyInjection;
13+
14+
/**
15+
* Marker interface for route 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 <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\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.route_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)
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