Warmup twig templates in non-standard paths (closes #12507)
This commit is contained in:
parent
ffed0cec10
commit
96cce38b00
@ -11,9 +11,11 @@
|
|||||||
|
|
||||||
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
|
namespace Symfony\Bundle\TwigBundle\CacheWarmer;
|
||||||
|
|
||||||
|
use Symfony\Component\Finder\Finder;
|
||||||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
|
||||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
|
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
|
||||||
|
use Symfony\Component\Templating\TemplateReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates the Twig cache for all templates.
|
* Generates the Twig cache for all templates.
|
||||||
@ -27,14 +29,16 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
|
|||||||
{
|
{
|
||||||
protected $container;
|
protected $container;
|
||||||
protected $finder;
|
protected $finder;
|
||||||
|
private $paths;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param ContainerInterface $container The dependency injection container
|
* @param ContainerInterface $container The dependency injection container
|
||||||
* @param TemplateFinderInterface $finder The template paths cache warmer
|
* @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
|
// We don't inject the Twig environment directly as it depends on the
|
||||||
// template locator (via the loader) which might be a cached one.
|
// template locator (via the loader) which might be a cached one.
|
||||||
@ -42,6 +46,7 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
|
|||||||
// has been warmed up
|
// has been warmed up
|
||||||
$this->container = $container;
|
$this->container = $container;
|
||||||
$this->finder = $finder;
|
$this->finder = $finder;
|
||||||
|
$this->paths = $paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,7 +58,13 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
|
|||||||
{
|
{
|
||||||
$twig = $this->container->get('twig');
|
$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')) {
|
if ('twig' !== $template->get('engine')) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -75,4 +86,32 @@ class TemplateCacheCacheWarmer implements CacheWarmerInterface
|
|||||||
{
|
{
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,6 +77,8 @@ class TwigExtension extends Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
|
||||||
|
|
||||||
// register bundles as Twig namespaces
|
// register bundles as Twig namespaces
|
||||||
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
|
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
|
||||||
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
|
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
|
<service id="twig.cache_warmer" class="%twig.cache_warmer.class%" public="false">
|
||||||
<argument type="service" id="service_container" />
|
<argument type="service" id="service_container" />
|
||||||
<argument type="service" id="templating.finder" />
|
<argument type="service" id="templating.finder" />
|
||||||
|
<argument type="collection" /> <!-- Twig paths -->
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
|
<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
|
||||||
|
Reference in New Issue
Block a user