2011-01-24 10:27:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-24 10:27:07 +00:00
|
|
|
*
|
|
|
|
* 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 17:20:44 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
|
2011-04-05 20:04:00 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the association between template names and their paths on the disk.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-24 10:27:07 +00:00
|
|
|
*/
|
|
|
|
class TemplatePathsCacheWarmer extends CacheWarmer
|
|
|
|
{
|
|
|
|
protected $kernel;
|
2011-02-10 17:20:44 +00:00
|
|
|
protected $parser;
|
2011-04-05 20:04:00 +01:00
|
|
|
protected $rootDir;
|
|
|
|
protected $locator;
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-02-10 15:15:51 +00:00
|
|
|
* @param KernelInterface $kernel A KernelInterface instance
|
2011-02-14 21:25:23 +00:00
|
|
|
* @param TemplateNameParser $parser A TemplateNameParser instance
|
2011-04-05 20:04:00 +01:00
|
|
|
* @param TemplateLocator $locator The template locator
|
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-04-05 20:04:00 +01:00
|
|
|
public function __construct(KernelInterface $kernel, TemplateNameParser $parser, TemplateLocator $locator, $rootDir)
|
2011-01-24 10:27:07 +00:00
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
2011-02-14 21:25:23 +00:00
|
|
|
$this->parser = $parser;
|
2011-04-05 20:04:00 +01:00
|
|
|
$this->locator = $locator;
|
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)
|
2011-03-18 10:23:23 +00:00
|
|
|
{
|
2011-02-24 10:07:22 +00:00
|
|
|
$templates = array();
|
|
|
|
|
|
|
|
foreach ($this->kernel->getBundles() as $name => $bundle) {
|
2011-03-23 08:04:19 +00:00
|
|
|
$templates += $this->findTemplatesIn($bundle->getPath().'/Resources/views', $name);
|
2011-02-24 10:07:22 +00:00
|
|
|
}
|
|
|
|
|
2011-03-23 08:04:19 +00:00
|
|
|
$templates += $this->findTemplatesIn($this->rootDir.'/views');
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2011-02-24 10:07:22 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
2011-01-24 10:27:07 +00:00
|
|
|
{
|
|
|
|
$templates = array();
|
|
|
|
|
2011-02-24 10:07:22 +00:00
|
|
|
if (is_dir($dir)) {
|
2011-01-24 10:27:07 +00:00
|
|
|
$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) {
|
2011-02-24 10:07:22 +00:00
|
|
|
if (null !== $bundle) {
|
2011-04-15 20:12:02 +01:00
|
|
|
$template->set('bundle', $bundle);
|
|
|
|
}
|
2011-04-05 20:04:00 +01:00
|
|
|
$templates[$template->getSignature()] = $this->locator->locate($template);
|
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-02-24 10:07:22 +00:00
|
|
|
return $templates;
|
2011-01-24 10:27:07 +00:00
|
|
|
}
|
|
|
|
}
|