Skip to content

Commit a47658f

Browse files
[FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberInterface
1 parent c6ab0c4 commit a47658f

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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 <[email protected]>
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
}

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)