-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...fony/Bundle/TwigBundle/Tests/Fixtures/templates/BarBundle/Resources/views/index.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{# Twig template #} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Foo/index.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{# Twig template #} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Resources/views/layout.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{# Twig template #} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Resources/views/sub/sub.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{# Twig template #} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
$sorted | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.