10BC0 [TwigBundle] Warmup twig templates in non-standard paths by kbond · Pull Request #14764 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
10BC0
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Symfony\Bundle\TwigBundle\CacheWarmer;

use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
use Symfony\Component\Templating\TemplateReference;

/**
* Generates the Twig cache for all templates.
Expand All @@ -27,21 +29,24 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
{
protected $container;
protected $finder;
private $paths;

/**
* Constructor.
*
* @param ContainerInterface $container The dependency injection container
* @param TemplateFinderInterface $finder The template paths cache warmer
* @param array $paths Additional twig paths to warm
*/
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder)
public function __construct(ContainerInterface $container, TemplateFinderInterface $finder, array $paths = array())
{
// 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->finder = $finder;
$this->paths = $paths;
}

/**
Expand All @@ -53,7 +58,13 @@ public function warmUp($cacheDir)
{
$twig = $this->container->get('twig');

foreach ($this->finder->findAllTemplates() as $template) {
$templates = $this->finder->findAllTemplates();

foreach ($this->paths as $path => $namespace) {
$templates = array_merge($templates, $this->findTemplatesInFolder($namespace, $path));
}

foreach ($templates as $template) {
if ('twig' !== $template->get('engine')) {
continue;
}
Expand All @@ -75,4 +86,32 @@ public function isOptional()
{
return true;
}

/**
* Find templates in the given directory.
*
* @param string $namespace The namespace for these templates
* @param string $dir The folder where to look for templates
*
* @return array An array of templates of type TemplateReferenceInterface
*/
private function findTemplatesInFolder($namespace, $dir)
{
if (!is_dir($dir)) {
return array();
}

$templates = array();
$finder = new Finder();

foreach ($finder->files()->followLinks()->in($dir) as $file) {
$name = $file->getRelativePathname();
$templates[] = new TemplateReference(
$namespace ? sprintf('@%s/%s', $namespace, $name) : $name,
'twig'
);
}

return $templates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public function load(array $configs, ContainerBuilder $container)
}
}

$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);

// register bundles as Twig namespaces
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="templating.finder" />
<argument type="collection" /> <!-- Twig paths -->
</service>

<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
Expand Down
0