[FrameworkBundle] Simplify the over-complicated template cache warmer

This commit is contained in:
Victor Berchet 2011-02-24 11:07:22 +01:00
parent 79bc233344
commit 788f63d460
2 changed files with 25 additions and 27 deletions

View File

@ -14,8 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Bundle\FrameworkBundle\Templating\Template;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
/**
@ -25,7 +23,8 @@ use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
*/
class TemplatePathsCacheWarmer extends CacheWarmer
{
protected $locator;
const TEMPLATES_PATH_IN_BUNDLE = '/Resources/views';
protected $kernel;
protected $rootDir;
protected $parser;
@ -34,14 +33,12 @@ class TemplatePathsCacheWarmer extends CacheWarmer
* Constructor.
*
* @param KernelInterface $kernel A KernelInterface instance
* @param FileLocatorInterface $locator A FileLocatorInterface instance
* @param TemplateNameParser $parser A TemplateNameParser instance
* @param string $rootDir The directory where global templates can be stored
*/
public function __construct(KernelInterface $kernel, FileLocatorInterface $locator, TemplateNameParser $parser, $rootDir)
public function __construct(KernelInterface $kernel, TemplateNameParser $parser, $rootDir)
{
$this->kernel = $kernel;
$this->locator = $locator;
$this->parser = $parser;
$this->rootDir = $rootDir;
}
@ -53,7 +50,13 @@ class TemplatePathsCacheWarmer extends CacheWarmer
*/
public function warmUp($cacheDir)
{
$templates = $this->computeTemplatePaths();
$templates = array();
foreach ($this->kernel->getBundles() as $name => $bundle) {
$templates += $this->findTemplatesIn($bundle->getPath().self::TEMPLATES_PATH_IN_BUNDLE, $name);
}
$templates += $this->findTemplatesIn($this->rootDir);
$this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
}
@ -68,35 +71,31 @@ class TemplatePathsCacheWarmer extends CacheWarmer
return false;
}
protected function computeTemplatePaths()
/**
* Find templates in the given directory
*
* @param string $dir The folder where to look for templates
* @param string $bundle The name of the bundle (null when out of a bundle)
*
* @return array An array of template paths
*/
protected function findTemplatesIn($dir, $bundle = null)
{
$prefix = '/Resources/views';
$templates = array();
foreach ($this->kernel->getBundles() as $name => $bundle) {
if (!is_dir($dir = $bundle->getPath().$prefix)) {
continue;
}
if (is_dir($dir)) {
$finder = new Finder();
foreach ($finder->files()->followLinks()->in($dir) as $file) {
$template = $this->parser->parseFromFilename($file->getRelativePathname());
if (false !== $template) {
$template->set('bundle', $name);
$templates[$template->getSignature()] = $this->locator->locate($template->getPath(), $this->rootDir);
}
}
}
if (is_dir($this->rootDir)) {
$finder = new Finder();
foreach ($finder->files()->followLinks()->in($this->rootDir) as $file) {
$template = $this->parser->parseFromFilename($file->getRelativePathname());
if (false !== $template) {
if (null !== $bundle) {
$template->set('bundle', $bundle);
}
$templates[$template->getSignature()] = $file->getRealPath();
}
}
}
return $templates;
return $templates;
}
}

View File

@ -41,7 +41,6 @@
<service id="templating.cache_warmer.template_paths" class="%templating.cache_warmer.template_paths.class%" public="false">
<argument type="service" id="kernel" />
<argument type="service" id="file_locator" />
<argument type="service" id="templating.name_parser" />
<argument>%kernel.root_dir%/views</argument>
</service>