8000 Use an option for twig file names · symfony/symfony@ac7ff46 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac7ff46

Browse files
committed
Use an option for twig file names
1 parent 4303b4b commit ac7ff46

File tree

6 files changed

+77
-14
lines changed

6 files changed

+77
-14
lines changed

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add option `twig.file_name_pattern` to restrict which files are compiled by cache warmer.
8+
49
6.0
510
---
611

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ private function addTwigOptions(ArrayNodeDefinition $rootNode)
141141
->info('The default path used to load templates')
142142
->defaultValue('%kernel.project_dir%/templates')
143143
->end()
144+
->arrayNode('file_name_pattern')
145+
->example('*.twig')
146+
->info('Pattern of file name used for cache warmer and linter')
147+
->beforeNormalization()
148+
->ifString()
149+
->then(function ($value) { return [$value]; })
150+
->end()
151+
->prototype('scalar')->end()
152+
->end()
144153
->arrayNode('paths')
145154
->normalizeKeys(false)
146155
->useAttributeAsKey('paths')

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public function load(array $configs, ContainerBuilder $container)
9595
}
9696

9797
// paths are modified in ExtensionPass if forms are enabled
98-
$container->getDefinition('twig.template_iterator')->replaceArgument(1, $config['paths']);
98+
$container->getDefinition('twig.template_iterator')
99+
->replaceArgument(1, $config['paths'])
100+
->replaceArgument(3, $config['file_name_pattern']);
99101

100102
foreach ($this->getBundleTemplatePaths($container, $config) as $name => $paths) {
101103
$namespace = $this->normalizeBundleName($name);

src/Symfony/Bundle/TwigBundle/Resources/config/twig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])
7676

7777
->set('twig.template_iterator', TemplateIterator::class)
78-
->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path')])
78+
->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path'), abstract_arg('File name pattern')])
7979

8080
->set('twig.template_cache_warmer', TemplateCacheWarmer::class)
8181
->args([service(ContainerInterface::class), service('twig.template_iterator')])

src/Symfony/Bundle/TwigBundle/TemplateIterator.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ class TemplateIterator implements \IteratorAggregate
2929
private \Traversable $templates;
3030
private array $paths;
3131
private ?string $defaultPath;
32+
private ?array $fileNames;
3233

3334
/**
34-
* @param array $paths Additional Twig paths to warm
35-
* @param string|null $defaultPath The directory where global templates can be stored
35+
* @param array $paths Additional Twig paths to warm
36+
* @param string|null $defaultPath The directory where global templates can be stored
37+
* @param string[]|null $fileNames Pattern of file names
3638
*/
37-
public function __construct(KernelInterface $kernel, array $paths = [], string $defaultPath = null)
39+
public function __construct(KernelInterface $kernel, array $paths = [], string $defaultPath = null, array $fileNames = null)
3840
{
3941
$this->kernel = $kernel;
4042
$this->paths = $paths;
4143
$this->defaultPath = $defaultPath;
44+
$this->fileNames = $fileNames;
4245
}
4346

4447
public function getIterator(): \Traversable
@@ -82,8 +85,19 @@ private function findTemplatesInDirectory(string $dir, string $namespace = null,
8285
}
8386

8487
$templates = [];
85-
foreach (Finder::create()->files()->followLinks()->in($dir)->exclude($excludeDirs)->name('*.twig') as $file) {
88+
$finder = Finder::create()->files()->followLinks()->in($dir)->exclude($excludeDirs);
89+
if (null !== $this->fileNames) {
90+
$finder->name($this->fileNames);
91+
} else {
92+
$deprecationNotice = true;
93+
}
94+
foreach ($finder as $file) {
8695
$templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname());
96+
97+
if (null === $this->fileNames && $deprecationNotice && 'twig' !== $file->getExtension()) {
98+
trigger_deprecation('symfony/twig-bundle', '6.1', 'Directory "%s" contains non "*.twig" files. They will not be compiled by cache warmer in Symfony 7.0. Set "twig.file_name_pattern: \'*.twig\'" in your configuration.', $dir);
99+
$deprecationNotice = false;
100+
}
87101
}
88102

89103
return $templates;

src/Symfony/Bundle/TwigBundle/Tests/TemplateIteratorTest.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,41 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\Tests;
1313

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Bundle\TwigBundle\TemplateIterator;
1516
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
1617
use Symfony\Component\HttpKernel\Kernel;
1718

1819
class TemplateIteratorTest extends TestCase
1920
{
21+
use ExpectDeprecationTrait;
22+
2023
public function testGetIterator()
2124
{
22-
$bundle = $this->createMock(BundleInterface::class);
23-
$bundle->expects($this->any())->method('getName')->willReturn('BarBundle');
24-
$bundle->expects($this->any())->method('getPath')->willReturn(__DIR__.'/Fixtures/templates/BarBundle');
25+
$iterator = new TemplateIterator($this->createKernelMock(), [__DIR__.'/Fixtures/templates/Foo' => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates', ['*.twig']);
2526

26-
$kernel = $this->createMock(Kernel::class);
27-
$kernel->expects($this->any())->method('getBundles')->willReturn([
28-
$bundle,
29-
]);
30-
$iterator = new TemplateIterator($kernel, [__DIR__.'/Fixtures/templates/Foo' => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates');
27+
$sorted = iterator_to_array($iterator);
28+
sort($sorted);
29+
$this->assertEquals(
30+
[
31+
'@Bar/index.html.twig',
32+
'@Bar/layout.html.twig',
33+
'@Foo/index.html.twig',
34+
'layout.html.twig',
35+
],
36+
$sorted
37+
);
38+
}
39+
40+
/**
41+
* @group legacy
42+
*/
43+
public function testGetIteratorDeprecateNonTwig()
44+
{
45+
$dir = __DIR__.'/Fixtures/templates/Foo';
46+
$this->expectDeprecation(sprintf('Since symfony/twig-bundle 6.1: Directory "%s" contains non "*.twig" files. They will not be compiled by cache warmer in Symfony 7.0. Set "twig.file_name_pattern: \'*.twig\'" in your configuration.', $dir));
47+
48+
$iterator = new TemplateIterator($this->createKernelMock(), [$dir => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates', null);
3149

3250
$sorted = iterator_to_array($iterator);
3351
sort($sorted);
@@ -36,9 +54,24 @@ public function testGetIterator()
3654
'@Bar/index.html.twig',
3755
'@Bar/layout.html.twig',
3856
'@Foo/index.html.twig',
57+
'@Foo/not-twig.js',
3958
'layout.html.twig',
4059
],
4160
$sorted
4261
);
4362
}
63+
64+
private function createKernelMock(): Kernel
65+
{
66+
$bundle = $this->createMock(BundleInterface::class);
67+
$bundle->expects($this->any())->method('getName')->willReturn('BarBundle');
68+
$bundle->expects($this->any())->method('getPath')->willReturn(__DIR__.'/Fixtures/templates/BarBundle');
69+
70+
$kernel = $this->createMock(Kernel::class);
71+
$kernel->expects($this->any())->method('getBundles')->willReturn([
72+
$bundle,
73+
]);
74+
75+
return $kernel;
76+
}
4477
}

0 commit comments

Comments
 (0)
0