Skip to content

Commit

Permalink
merged branch dbu/router-cache-warmup-interface (PR #2451)
Browse files Browse the repository at this point in the history
Commits
-------

96235a6 cs fix
46a69f1 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.

Discussion
----------

define a WarmableInterface and only warm the cache if it implements warmable

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.

Bug fix: yes
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes*
Fixes the following tickets: #2422

(*) tests:
success for
phpunit src/Symfony/Bundle/FrameworkBundle/Tests/
and
phpunit tests/Symfony/Tests/Component/Routing/

but when running all tests, i have to put gc_disable() into autoload to avoid segmentation fault and then get (after 3000 successful tests):

PHP Fatal error:  Call to undefined method Symfony\Tests\Component\HttpKernel\Debug\StopwatchTest::assertCount() in /home/david/liip/symfony-cmf/cmf-sandbox/vendor/symfony/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchTest.php on line 84

---------------------------------------------------------------------------

by stof at 2011/10/22 08:46:31 -0700

@dbu assertCount is new in PHP 3.6. It seems like the test of the new 2.1 feature has been written using 3.6-RC.

---------------------------------------------------------------------------

by dbu at 2011/10/22 09:40:07 -0700

@stof: ah, thanks for the hint. i hope you mean php 3.4 and not 3.6? but i assume symfony 2.1 should be able to run on 3.3, right?

anyways, the tests run through if i disable the StopWatch, so i guess we can consider the tests succeeding.

---------------------------------------------------------------------------

by stof at 2011/10/22 09:41:28 -0700

this is a method of PHPUnit TestCase class. I was talking about the PHPUnit version
  • Loading branch information
fabpot committed Oct 23, 2011
2 parents 3134ef1 + 96235a6 commit cb9ac13
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
*/
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 cb9ac13

Please sign in to comment.