2011-01-24 10:27:34 +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:34 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
|
|
|
|
|
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2011-03-18 10:23:23 +00:00
|
|
|
use Symfony\Component\Finder\Finder;
|
2011-01-24 10:27:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the Twig cache for all templates.
|
|
|
|
*
|
|
|
|
* This warmer must be registered after TemplatePathsCacheWarmer,
|
|
|
|
* as the Twig loader will need the cache generated by it.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-01-24 10:27:34 +00:00
|
|
|
*/
|
|
|
|
class TemplateCacheCacheWarmer extends CacheWarmer
|
|
|
|
{
|
|
|
|
protected $container;
|
2011-03-18 10:23:23 +00:00
|
|
|
protected $parser;
|
|
|
|
protected $kernel;
|
2011-01-24 10:27:34 +00:00
|
|
|
|
2011-03-18 10:23:23 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container The dependency injection container
|
|
|
|
* @param string $rootDir The directory where global templates can be stored
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container, $rootDir)
|
2011-01-24 10:27:34 +00:00
|
|
|
{
|
|
|
|
// we don't inject the Twig environment directly as it needs
|
|
|
|
// the loader, which is a cached one, and the cache is not
|
|
|
|
// yet available when this instance is created (the
|
|
|
|
// TemplateCacheCacheWarmer has not been run yet).
|
|
|
|
$this->container = $container;
|
2011-03-18 10:23:23 +00:00
|
|
|
$this->parser = $container->get('templating.name_parser');
|
|
|
|
$this->kernel = $container->get('kernel');
|
|
|
|
$this->rootDir = $rootDir;
|
2011-01-24 10:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Warms up the cache.
|
|
|
|
*
|
|
|
|
* @param string $cacheDir The cache directory
|
|
|
|
*/
|
|
|
|
public function warmUp($cacheDir)
|
|
|
|
{
|
|
|
|
$twig = $this->container->get('twig');
|
2011-03-18 10:23:23 +00:00
|
|
|
|
|
|
|
foreach ($this->kernel->getBundles() as $name => $bundle) {
|
2011-03-28 21:04:39 +01:00
|
|
|
foreach ($this->findTemplatesIn($bundle->getPath().'/Resources/views', $name) as $template) {
|
|
|
|
$twig->loadTemplate($template);
|
|
|
|
}
|
2011-03-18 10:23:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 21:04:39 +01:00
|
|
|
foreach ($this->findTemplatesIn($this->rootDir.'/views') as $template) {
|
2011-03-18 10:30:23 +00:00
|
|
|
$twig->loadTemplate($template);
|
2011-01-24 10:27:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether this warmer is optional or not.
|
|
|
|
*
|
|
|
|
* @return Boolean always true
|
|
|
|
*/
|
|
|
|
public function isOptional()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-18 10:23:23 +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)
|
|
|
|
*
|
2011-03-18 10:30:23 +00:00
|
|
|
* @return array An array of TemplateReference
|
2011-03-18 10:23:23 +00:00
|
|
|
*/
|
|
|
|
protected function findTemplatesIn($dir, $bundle = null)
|
|
|
|
{
|
|
|
|
$templates = array();
|
|
|
|
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
$finder = new Finder();
|
|
|
|
foreach ($finder->files()->followLinks()->in($dir) as $file) {
|
|
|
|
$template = $this->parser->parseFromFilename($file->getRelativePathname());
|
|
|
|
if (false !== $template && 'twig' == $template->get('engine')) {
|
|
|
|
if (null !== $bundle) {
|
|
|
|
$template->set('bundle', $bundle);
|
|
|
|
}
|
2011-04-06 10:43:35 +01:00
|
|
|
$templates[] = $template->getLogicalName();
|
2011-03-18 10:23:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $templates;
|
|
|
|
}
|
2011-01-24 10:27:34 +00:00
|
|
|
}
|