Skip to content

Commit

Permalink
define a WarmableInterface and only warm the cache if it implements w…
Browse files Browse the repository at this point in the history
…armable to allow replacing the core router. this fixes #2422. combining routers will only really work when #2450 is merged too.
  • Loading branch information
dbu committed Oct 22, 2011
1 parent b95fe53 commit 46a69f1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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);
}
}

/**
Expand Down
@@ -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);
}
18 changes: 17 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Expand Up @@ -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 <fabien@symfony.com>
*/
class Router extends BaseRouter
class Router extends BaseRouter implements WarmableInterface
{
private $container;

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 46a69f1

Please sign in to comment.