From 46a69f1ca004f712d39478d9c23076e03b781e88 Mon Sep 17 00:00:00 2001 From: dbu Date: Sat, 22 Oct 2011 16:11:56 +0200 Subject: [PATCH] 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. --- .../CacheWarmer/RouterCacheWarmer.php | 15 ++++------- .../CacheWarmer/WarmableInterface.php | 27 +++++++++++++++++++ .../Bundle/FrameworkBundle/Routing/Router.php | 18 ++++++++++++- 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/CacheWarmer/WarmableInterface.php diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php index a400e3204013..4376f1ead341 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php @@ -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 @@ public function __construct(Router $router) */ 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); + } } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/WarmableInterface.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/WarmableInterface.php new file mode 100644 index 000000000000..d041fcc0e903 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/WarmableInterface.php @@ -0,0 +1,27 @@ + + * + * 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 + */ +interface WarmableInterface +{ + /** + * Warms up the cache. + * + * @param string $cacheDir The cache directory + */ + public function warmUp($cacheDir); +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index b5d66362f45e..1fd3aef0fc54 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -15,13 +15,14 @@ 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 */ -class Router extends BaseRouter +class Router extends BaseRouter implements WarmableInterface { private $container; @@ -57,6 +58,21 @@ public function getRouteCollection() 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. *