define a WarmableInterface and only warm the cache if it implements warmable to allow replacing the core router. this fixes #2422. combining routers will only really work when #2450 is merged too.

This commit is contained in:
dbu 2011-10-22 16:11:56 +02:00
parent b95fe53bf4
commit 46a69f1ca0
3 changed files with 49 additions and 11 deletions

View File

@ -12,7 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouterInterface;
/**
* Generates the router matcher and generator classes.
@ -28,7 +28,7 @@ class RouterCacheWarmer implements CacheWarmerInterface
*
* @param Router $router A Router instance
*/
public function __construct(Router $router)
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
@ -40,14 +40,9 @@ class RouterCacheWarmer implements CacheWarmerInterface
*/
public function warmUp($cacheDir)
{
$currentDir = $this->router->getOption('cache_dir');
// force cache generation
$this->router->setOption('cache_dir', $cacheDir);
$this->router->getMatcher();
$this->router->getGenerator();
$this->router->setOption('cache_dir', $currentDir);
if ($this->router instanceof WarmableInterface) {
$this->router->warmUp($cacheDir);
}
}
/**

View File

@ -0,0 +1,27 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
/**
* Interface for services that support warming their cache.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface WarmableInterface
{
/**
* Warms up the cache.
*
* @param string $cacheDir The cache directory
*/
public function warmUp($cacheDir);
}

View File

@ -15,13 +15,14 @@ use Symfony\Component\Routing\Router as BaseRouter;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\WarmableInterface;
/**
* This Router only creates the Loader only when the cache is empty.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Router extends BaseRouter
class Router extends BaseRouter implements WarmableInterface
{
private $container;
@ -57,6 +58,21 @@ class Router extends BaseRouter
return $this->collection;
}
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
$currentDir = $this->getOption('cache_dir');
// force cache generation
$this->setOption('cache_dir', $cacheDir);
$this->getMatcher();
$this->getGenerator();
$this->setOption('cache_dir', $currentDir);
}
/**
* Replaces placeholders with service container parameter values in route defaults and requirements.
*