8000 [TwigBundle] Allow to exclude files and directories on cache warmup by yceruto · Pull Request #34416 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Allow to exclude files and directories on cache warmup #34416

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

Closed
wants to merge 1 commit into from
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
Allow to configure the files and directories to warmup on cache
  • Loading branch information
yceruto committed Nov 16, 2019
commit a6f4cfa031274d75ca1465ca98e12ca32c3b4107
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* added `cache_pattern_files` and `cache_exclude_paths` options, allowing to configure the files and directories to warmup on cache

5.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
->scalarNode('autoescape_service_method')->defaultNull()->end()
->scalarNode('base_template_class')->example('Twig\Template')->cannotBeEmpty()->end()
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
->arrayNode('cache_pattern_files')
->beforeNormalization()->ifString()->then(static function ($v) { return [$v]; })->end()
->prototype('scalar')->end()
->end()
->arrayNode('cache_exclude_paths')
->addDefaultChildrenIfNoneSet()
->prototype('scalar')->defaultValue('bundles')->end()
->validate()
->ifTrue(static function ($v) { return !\in_array('bundles', $v, true); })
->then(static function ($v) {
return array_merge(['bundles'], $v);
})
->end()
->end()
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
->booleanNode('strict_variables')->defaultValue('%kernel.debug%')->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public function load(array $configs, ContainerBuilder $container)
}
}

$container->getDefinition('twig.template_iterator')
->setArgument(3, $config['cache_pattern_files'])
->setArgument(4, $config['cache_exclude_paths'])
;

if (isset($config['autoescape_service']) && isset($config['autoescape_service_method'])) {
$config['autoescape'] = [new Reference($config['autoescape_service']), $config['autoescape_service_method']];
}
Expand Down
20 changes: 16 additions & 4 deletions src/Symfony/Bundle/TwigBundle/TemplateIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ class TemplateIterator implements \IteratorAggregate
private $templates;
private $paths;
private $defaultPath;
private $patternFiles;
private $excludePaths;

/**
* @param array $paths Additional Twig paths to warm
* @param string|null $defaultPath The directory where global templates can be stored
*/
public function __construct(KernelInterface $kernel, array $paths = [], string $defaultPath = null)
public function __construct(KernelInterface $kernel, array $paths = [], string $defaultPath = null, array $patternFiles = [], array $excludePaths = ['bundles'])
{
$this->kernel = $kernel;
$this->paths = $paths;
$this->defaultPath = $defaultPath;
$this->patternFiles = $patternFiles;
$this->excludePaths = $excludePaths;
}

public function getIterator(): \Traversable
Expand All @@ -45,7 +49,7 @@ public function getIterator(): \Traversable
return $this->templates;
}

$templates = null !== $this->defaultPath ? $this->findTemplatesInDirectory($this->defaultPath, null, ['bundles']) : [];
$templates = null !== $this->defaultPath ? $this->findTemplatesInDirectory($this->defaultPath, null, $this->excludePaths) : [];

foreach ($this->kernel->getBundles() as $bundle) {
$name = $bundle->getName();
Expand Down Expand Up @@ -74,14 +78,22 @@ public function getIterator(): \Traversable
*
* @return string[]
*/
private function findTemplatesInDirectory(string $dir, string $namespace = null, array $excludeDirs = []): array
private function findTemplatesInDirectory(string $dir, string $namespace = null, array $excludePaths = []): array
{
if (!is_dir($dir)) {
return [];
}

$finder = Finder::create()
->files()
->followLinks()
->name($this->patternFiles)
->in($dir)
->exclude($excludePaths)
;

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

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public function testGetIterator()
$kernel->expects($this->any())->method('getBundles')->willReturn([
$bundle,
]);
$iterator = new TemplateIterator($kernel, [__DIR__.'/Fixtures/templates/Foo' => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates');
$patternFiles = ['/.*(?<!\.txt)$/']; // excluding *.txt files
$excludePaths = ['bundles', 'node_modules'];
$iterator = new TemplateIterator($kernel, [__DIR__.'/Fixtures/templates/Foo' => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates', $patternFiles, $excludePaths);

$sorted = iterator_to_array($iterator);
sort($sorted);
Expand Down
0