Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a524658

Browse filesBrowse files
rpkampfabpot
authored andcommitted
Improve Translator caching
1 parent 760bbd5 commit a524658
Copy full SHA for a524658

File tree

5 files changed

+89
-8
lines changed
Filter options

5 files changed

+89
-8
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
10751075
// Discover translation directories
10761076
$dirs = [];
10771077
$transPaths = [];
1078+
$nonExistingDirs = [];
10781079
if (class_exists('Symfony\Component\Validator\Validation')) {
10791080
$r = new \ReflectionClass('Symfony\Component\Validator\Validation');
10801081

@@ -1093,18 +1094,21 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
10931094
$defaultDir = $container->getParameterBag()->resolveValue($config['default_path']);
10941095
$rootDir = $container->getParameter('kernel.root_dir');
10951096
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
1096-
if ($container->fileExists($dir = $bundle['path'].'/Resources/translations')) {
1097+
if (\is_dir($dir = $bundle['path'].'/Resources/translations')) {
10971098
$dirs[] = $dir;
1099+
} else {
1100+
$nonExistingDirs[] = $dir;
10981101
}
1099-
if ($container->fileExists($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
1102+
if (\is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
11001103
@trigger_error(sprintf('Translations directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultDir), E_USER_DEPRECATED);
1101-
11021104
$dirs[] = $dir;
1105+
} else {
1106+
$nonExistingDirs[] = $dir;
11031107
}
11041108
}
11051109

11061110
foreach ($config['paths'] as $dir) {
1107-
if ($container->fileExists($dir)) {
1111+
if (\is_dir($dir)) {
11081112
$dirs[] = $transPaths[] = $dir;
11091113
} else {
11101114
throw new \UnexpectedValueException(sprintf('%s defined in translator.paths does not exist or is not a directory', $dir));
@@ -1119,15 +1123,20 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11191123
$container->getDefinition('console.command.translation_update')->replaceArgument(6, $transPaths);
11201124
}
11211125

1122-
if ($container->fileExists($defaultDir)) {
1126+
if (\is_dir($defaultDir)) {
11231127
$dirs[] = $defaultDir;
1128+
} else {
1129+
$nonExistingDirs[] = $defaultDir;
11241130
}
1125-
if ($container->fileExists($dir = $rootDir.'/Resources/translations')) {
1131+
1132+
if (\is_dir($dir = $rootDir.'/Resources/translations')) {
11261133
if ($dir !== $defaultDir) {
11271134
@trigger_error(sprintf('Translations directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultDir), E_USER_DEPRECATED);
11281135
}
11291136

11301137
$dirs[] = $dir;
1138+
} else {
1139+
$nonExistingDirs[] = $dir;
11311140
}
11321141

11331142
// Register translation resources
@@ -1154,7 +1163,10 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
11541163

11551164
$options = array_merge(
11561165
$translator->getArgument(4),
1157-
['resource_files' => $files]
1166+
[
1167+
'resource_files' => $files,
1168+
'scanned_directories' => \array_merge($dirs, $nonExistingDirs),
1169+
]
11581170
);
11591171

11601172
$translator->replaceArgument(4, $options);

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2626
use Symfony\Component\Cache\Adapter\ProxyAdapter;
2727
use Symfony\Component\Cache\Adapter\RedisAdapter;
28+
use Symfony\Component\Config\Resource\DirectoryResource;
29+
use Symfony\Component\Config\Resource\FileExistenceResource;
2830
use Symfony\Component\DependencyInjection\ChildDefinition;
2931
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
3032
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
@@ -800,6 +802,26 @@ public function testTranslator()
800802

801803
$calls = $container->getDefinition('translator.default')->getMethodCalls();
802804
$this->assertEquals(['fr'], $calls[1][1][0]);
805+
806+
$nonExistingDirectories = array_filter(
807+
$options['scanned_directories'],
808+
function ($directory) {
809+
return !file_exists($directory);
810+
}
811+
);
812+
813+
$this->assertNotEmpty($nonExistingDirectories, 'FrameworkBundle should pass non existing directories to Translator');
814+
815+
$resources = $container->getResources();
816+
foreach ($resources as $resource) {
817+
if ($resource instanceof DirectoryResource) {
818+
$this->assertNotContains('translations', $resource->getResource());
819+
}
820+
821+
if ($resource instanceof FileExistenceResource) {
822+
$this->assertNotContains('translations', $resource->getResource());
823+
}
824+
}
803825
}
804826

805827
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Translation/TranslatorTest.php
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Container\ContainerInterface;
1616
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
17+
use Symfony\Component\Config\Resource\DirectoryResource;
18+
use Symfony\Component\Config\Resource\FileExistenceResource;
1719
use Symfony\Component\Filesystem\Filesystem;
1820
use Symfony\Component\Translation\Formatter\MessageFormatter;
1921
use Symfony\Component\Translation\MessageCatalogue;
@@ -223,6 +225,29 @@ public function getDebugModeAndCacheDirCombinations()
223225
];
224226
}
225227

228+
public function testCatalogResourcesAreAddedForScannedDirectories()
229+
{
230+
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
231+
$resourceFiles = [
232+
'fr' => [
233+
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
234+
],
235+
];
236+
237+
/** @var Translator $translator */
238+
$translator = $this->getTranslator($loader, [
239+
'resource_files' => $resourceFiles,
240+
'scanned_directories' => [__DIR__, '/tmp/I/sure/hope/this/does/not/exist'],
241+
], 'yml');
242+
243+
$catalogue = $translator->getCatalogue('fr');
244+
245+
$resources = $catalogue->getResources();
246+
247+
$this->assertEquals(new DirectoryResource(__DIR__), $resources[1]);
248+
$this->assertEquals(new FileExistenceResource('/tmp/I/sure/hope/this/does/not/exist'), $resources[2]);
249+
}
250+
226251
protected function getCatalogue($locale, $messages, $resources = [])
227252
{
228253
$catalogue = new MessageCatalogue($locale);

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Translation;
1313

1414
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\Config\Resource\DirectoryResource;
16+
use Symfony\Component\Config\Resource\FileExistenceResource;
1517
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1618
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1719
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
@@ -31,6 +33,7 @@ class Translator extends BaseTranslator implements WarmableInterface
3133
'cache_dir' => null,
3234
'debug' => false,
3335
'resource_files' => [],
36+
'scanned_directories' => [],
3437
];
3538

3639
/**
@@ -48,6 +51,11 @@ class Translator extends BaseTranslator implements WarmableInterface
4851

4952
private $resourceFiles;
5053

54+
/**
55+
* @var string[]
56+
*/
57+
private $scannedDirectories;
58+
5159
/**
5260
* Constructor.
5361
*
@@ -78,6 +86,7 @@ public function __construct(ContainerInterface $container, MessageFormatterInter
7886
$this->options = array_merge($this->options, $options);
7987
$this->resourceLocales = array_keys($this->options['resource_files']);
8088
$this->resourceFiles = $this->options['resource_files'];
89+
$this->scannedDirectories = $this->options['scanned_directories'];
8190

8291
parent::__construct($defaultLocale, $formatter, $this->options['cache_dir'], $this->options['debug']);
8392
}
@@ -120,6 +129,16 @@ protected function initializeCatalogue($locale)
120129
parent::initializeCatalogue($locale);
121130
}
122131

132+
protected function doLoadCatalogue($locale): void
133+
{
134+
parent::doLoadCatalogue($locale);
135+
136+
foreach ($this->scannedDirectories as $directory) {
137+
$resourceClass = file_exists($directory) ? DirectoryResource::class : FileExistenceResource::class;
138+
$this->catalogues[$locale]->addResource(new $resourceClass($directory));
139+
}
140+
}
141+
123142
protected function initialize()
124143
{
125144
if ($this->resourceFiles) {

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,10 @@ private function getCatalogueCachePath($locale)
395395
return $this->cacheDir.'/catalogue.'.$locale.'.'.strtr(substr(base64_encode(hash('sha256', serialize($this->fallbackLocales), true)), 0, 7), '/', '_').'.php';
396396
}
397397

398-
private function doLoadCatalogue($locale): void
398+
/**
399+
* @internal
400+
*/
401+
protected function doLoadCatalogue($locale): void
399402
{
400403
$this->catalogues[$locale] = new MessageCatalogue($locale);
401404

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.