2011-01-24 10:27:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-27 11:22:32 +00:00
|
|
|
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
|
2011-01-25 16:20:20 +00:00
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
2011-01-24 10:27:07 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\FileLocatorInterface;
|
2011-02-10 17:20:44 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\Template;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the association between template names and their paths on the disk.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class TemplatePathsCacheWarmer extends CacheWarmer
|
|
|
|
{
|
2011-02-10 15:15:51 +00:00
|
|
|
protected $locator;
|
2011-01-24 10:27:07 +00:00
|
|
|
protected $kernel;
|
|
|
|
protected $rootDir;
|
2011-02-10 17:20:44 +00:00
|
|
|
protected $parser;
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param KernelInterface $kernel A KernelInterface instance
|
|
|
|
* @param FileLocatorInterface $locator A FileLocatorInterface instance
|
2011-02-14 21:25:23 +00:00
|
|
|
* @param TemplateNameParser $parser A TemplateNameParser instance
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param string $rootDir The directory where global templates can be stored
|
2011-01-24 10:27:07 +00:00
|
|
|
*/
|
2011-02-14 21:25:23 +00:00
|
|
|
public function __construct(KernelInterface $kernel, FileLocatorInterface $locator, TemplateNameParser $parser, $rootDir)
|
2011-01-24 10:27:07 +00:00
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
2011-02-10 15:15:51 +00:00
|
|
|
$this->locator = $locator;
|
2011-02-14 21:25:23 +00:00
|
|
|
$this->parser = $parser;
|
2011-01-24 10:27:07 +00:00
|
|
|
$this->rootDir = $rootDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Warms up the cache.
|
|
|
|
*
|
|
|
|
* @param string $cacheDir The cache directory
|
|
|
|
*/
|
|
|
|
public function warmUp($cacheDir)
|
|
|
|
{
|
|
|
|
$templates = $this->computeTemplatePaths();
|
|
|
|
|
|
|
|
$this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether this warmer is optional or not.
|
|
|
|
*
|
|
|
|
* @return Boolean always false
|
|
|
|
*/
|
|
|
|
public function isOptional()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function computeTemplatePaths()
|
|
|
|
{
|
|
|
|
$prefix = '/Resources/views';
|
|
|
|
$templates = array();
|
|
|
|
foreach ($this->kernel->getBundles() as $name => $bundle) {
|
2011-02-05 21:40:30 +00:00
|
|
|
if (!is_dir($dir = $bundle->getPath().$prefix)) {
|
2011-01-24 10:27:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
2011-01-25 08:55:03 +00:00
|
|
|
foreach ($finder->files()->followLinks()->in($dir) as $file) {
|
2011-02-16 08:39:12 +00:00
|
|
|
$template = $this->parser->parseFromFilename($file->getRelativePathname());
|
|
|
|
if (false !== $template) {
|
|
|
|
$template->set('bundle', $name);
|
2011-02-10 17:20:44 +00:00
|
|
|
$templates[$template->getSignature()] = $this->locator->locate($template->getPath(), $this->rootDir);
|
2011-01-26 13:53:12 +00:00
|
|
|
}
|
2011-01-24 10:27:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 07:29:11 +00:00
|
|
|
if (is_dir($this->rootDir)) {
|
|
|
|
$finder = new Finder();
|
2011-01-25 08:55:03 +00:00
|
|
|
foreach ($finder->files()->followLinks()->in($this->rootDir) as $file) {
|
2011-02-16 08:39:12 +00:00
|
|
|
$template = $this->parser->parseFromFilename($file->getRelativePathname());
|
|
|
|
if (false !== $template) {
|
2011-02-10 17:20:44 +00:00
|
|
|
$templates[$template->getSignature()] = $file->getRealPath();
|
2011-01-26 13:53:12 +00:00
|
|
|
}
|
2011-01-25 07:29:11 +00:00
|
|
|
}
|
2011-01-24 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 10:27:07 +00:00
|
|
|
return $templates;
|
|
|
|
}
|
|
|
|
}
|