Skip to content

[FrameworkBundle] Make RouterCacheWarmer implement ServiceSubscriberInterface #23570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function getUrl($name, $parameters = array(), $schemeRelative = false)
*
* @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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,20 +22,28 @@
*
* @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__));
}
}

/**
Expand All @@ -57,4 +67,14 @@ public function isOptional()
{
return true;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedServices()
{
return array(
'router' => RouterInterface::class,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down