Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b237ce

Browse files
committed
bug #34397 [FrameworkBundle] Remove project dir from Translator cache vary scanned directories (fancyweb)
This PR was merged into the 4.3 branch. Discussion ---------- [FrameworkBundle] Remove project dir from Translator cache vary scanned directories | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Closes #34395 | License | MIT | Doc PR | - Weird cases such as having different paths for directories found through reflection (cf https://github.com/symfony/symfony/blob/8522a88185a7e283ad342b3539cd088d76968df9/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L1105) or using different values on warmup and run for another parameter than `kernel.project_dir` are still unconvered. Unfortunately there is nothing we can do, do we care? If yes, then we might just wanna enable #34129 when the `debug` option is on. Commits ------- e75e01d [FrameworkBundle] Remove project dir from Translator cache vary scanned directories
2 parents 1fbe615 + e75e01d commit 3b237ce

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,11 +1184,18 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11841184
$files[$locale][] = (string) $file;
11851185
}
11861186

1187+
$projectDir = $container->getParameter('kernel.project_dir');
1188+
11871189
$options = array_merge(
11881190
$translator->getArgument(4),
11891191
[
11901192
'resource_files' => $files,
1191-
'scanned_directories' => array_merge($dirs, $nonExistingDirs),
1193+
'scanned_directories' => $scannedDirectories = array_merge($dirs, $nonExistingDirs),
1194+
'cache_vary' => [
1195+
'scanned_directories' => array_map(static function (string $dir) use ($projectDir): string {
1196+
return 0 === strpos($dir, $projectDir.'/') ? substr($dir, 1 + \strlen($projectDir)) : $dir;
1197+
}, $scannedDirectories),
1198+
],
11921199
]
11931200
);
11941201

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ function ($directory) {
824824
);
825825

826826
$this->assertNotEmpty($nonExistingDirectories, 'FrameworkBundle should pass non existing directories to Translator');
827+
828+
$this->assertSame('Fixtures/translations', $options['cache_vary']['scanned_directories'][3]);
827829
}
828830

829831
/**

src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,10 @@ public function testCachedCatalogueIsReDumpedWhenScannedDirectoriesChange()
255255
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
256256
],
257257
],
258-
'scanned_directories' => [
259-
__DIR__.'/../Fixtures/Resources/translations/',
258+
'cache_vary' => [
259+
'scanned_directories' => [
260+
'/Fixtures/Resources/translations/',
261+
],
260262
],
261263
], 'yml');
262264

@@ -271,9 +273,11 @@ public function testCachedCatalogueIsReDumpedWhenScannedDirectoriesChange()
271273
__DIR__.'/../Fixtures/Resources/translations2/ccc.fr.yml',
272274
],
273275
],
274-
'scanned_directories' => [
275-
__DIR__.'/../Fixtures/Resources/translations/',
276-
__DIR__.'/../Fixtures/Resources/translations2/',
276+
'cache_vary' => [
277+
'scanned_directories' => [
278+
'/Fixtures/Resources/translations/',
279+
'/Fixtures/Resources/translations2/',
280+
],
277281
],
278282
], 'yml');
279283

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Translator extends BaseTranslator implements WarmableInterface
3434
'debug' => false,
3535
'resource_files' => [],
3636
'scanned_directories' => [],
37+
'cache_vary' => [],
3738
];
3839

3940
/**
@@ -61,9 +62,10 @@ class Translator extends BaseTranslator implements WarmableInterface
6162
*
6263
* Available options:
6364
*
64-
* * cache_dir: The cache directory (or null to disable caching)
65-
* * debug: Whether to enable debugging or not (false by default)
65+
* * cache_dir: The cache directory (or null to disable caching)
66+
* * debug: Whether to enable debugging or not (false by default)
6667
* * resource_files: List of translation resources available grouped by locale.
68+
* * cache_vary: An array of data that is serialized to generate the cached catalogue name.
6769
*
6870
* @throws InvalidArgumentException
6971
*/
@@ -82,9 +84,7 @@ public function __construct(ContainerInterface $container, MessageFormatterInter
8284
$this->resourceFiles = $this->options['resource_files'];
8385
$this->scannedDirectories = $this->options['scanned_directories'];
8486

85-
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug'], [
86-
'scanned_directories' => $this->scannedDirectories,
87-
]);
87+
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug'], $this->options['cache_vary']);
8888
}
8989

9090
/**

0 commit comments

Comments
 (0)