feature #32543 [FrameworkBundle] add config translator cache_dir (Raulnet)

This PR was submitted for the 4.3 branch but it was squashed and merged into the 4.4 branch instead (closes #32543).

Discussion
----------

[FrameworkBundle] add config translator cache_dir

| Q             | A
| ------------- | ---
| Branch?       |  4.4 (be careful when merging)
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #32542
| License       | MIT

Now the parameter cache_dir of Translator constructor can be change or disable on config

```yaml
#framework.yaml
framework:
    ...
    translator:
        cache_dir: null #(cache disable)
    ...
```

Commits
-------

7613c7d1de [FrameworkBundle] add config translator cache_dir
This commit is contained in:
Fabien Potencier 2019-08-04 04:36:15 +02:00
commit 35bf2fa9b5
11 changed files with 43 additions and 2 deletions

View File

@ -762,6 +762,7 @@ class Configuration implements ConfigurationInterface
->end()
->booleanNode('logging')->defaultValue(false)->end()
->scalarNode('formatter')->defaultValue('translator.formatter.default')->end()
->scalarNode('cache_dir')->defaultValue('%kernel.cache_dir%/translations')->end()
->scalarNode('default_path')
->info('The default path used to load translations')
->defaultValue('%kernel.project_dir%/translations')

View File

@ -1118,6 +1118,10 @@ class FrameworkExtension extends Extension
$translator = $container->findDefinition('translator.default');
$translator->addMethodCall('setFallbackLocales', [$config['fallbacks'] ?: [$defaultLocale]]);
$defaultOptions = $translator->getArgument(4);
$defaultOptions['cache_dir'] = $config['cache_dir'];
$translator->setArgument(4, $defaultOptions);
$container->setParameter('translator.logging', $config['logging']);
$container->setParameter('translator.default_path', $config['default_path']);

View File

@ -186,6 +186,7 @@
<xsd:attribute name="fallback" type="xsd:string" />
<xsd:attribute name="logging" type="xsd:boolean" />
<xsd:attribute name="formatter" type="xsd:string" />
<xsd:attribute name="cache-dir" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="validation">

View File

@ -241,6 +241,7 @@ class ConfigurationTest extends TestCase
'translator' => [
'enabled' => !class_exists(FullStack::class),
'fallbacks' => [],
'cache_dir' => '%kernel.cache_dir%/translations',
'logging' => false,
'formatter' => 'translator.formatter.default',
'paths' => [],

View File

@ -46,6 +46,7 @@ $container->loadFromExtension('framework', [
'enabled' => true,
'fallback' => 'fr',
'paths' => ['%kernel.project_dir%/Fixtures/translations'],
'cache_dir' => '%kernel.cache_dir%/translations',
],
'validation' => [
'enabled' => true,

View File

@ -0,0 +1,7 @@
<?php
$container->loadFromExtension('framework', [
'translator' => [
'cache_dir' => null,
],
]);

View File

@ -26,7 +26,7 @@
</framework:format>
</framework:request>
<framework:assets version="v1" />
<framework:translator enabled="true" fallback="fr" logging="true">
<framework:translator enabled="true" fallback="fr" logging="true" cache-dir="%kernel.cache_dir%/translations">
<framework:path>%kernel.project_dir%/Fixtures/translations</framework:path>
</framework:translator>
<framework:validation enabled="true" />

View File

@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config secret="s3cr3t">
<framework:translator enabled="true" fallback="fr" logging="true" cache-dir="null" />
</framework:config>
</container>

View File

@ -36,6 +36,7 @@ framework:
enabled: true
fallback: fr
default_path: '%kernel.project_dir%/translations'
cache_dir: '%kernel.cache_dir%/translations'
paths: ['%kernel.project_dir%/Fixtures/translations']
validation:
enabled: true

View File

@ -0,0 +1,3 @@
framework:
translator:
cache_dir: ~

View File

@ -51,11 +51,11 @@ use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Translation\DependencyInjection\TranslatorPass;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
use Symfony\Component\Workflow;
use Symfony\Contracts\Translation\TranslatorInterface;
abstract class FrameworkExtensionTest extends TestCase
{
@ -783,6 +783,9 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
$options = $container->getDefinition('translator.default')->getArgument(4);
$this->assertArrayHasKey('cache_dir', $options);
$this->assertSame($container->getParameter('kernel.cache_dir').'/translations', $options['cache_dir']);
$files = array_map('realpath', $options['resource_files']['en']);
$ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
$this->assertContains(
@ -853,6 +856,13 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertEquals(['en', 'fr'], $calls[1][1][0]);
}
public function testTranslatorCacheDirDisabled()
{
$container = $this->createContainerFromFile('translator_cache_dir_disabled');
$options = $container->getDefinition('translator.default')->getArgument(4);
$this->assertNull($options['cache_dir']);
}
/**
* @group legacy
*/