feature #23570 [FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberInterface (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberInterface

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Blocks #23569

Commits
-------

c0af0031ec [FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberInterface
This commit is contained in:
Fabien Potencier 2017-07-18 15:06:41 +02:00
commit 5a521bf5ae
3 changed files with 28 additions and 7 deletions

View File

@ -91,7 +91,7 @@ class RoutingExtension extends AbstractExtension
*
* @return array An array with the contexts the URL is safe
*
* To be made @final in 3.4, and the type-hint be changed to "\Twig\Node\Node" in 4.0.
* @final since version 3.4, type-hint to be changed to "\Twig\Node\Node" in 4.0
*/
public function isUrlGenerationSafe(\Twig_Node $argsNode)
{

View File

@ -11,6 +11,8 @@
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\RouterInterface;
@ -20,20 +22,28 @@ use Symfony\Component\Routing\RouterInterface;
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @final since version 3.4, to be given a container instead in 4.0
* @final since version 3.4
*/
class RouterCacheWarmer implements CacheWarmerInterface
class RouterCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
{
protected $router;
/**
* Constructor.
*
* @param RouterInterface $router A Router instance
* @param ContainerInterface $container
*/
public function __construct(RouterInterface $router)
public function __construct($container)
{
$this->router = $router;
// As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
if ($container instanceof ContainerInterface) {
$this->router = $container->get('router'); // For BC, the $router property must be populated in the constructor
} elseif ($container instanceof RouterInterface) {
$this->router = $container;
@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);
} else {
throw new \InvalidArgumentException(sprintf('%s only accepts instance of Psr\Container\ContainerInterface as first argument.', __CLASS__));
}
}
/**
@ -57,4 +67,14 @@ class RouterCacheWarmer implements CacheWarmerInterface
{
return true;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return array(
'router' => RouterInterface::class,
);
}
}

View File

@ -97,8 +97,9 @@
<service id="Symfony\Component\Routing\RequestContext" alias="router.request_context" />
<service id="router.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer">
<tag name="container.service_subscriber" id="router" />
<tag name="kernel.cache_warmer" />
<argument type="service" id="router" />
<argument type="service" id="Psr\Container\ContainerInterface" />
</service>
<service id="router_listener" class="Symfony\Component\HttpKernel\EventListener\RouterListener" public="true">