Skip to content

Commit

Permalink
refactored ConfigCache and optimized container:debug task
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 14, 2011
1 parent 58bf229 commit f4e4a2a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 43 deletions.
Expand Up @@ -14,40 +14,37 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\ConfigCache;

/**
* Dumps the ContainerBuilder to a cache file so that it can be used by
* debugging tools such as the container:debug console command.
*
* @author Ryan Weaver <ryan@thatsquality.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
class ContainerBuilderDebugDumpPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$file = self::getBuilderCacheFilename($container);
$cache = new ConfigCache(self::getBuilderCacheFilename($container), false);

if (false !== @file_put_contents($file, serialize($container))) {
chmod($file, 0666);
} else {
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $file));
}
$cache->write(serialize($container));
}

/**
* Calculates the cache filename to be used to cache the ContainerBuilder
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
*
* @return string
*/
public static function getBuilderCacheFilename(ContainerInterface $container)
static public function getBuilderCacheFilename(ContainerInterface $container)
{
$cacheDir = $container->getParameter('kernel.cache_dir');
$name = $container->getParameter('kernel.name');
$env = ucfirst($container->getParameter('kernel.environment'));
$debug = ($container->getParameter('kernel.debug')) ? 'Debug' : '';
$debug = $container->getParameter('kernel.debug') ? 'Debug' : '';

return $cacheDir.'/'.$name.$env.$debug.'ProjectContainerBuilder.cache';
return $container->getParameter('kernel.cache_dir').'/'.$name.$env.$debug.'ProjectContainerBuilder.cache';
}
}
}
Expand Up @@ -89,7 +89,7 @@ protected function loadCatalogue($locale)
return parent::loadCatalogue($locale);
}

$cache = new ConfigCache($this->options['cache_dir'], 'catalogue.'.$locale, $this->options['debug']);
$cache = new ConfigCache($this->options['cache_dir'].'/catalogue.'.$locale.'.php', $this->options['debug']);
if (!$cache->isFresh()) {
$this->initialize();

Expand Down
32 changes: 11 additions & 21 deletions src/Symfony/Component/Config/ConfigCache.php
Expand Up @@ -22,19 +22,16 @@
class ConfigCache
{
protected $debug;
protected $cacheDir;
protected $file;

/**
* Constructor.
*
* @param string $cacheDir The cache directory
* @param string $file The cache file name (without the .php extension)
* @param string $file The absolute cache path
* @param Boolean $debug Whether debugging is enabled or not
*/
public function __construct($cacheDir, $file, $debug)
public function __construct($file, $debug)
{
$this->cacheDir = $cacheDir;
$this->file = $file;
$this->debug = (Boolean) $debug;
}
Expand All @@ -46,7 +43,7 @@ public function __construct($cacheDir, $file, $debug)
*/
public function __toString()
{
return $this->getCacheFile();
return $this->file;
}

/**
Expand All @@ -58,21 +55,20 @@ public function __toString()
*/
public function isFresh()
{
$file = $this->getCacheFile();
if (!file_exists($file)) {
if (!file_exists($this->file)) {
return false;
}

if (!$this->debug) {
return true;
}

$metadata = $this->getCacheFile('meta');
$metadata = $this->file.'.meta';
if (!file_exists($metadata)) {
return false;
}

$time = filemtime($file);
$time = filemtime($this->file);
$meta = unserialize(file_get_contents($metadata));
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
Expand All @@ -93,8 +89,7 @@ public function isFresh()
*/
public function write($content, array $metadata = null)
{
$file = $this->getCacheFile();
$dir = dirname($file);
$dir = dirname($this->file);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
Expand All @@ -103,24 +98,19 @@ public function write($content, array $metadata = null)
throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
}

$tmpFile = tempnam(dirname($file), basename($file));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
chmod($file, 0666);
$tmpFile = tempnam(dirname($this->file), basename($this->file));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
chmod($this->file, 0666);
} else {
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
}

if (null !== $metadata && true === $this->debug) {
$file = $this->getCacheFile('meta');
$file = $this->file.'.meta';
$tmpFile = tempnam(dirname($file), basename($file));
if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
chmod($file, 0666);
}
}
}

protected function getCacheFile($extension = 'php')
{
return $this->cacheDir.'/'.$this->file.'.'.$extension;
}
}
15 changes: 8 additions & 7 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -400,7 +400,7 @@ protected function initializeBundles()
protected function initializeContainer()
{
$class = $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
$cache = new ConfigCache($this->getCacheDir(), $class, $this->debug);
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
$fresh = false;
if (!$cache->isFresh()) {
$container = $this->buildContainer();
Expand Down Expand Up @@ -471,13 +471,7 @@ protected function buildContainer()
if (null !== $cont = $this->registerContainerConfiguration($this->getContainerLoader($container))) {
$container->merge($cont);
}
$container->compile();

return $container;
}

protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class)
{
foreach (array('cache', 'logs') as $name) {
$dir = $container->getParameter(sprintf('kernel.%s_dir', $name));
if (!is_dir($dir)) {
Expand All @@ -489,6 +483,13 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
}
}

$container->compile();

return $container;
}

protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container, $class)
{
// cache the container
$dumper = new PhpDumper($container);
$content = $dumper->dump(array('class' => $class));
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Routing/Router.php
Expand Up @@ -154,7 +154,7 @@ public function getMatcher()
}

$class = $this->options['matcher_cache_class'];
$cache = new ConfigCache($this->options['cache_dir'], $class, $this->options['debug']);
$cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
if (!$cache->isFresh($class)) {
$dumper = new $this->options['matcher_dumper_class']($this->getRouteCollection());

Expand Down Expand Up @@ -187,7 +187,7 @@ public function getGenerator()
}

$class = $this->options['generator_cache_class'];
$cache = new ConfigCache($this->options['cache_dir'], $class, $this->options['debug']);
$cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
if (!$cache->isFresh($class)) {
$dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());

Expand Down

0 comments on commit f4e4a2a

Please sign in to comment.