Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Templating #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Feb 24, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
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;

/**
Expand All @@ -25,7 +23,8 @@
*/
class TemplatePathsCacheWarmer extends CacheWarmer
{
protected $locator;
const TEMPLATES_PATH_IN_BUNDLE = '/Resources/views';

protected $kernel;
protected $rootDir;
protected $parser;
Expand All @@ -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;
}
Expand All @@ -52,8 +49,14 @@ public function __construct(KernelInterface $kernel, FileLocatorInterface $locat
* @param string $cacheDir The cache directory
*/
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)));
}
Expand All @@ -68,35 +71,31 @@ public function isOptional()
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down