8000 [FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberI… · symfony/symfony@c0af003 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0af003

Browse files
[FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberInterface
1 parent ab661bd commit c0af003

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/Symfony/Bridge/Twig/Extension/RoutingExtension.php

Lines changed: 1 addition & 1 deletion
10000
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getUrl($name, $parameters = array(), $schemeRelative = false)
9191
*
9292
* @return array An array with the contexts the URL is safe
9393
*
94-
* To be made @final in 3.4, and the type-hint be changed to "\Twig\Node\Node" in 4.0.
94+
* @final since version 3.4, type-hint to be changed to "\Twig\Node\Node" in 4.0
9595
*/
9696
public function isUrlGenerationSafe(\Twig_Node $argsNode)
9797
{

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1416
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1517
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1618
use Symfony\Component\Routing\RouterInterface;
@@ -20,20 +22,28 @@
2022
*
2123
* @author Fabien Potencier <fabien@symfony.com>
2224
*
23-
* @final since version 3.4, to be given a container instead in 4.0
25+
* @final since version 3.4
2426
*/
25-
class RouterCacheWarmer implements CacheWarmerInterface
27+
class RouterCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
2628
{
2729
protected $router;
2830

2931
/**
3032
* Constructor.
3133
*
32-
* @param RouterInterface $router A Router instance
34+
* @param ContainerInterface $container
3335
*/
34-
public function __construct(RouterInterface $router)
36+
public function __construct($container)
3537
{
36-
$this->router = $router;
38+
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
39+
if ($container instanceof ContainerInterface) {
40+
$this->router = $container->get('router'); // For BC, the $router property must be populated in the constructor
41+
} elseif ($container instanceof RouterInterface) {
42+
$this->router = $container;
43+
@trigger_error(sprintf('Using a "%s" as first argument of %s is deprecated since version 3.4 and will be unsupported in version 4.0. Use a %s instead.', RouterInterface::class, __CLASS__, ContainerInterface::class), E_USER_DEPRECATED);
44+
} else {
45+
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Psr\Container\ContainerInterface as first argument.', __CLASS__));
46+
}
3747
}
3848

3949
/**
@@ -57,4 +67,14 @@ public function isOptional()
5767
{
5868
return true;
5969
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public static function getSubscribedServices()
75+
{
76+
return array(
77+
'router' => RouterInterface::class,
78+
);
79+
}
6080
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797
<service id="Symfony\Component\Routing\RequestContext" alias="router.request_context" />
9898

9999
<service id="router.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer">
100+
<tag name="container.service_subscriber" id="router" />
100101
<tag name="kernel.cache_warmer" />
101-
<argument type="service" id="router" />
102+
<argument type="service" id="Psr\Container\ContainerInterface" />
102103
</service>
103104

104105
<service id="router_listener" class="Symfony\Component\HttpKernel\EventListener\RouterListener" public="true">

0 commit comments

Comments
 (0)
0