Skip to content

Commit 5145ed4

Browse files
committed
feature #45845 [TwigBundle]  Pre-compile only *.twig files in cache warmup (GromNaN)
This PR was merged into the 6.1 branch. Discussion ---------- [TwigBundle]  Pre-compile only *.twig files in cache warmup | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - In my project, the template directory contains all the frontend assets (CSS, JS and images), this is a choice to locate all component files in the same directories. But the asset files are compiled as if they were twig template, which is a waste of time and storage. ``` front/ ├─ components/ │ ├─ video/ │ │ ├─ templates/ │ │ │ ├─ video.html.twig │ │ ├─ styles/ │ │ │ ├─ video.css │ │ ├─ scripts/ │ │ │ ├─ video.js ``` ~This patch limit warmup to `*.twig` files.~ This PR adds an option to restrict file pattern, for cache warmup and lint. ```yaml twig: file_name_pattern: "*.twig" ``` | | Before | After | |------------------|--------|-------| | Time to generate | 25s | 9s | | Storage | 80MB | 26MB | | Number of files | 4800 | 1500 | The filter on `*.twig` files is already in the `lint:twig` command. Note: If a non-twig template is included in a template (ex: to inline CSS), the `source()` function must be used ; the cache is not used in this case. Commits ------- 4de7a0227b Use an option for twig file names in cache warmer and linter
2 parents 22aee69 + f1899f4 commit 5145ed4

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Command/LintCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@
3939
#[AsCommand(name: 'lint:twig', description: 'Lint a Twig template and outputs encountered errors')]
4040
class LintCommand extends Command
4141
{
42+
protected string|array $namePatterns;
4243
private Environment $twig;
4344
private string $format;
4445

45-
public function __construct(Environment $twig)
46+
public function __construct(Environment $twig, string|array $namePatterns = ['*.twig'])
4647
{
4748
parent::__construct();
4849

4950
$this->twig = $twig;
51+
$this->namePatterns = $namePatterns;
5052
}
5153

5254
protected function configure()
@@ -146,7 +148,7 @@ protected function findFiles(string $filename)
146148
if (is_file($filename)) {
147149
return [$filename];
148150
} elseif (is_dir($filename)) {
149-
return Finder::create()->files()->in($filename)->name('*.twig');
151+
return Finder::create()->files()->in($filename)->name($this->namePatterns);
150152
}
151153

152154
throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));

0 commit comments

Comments
 (0)