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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Bundle\FrameworkBundle\Templating\CacheWarmer;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the association between template names and their paths on the disk.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class TemplatePathsCacheWarmer extends CacheWarmer
|
|
|
|
{
|
|
|
|
protected $kernel;
|
|
|
|
protected $rootDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2011-01-25 16:20:20 +00:00
|
|
|
* @param KernelInterface $kernel A KernelInterface instance
|
|
|
|
* @param string $rootDir The directory where global templates can be stored
|
2011-01-24 10:27:07 +00:00
|
|
|
*/
|
2011-01-25 16:20:20 +00:00
|
|
|
public function __construct(KernelInterface $kernel, $rootDir)
|
2011-01-24 10:27:07 +00:00
|
|
|
{
|
|
|
|
$this->kernel = $kernel;
|
|
|
|
$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) {
|
|
|
|
if (!is_dir($dir = $bundle->getPath().$prefix)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$finder = new Finder();
|
2011-01-25 08:55:03 +00:00
|
|
|
foreach ($finder->files()->followLinks()->in($dir) as $file) {
|
2011-01-26 13:53:12 +00:00
|
|
|
if (false !== $template = $this->parseTemplateName($file, $prefix.'/', $bundle->getName())) {
|
|
|
|
$resource = '@'.$template['bundle'].'/Resources/views/'.$template['controller'].'/'.$template['name'].'.'.$template['format'].'.'.$template['engine'];
|
2011-01-24 10:27:07 +00:00
|
|
|
|
2011-01-26 13:53:12 +00:00
|
|
|
$templates[md5(serialize($template))] = $this->kernel->locateResource($resource, $this->rootDir);
|
|
|
|
}
|
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-01-26 13:53:12 +00:00
|
|
|
if (false !== $template = $this->parseTemplateName($file, strtr($this->rootDir, '\\', '/').'/')) {
|
|
|
|
$templates[md5(serialize($template))] = (string) $file;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-26 13:53:12 +00:00
|
|
|
protected function parseTemplateName($file, $prefix, $bundle = '')
|
2011-01-24 10:27:07 +00:00
|
|
|
{
|
2011-01-24 21:05:48 +00:00
|
|
|
$path = strtr($file->getPathname(), '\\', '/');
|
2011-01-24 10:27:07 +00:00
|
|
|
|
|
|
|
list(, $tmp) = explode($prefix, $path, 2);
|
|
|
|
$parts = explode('/', strtr($tmp, '\\', '/'));
|
|
|
|
|
2011-01-26 13:53:12 +00:00
|
|
|
$elements = explode('.', array_pop($parts));
|
|
|
|
if (3 !== count($elements)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'bundle' => $bundle,
|
|
|
|
'controller' => implode('/', $parts),
|
|
|
|
'name' => $elements[0],
|
|
|
|
'format' => $elements[1],
|
|
|
|
'engine' => $elements[2],
|
|
|
|
);
|
2011-01-24 10:27:07 +00:00
|
|
|
}
|
|
|
|
}
|