Skip to content

Commit

Permalink
[Template cache warmers] Factorize common code
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Apr 23, 2011
1 parent 5308a5c commit 33dd89f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
use \Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateFinderInterface;
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;

/**
Expand All @@ -24,25 +22,19 @@
*/
class TemplatePathsCacheWarmer extends CacheWarmer
{
protected $kernel;
protected $parser;
protected $rootDir;
protected $finder;
protected $locator;

/**
* Constructor.
*
* @param KernelInterface $kernel A KernelInterface instance
* @param TemplateNameParser $parser A TemplateNameParser instance
* @param TemplateLocator $locator The template locator
* @param string $rootDir The directory where global templates can be stored
* @param TemplateFinderInterface $finder A template finder
* @param TemplateLocator $locator The template locator
*/
public function __construct(KernelInterface $kernel, TemplateNameParser $parser, TemplateLocator $locator, $rootDir)
public function __construct(TemplateFinderInterface $finder, TemplateLocator $locator)
{
$this->kernel = $kernel;
$this->parser = $parser;
$this->finder = $finder;
$this->locator = $locator;
$this->rootDir = $rootDir;
}

/**
Expand All @@ -54,12 +46,10 @@ public function warmUp($cacheDir)
{
$templates = array();

foreach ($this->kernel->getBundles() as $name => $bundle) {
$templates += $this->findTemplatesIn($bundle->getPath().'/Resources/views', $name);
foreach ($this->finder->findAllTemplates() as $template) {
$templates[$template->getSignature()] = $this->locator->locate($template);
}

$templates += $this->findTemplatesIn($this->rootDir.'/views');

$this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
}

Expand All @@ -72,32 +62,4 @@ public function isOptional()
{
return false;
}

/**
* 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)
{
$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) {
if (null !== $bundle) {
$template->set('bundle', $bundle);
}
$templates[$template->getSignature()] = $this->locator->locate($template);
}
}
}

return $templates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<parameter key="templating.loader.filesystem.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\FilesystemLoader</parameter>
<parameter key="templating.loader.cache.class">Symfony\Component\Templating\Loader\CacheLoader</parameter>
<parameter key="templating.loader.chain.class">Symfony\Component\Templating\Loader\ChainLoader</parameter>
<parameter key="templating.finder.class">Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateFinder</parameter>
</parameters>

<services>
Expand All @@ -34,13 +35,17 @@
<argument type="service" id="file_locator" />
</service>

<service id="templating.cache_warmer.template_paths" class="%templating.cache_warmer.template_paths.class%" public="false">
<service id="templating.finder" class="%templating.finder.class%" public="false">
<argument type="service" id="kernel" />
<argument type="service" id="templating.name_parser" />
<argument type="service" id="templating.locator.uncached" />
<argument>%kernel.root_dir%/Resources</argument>
</service>

<service id="templating.cache_warmer.template_paths" class="%templating.cache_warmer.template_paths.class%" public="false">
<argument type="service" id="templating.finder" />
<argument type="service" id="templating.locator.uncached" />
</service>

<service id="templating.loader.filesystem" class="%templating.loader.filesystem.class%" public="false">
<argument type="service" id="templating.locator" />
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Templating\Loader;

use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

/**
* Finds all the templates.
*
* @author Victor Berchet <[email protected]>
*/
class TemplateFinder implements TemplateFinderInterface
{
private $kernel;
private $parser;
private $rootDir;
private $templates;

/**
* Constructor.
*
* @param KernelInterface $kernel A KernelInterface instance
* @param TemplateNameParser $parser A TemplateNameParser instance
* @param string $rootDir The directory where global templates can be stored
*/
public function __construct(KernelInterface $kernel, TemplateNameParser $parser, $rootDir)
{
$this->kernel = $kernel;
$this->parser = $parser;
$this->rootDir = $rootDir;
}

/**
* Find all the templates in the bundle and in the kernel Resources folder.
*
* @return array An array of templates of type TemplateReferenceInterface
*/
public function findAllTemplates()
{
if (null !== $this->templates) {
return $this->templates;
}

$templates = array();

foreach ($this->kernel->getBundles() as $name => $bundle) {
$templates = array_merge($templates, $this->findTemplatesInBundle($bundle));
}

$templates = array_merge($templates, $this->findTemplatesInFolder($this->rootDir.'/views'));

return $this->templates = $templates;
}

/**
* Find templates in the given directory.
*
* @param string $dir The folder where to look for templates
*
* @return array An array of templates of type TemplateReferenceInterface
*/
private function findTemplatesInFolder($dir)
{
$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) {
$templates[] = $template;
}
}
}

return $templates;
}

/**
* Find templates in the given bundle.
*
* @param BundleInterface $bundle The bundle where to look for templates
*
* @return array An array of templates of type TemplateReferenceInterface
*/
private function findTemplatesInBundle(BundleInterface $bundle)
{
$templates = $this->findTemplatesInFolder($bundle->getPath().'/Resources/views');
$name = $bundle->getName();

foreach ($templates as $i => $template) {
$templates[$i] = $template->set('bundle', $name);
}

return $templates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Templating\Loader;


/**
* Interface for finding all the templates.
*
* @author Victor Berchet <[email protected]>
*/
interface TemplateFinderInterface
{
/**
* Find all the templates.
*
* @return array An array of templates of type TemplateReferenceInterface
*/
function findAllTemplates();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
namespace Symfony\Bundle\TwigBundle\CacheWarmer;

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplatePathsCacheWarmer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateFinder;

/**
* Generates the Twig cache for all templates.
Expand All @@ -26,25 +27,22 @@
class TemplateCacheCacheWarmer extends CacheWarmer
{
protected $container;
protected $parser;
protected $kernel;
protected $warmer;

/**
* Constructor.
*
* @param ContainerInterface $container The dependency injection container
* @param string $rootDir The directory where global templates can be stored
* @param ContainerInterface $container The dependency injection container
* @param TemplatePathsCacheWarmer $warmer The template paths cache warmer
*/
public function __construct(ContainerInterface $container, $rootDir)
public function __construct(ContainerInterface $container, TemplateFinder $finder)
{
// 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).
// We don't inject the Twig environment directly as it depends on the
// template locator (via the loader) which might be a cached one.
// The cached template locator is available once the TemplatePathsCacheWarmer
// has been warmed up
$this->container = $container;
$this->parser = $container->get('templating.name_parser');
$this->kernel = $container->get('kernel');
$this->rootDir = $rootDir;
$this->finder = $finder;
}

/**
Expand All @@ -56,15 +54,11 @@ public function warmUp($cacheDir)
{
$twig = $this->container->get('twig');

foreach ($this->kernel->getBundles() as $name => $bundle) {
foreach ($this->findTemplatesIn($bundle->getPath().'/Resources/views', $name) as $template) {
foreach ($this->finder->findAllTemplates() as $template) {
if ('twig' === $template->get('engine')) {
$twig->loadTemplate($template);
}
}

foreach ($this->findTemplatesIn($this->rootDir.'/views') as $template) {
$twig->loadTemplate($template);
}
}

/**
Expand All @@ -76,32 +70,4 @@ public function isOptional()
{
return true;
}

/**
* 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 TemplateReference
*/
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);
}
$templates[] = $template->getLogicalName();
}
}
}

return $templates;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
<argument type="service" id="service_container" />
<argument>%kernel.root_dir%/Resources</argument>
<argument type="service" id="templating.finder" />
</service>

<service id="twig.loader" class="%twig.loader.class%">
Expand Down

0 comments on commit 33dd89f

Please sign in to comment.