Skip to content

Commit 43a79f5

Browse files
committed
feature #32598 [FrameworkBundle][Routing] Private service route loaders (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle][Routing] Private service route loaders | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony#30402 | License | MIT | Doc PR | symfony/symfony-docs#11337 Continuation of symfony/symfony#30926. ~Please review only the 2nd commit, I'm building this on top of symfony/symfony#32582 Commits ------- 64aa2c8529 [FrameworkBundle][Routing] Private service route loaders
2 parents 1289e64 + 29d09eb commit 43a79f5

File tree

9 files changed

+175
-3
lines changed

9 files changed

+175
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Added support for configuring chained cache pools
1313
* Deprecated booting the kernel before running `WebTestCase::createClient()`
1414
* Deprecated `routing.loader.service`, use `routing.loader.container` instead.
15+
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
1516

1617
4.3.0
1718
-----

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',

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;
@@ -447,6 +448,9 @@ public function load(array $configs, ContainerBuilder $container)
447448
if (!$config['disallow_search_engine_index'] ?? false) {
448449
$container->removeDefinition('disallow_search_engine_index_response_listener');
449450
}
451+
452+
$container->registerForAutoconfiguration(RouteLoaderInterface::class)
453+
->addTag('routing.route_loader');
450454
}
451455

452456
/**

Kernel/MicroKernelTrait.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,20 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6464
],
6565
]);
6666

67-
if ($this instanceof EventSubscriberInterface) {
67+
if (!$container->hasDefinition('kernel')) {
6868
$container->register('kernel', static::class)
6969
->setSynthetic(true)
7070
->setPublic(true)
71-
->addTag('kernel.event_subscriber')
7271
;
7372
}
7473

74+
$kernelDefinition = $container->getDefinition('kernel');
75+
$kernelDefinition->addTag('routing.route_loader');
76+
77+
if ($this instanceof EventSubscriberInterface) {
78+
$kernelDefinition->addTag('kernel.event_subscriber');
79+
}
80+
7581
$this->configureContainer($container, $loader);
7682

7783
$container->addObjectResource($this);

Resources/config/routing.xml

Lines changed: 6 additions & 1 deletion
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">
Lines changed: 51 additions & 0 deletions
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+
}

Routing/RouteLoaderInterface.php

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+
}

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\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)
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