8000 [TwigBundle] added a Twig templates warmer when templating is disabled by fabpot · Pull Request #16271 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] added a Twig templates warmer when templating is disabled #16271

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
merged 1 commit into from
Oct 19, 2015
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 @@ -45,7 +45,7 @@ public function __construct(KernelInterface $kernel, TemplateNameParserInterface
/**
* Find all the templates in the bundle and in the kernel Resources folder.
*
* @return array An array of templates of type TemplateReferenceInterface
* @return TemplateReferenceInterface[]
*/
public function findAllTemplates()
{
Expand All @@ -69,7 +69,7 @@ public function findAllTemplates()
*
* @param string $dir The folder where to look for templates
*
* @return array An array of templates of type TemplateReferenceInterface
* @return TemplateReferenceInterface[]
*/
private function findTemplatesInFolder($dir)
{
Expand All @@ -93,7 +93,7 @@ private function findTemplatesInFolder($dir)
*
* @param BundleInterface $bundle The bundle where to look for templates
*
* @return array An array of templates of type TemplateReferenceInterface
* @return TemplateReferenceInterface[]
*/
private function findTemplatesInBundle(BundleInterface $bundle)
{
Expand Down
54 changes: 54 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
public function __construct(\Twig_Environment $twig, \Traversable $iterator)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

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

namespace Symfony\Bundle\TwigBundle\CacheWarmer;

use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

/**
* Generates the Twig cache for all templates.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateCacheWarmer implements CacheWarmerInterface
{
private $twig;
private $iterator;

{
$this->twig = $twig;
$this->iterator = $iterator;
}

/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
foreach ($this->iterator as $template) {
try {
$this->twig->loadTemplate($template);
} catch (\Twig_Error $e) {
// problem during compilation, give up
// might be a syntax error or a non-Twig template
}
}
}

/**
* {@inheritdoc}
*/
public function isOptional()
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function load(array $configs, ContainerBuilder $container)
}

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

// register bundles as Twig namespaces
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
<argument type="collection" /> <!-- Twig paths -->
</service>

<service id="twig.template_iterator" class="Symfony\Bundle\TwigBundle\TemplateIterator" public="false">
<argument type="service" id="kernel" />
<argument>%kernel.root_dir%</argument>
<argument type="collection" /> <!-- Twig paths -->
</service>

<service id="twig.template_cache_warmer" class="Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer" public="false">
<tag name="kernel.cache_warmer" />
<argument type="service" id="twig" />
<argument type="service" id="twig.template_iterator" />
</service>

<service id="twig.loader.native_filesystem" class="Twig_Loader_Filesystem" public="false">
<argument type="collection" />
</service>
Expand Down
92 changes: 92 additions & 0 deletions src/Symfony/Bundle/TwigBundle/TemplateIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

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

namespace Symfony\Bundle\TwigBundle;

use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Finder\Finder;

/**
* Iterator for all templates in bundles and in the application Resources directory.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TemplateIterator implements \IteratorAggregate
{
private $kernel;
private $rootDir;
private $templates;
private $paths;

/**
* @param KernelInterface $kernel A KernelInterface instance
* @param string $rootDir The directory where global templates can be stored
* @param array $paths Additional Twig paths to warm
*/
public function __construct(KernelInterface $kernel, $rootDir, array $paths = array())
{
$this->kernel = $kernel;
$this->rootDir = $rootDir;
$this->paths = $paths;
}

/**
* {@inheritdoc}
*/
public function getIterator()
{
if (null !== $this->templates) {
return $this->templates;
}

$this->templates = $this->findTemplatesInDirectory($this->rootDir.'/Resources/views');
foreach ($this->kernel->getBundles() as $bundle) {
$name = $bundle->getName();
if ('Bundle' === substr($name, -6)) {
$name = substr($name, 0, -6);
}

$this->templates = array_merge(
$this->templates,
$this->findTemplatesInDirectory($bundle->getPath().'/Resources/views', $name),
$this->findTemplatesInDirectory($this->rootDir.'/'.$bundle->getName().'/views', $name)
);
}

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

return $this->templates = new \ArrayIterator(array_unique($this->templates));
}

/**
* Find templates in the given directory.
*
* @param string $dir The directory where to look for templates
* @param string|null $namespace The template namespace
*
* @return array
*/
private function findTemplatesInDirectory($dir, $namespace = null)
{
if (!is_dir($dir)) {
return array();
}

$templates = array();
foreach (Finder::create()->files()->followLinks()->in($dir) as $file) {
$templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname());
}

return $templates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# Twig template #}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# Twig template #}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# Twig template #}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# Twig template #}
42 changes: 42 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

namespace Symfony\Bundle\TwigBundle\Tests;

use Symfony\Bundle\TwigBundle\TemplateIterator;

class TemplateIteratorTest extends TestCase
{
public function testGetIterator()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
$bundle->expects($this->any())->method('getName')->will($this->returnValue('BarBundle'));
$bundle->expects($this->any())->method('getPath')->will($this->returnValue(__DIR__.'/Fixtures/templates/BarBundle'));

$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Kernel')->disableOriginalConstructor()->getMock();
$kernel->expects($this->any())->method('getBundles')->will($this->returnValue(array(
$bundle,
)));
$iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', array(__DIR__.'/Fixtures/templates/Foo' => 'Foo'));

$sorted = iterator_to_array($iterator);
sort($sorted);
$this->assertEquals(
array(
'@Bar/index.html.twig',
'@Foo/index.html.twig',
'layout.html.twig',
'sub/sub.html.twig',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on windows fails, I think DIRECTORY_SEPARATOR fixed it

'sub'.DIRECTORY_SEPARATOR.'sub.html.twig',

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've normalized the output of the generator instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the normalization is not the best way. Twig internally doesn't normalize either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but Twig uses what the developer uses as name, which may not be the DIRECTORY_SEPARATOR (it is even likely it is not, because you don't have access to this constant easily when including a template)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, when twig resolves namespaces, it uses the local filesystem separators in its final path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tobion this iterator is meant to return template names, not cache keys. So it is the input of the Twig resolution, not the output.

),
$sorted
);
}
}
0