Skip to content

Commit e6b000b

Browse files
bocharsky-bwNyholm
authored andcommitted
Add Symfony 4 support (#145)
* Add Symfony 4 support * Use ChildDefinition instead of DefinitionDecorator * Make some services public and fix deprecated class calls * Fix more tests * Fix remaining tests: Make more services public * Call write() when exists instead of deprecated writeTranslations() * Make php_translation.cache_clearer service public - we call it in controller * Revert php-translation/symfony-storage version in composer.json * Revert making php_translation.extractor.php public - use public alias * Revert making php_translation.extractor.twig public - use public alias * Revert making php_translation.extractor public - use public alias * Revert making one more service public in favor of public alias * * Translation Loader or Reader (BC SF3.4) * * StyleCI * * Legacy wrapper for ChildDefiniton/DefinitionDecorator * * StyleCI fix * * One more fix for translation loader or reader * Tweaked service name to translation.loader_or_reader * Bump php-translation/symfony-storage version to ^0.3.4 * Fix styles: Add missing header for new PHP classes * Create LegacyTranslationLoader on compilation to avoid extra checks * Bump symfony-storage version to v0.4.0 * Move test.* services to a separate file and include it for tests * Add a description for LoaderOrReaderPass class * Use new LegacyTranslationReader and LegacyTranslationWriter classes * Make php_translation.importer public and remove its test alias
1 parent 7858771 commit e6b000b

File tree

13 files changed

+269
-36
lines changed

13 files changed

+269
-36
lines changed

Catalogue/CatalogueFetcher.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespace Translation\Bundle\Catalogue;
1313

14-
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
14+
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader;
1515
use Symfony\Component\Translation\MessageCatalogue;
16+
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
1617
use Translation\Bundle\Model\Configuration;
18+
use Translation\SymfonyStorage\LegacyTranslationReader;
19+
use Translation\SymfonyStorage\TranslationLoader;
1720

1821
/**
1922
* Fetches catalogues from source files. This will only work with local file storage
@@ -26,16 +29,20 @@
2629
final class CatalogueFetcher
2730
{
2831
/**
29-
* @var TranslationLoader
32+
* @var TranslationReaderInterface
3033
*/
31-
private $loader;
34+
private $reader;
3235

3336
/**
34-
* @param TranslationLoader $loader
37+
* @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader
3538
*/
36-
public function __construct(TranslationLoader $loader)
39+
public function __construct($reader)
3740
{
38-
$this->loader = $loader;
41+
if (!$reader instanceof TranslationReaderInterface) {
42+
$reader = new LegacyTranslationReader($reader);
43+
}
44+
45+
$this->reader = $reader;
3946
}
4047

4148
/**
@@ -57,7 +64,7 @@ public function getCatalogues(Configuration $config, array $locales = [])
5764
$currentCatalogue = new MessageCatalogue($locale);
5865
foreach ($dirs as $path) {
5966
if (is_dir($path)) {
60-
$this->loader->loadMessages($path, $currentCatalogue);
67+
$this->reader->read($path, $currentCatalogue);
6168
}
6269
}
6370
$catalogues[] = $currentCatalogue;

Catalogue/CatalogueWriter.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
use Symfony\Component\Translation\MessageCatalogue;
1515
use Symfony\Component\Translation\Writer\TranslationWriter;
16+
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
1617
use Translation\Bundle\Model\Configuration;
18+
use Translation\SymfonyStorage\LegacyTranslationWriter;
1719

1820
/**
1921
* Write catalogues back to disk.
@@ -25,7 +27,7 @@
2527
final class CatalogueWriter
2628
{
2729
/**
28-
* @var TranslationWriter
30+
* @var TranslationWriterInterface
2931
*/
3032
private $writer;
3133

@@ -38,10 +40,12 @@ final class CatalogueWriter
3840
* @param TranslationWriter $writer
3941
* @param string $defaultLocale
4042
*/
41-
public function __construct(
42-
TranslationWriter $writer,
43-
$defaultLocale
44-
) {
43+
public function __construct(TranslationWriter $writer, $defaultLocale)
44+
{
45+
if (!$writer instanceof TranslationWriterInterface) {
46+
$writer = new LegacyTranslationWriter($writer);
47+
}
48+
4549
$this->writer = $writer;
4650
$this->defaultLocale = $defaultLocale;
4751
}
@@ -53,7 +57,7 @@ public function __construct(
5357
public function writeCatalogues(Configuration $config, array $catalogues)
5458
{
5559
foreach ($catalogues as $catalogue) {
56-
$this->writer->writeTranslations(
60+
$this->writer->write(
5761
$catalogue,
5862
$config->getOutputFormat(),
5963
[
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\DependencyInjection\CompilerPass;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* To provide a BC layer for Symfony 2.7 to 3.3 this compiler pass
19+
* registers an alias of whether TranslationReader or TranslationLoader
20+
* to be able to inject it in other services.
21+
*/
22+
class LoaderOrReaderPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container)
25+
{
26+
if ($container->has('translation.reader')) {
27+
$container->setAlias('translation.loader_or_reader', 'translation.reader');
28+
29+
return;
30+
}
31+
32+
if ($container->has('translation.loader')) {
33+
$container->setAlias('translation.loader_or_reader', 'translation.loader');
34+
35+
return;
36+
}
37+
}
38+
}

DependencyInjection/TranslationExtension.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Translation\Bundle\DependencyInjection;
1313

14+
use Symfony\Component\DependencyInjection\Alias;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\Config\FileLocator;
17+
use Symfony\Component\DependencyInjection\ChildDefinition;
1618
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1719
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1820
use Symfony\Component\DependencyInjection\Reference;
@@ -74,6 +76,10 @@ public function load(array $configs, ContainerBuilder $container)
7476
$loader->load('auto_translation.yml');
7577
$this->enableFallbackAutoTranslator($container, $config);
7678
}
79+
80+
if ('test' === getenv('ENV')) {
81+
$loader->load('services_test.yml');
82+
}
7783
}
7884

7985
/**
@@ -105,8 +111,9 @@ private function handleConfigNode(ContainerBuilder $container, array $config)
105111
/*
106112
* Configure storage chain service
107113
*/
108-
$storageDefinition = new DefinitionDecorator('php_translation.storage.abstract');
114+
$storageDefinition = $this->createChildDefinition('php_translation.storage.abstract');
109115
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
116+
$storageDefinition->setPublic(true);
110117
$container->setDefinition('php_translation.storage.'.$name, $storageDefinition);
111118

112119
// Add storages
@@ -121,7 +128,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config)
121128
continue;
122129
}
123130

124-
$def = new DefinitionDecorator($serviceId);
131+
$def = $this->createChildDefinition($serviceId);
125132
$def->replaceArgument(2, [$c['output_dir']])
126133
->replaceArgument(3, [$c['local_file_storage_options']])
127134
->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]);
@@ -131,7 +138,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config)
131138

132139
if (null !== $first) {
133140
// Create some aliases for the default storage
134-
$container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
141+
$container->setAlias('php_translation.storage', new Alias('php_translation.storage.'.$first, true));
135142
if ('default' !== $first) {
136143
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
137144
}
@@ -222,4 +229,20 @@ public function getAlias()
222229
{
223230
return 'translation';
224231
}
232+
233+
/**
234+
* To avoid BC break for Symfony 3.3+.
235+
*
236+
* @param $parent
237+
*
238+
* @return ChildDefinition|DefinitionDecorator
239+
*/
240+
private function createChildDefinition($parent)
241+
{
242+
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
243+
return new ChildDefinition($parent);
244+
}
245+
246+
return new DefinitionDecorator($parent);
247+
}
225248
}

Resources/config/services.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
services:
22
php_translation.catalogue_fetcher:
3+
public: true
34
class: Translation\Bundle\Catalogue\CatalogueFetcher
4-
arguments: ["@translation.loader"]
5+
arguments: ["@translation.loader_or_reader"]
56

67
php_translation.catalogue_writer:
8+
public: true
79
class: Translation\Bundle\Catalogue\CatalogueWriter
810
arguments: ["@translation.writer", "%php_translation.default_locale%"]
911

@@ -13,26 +15,30 @@ services:
1315
arguments: ["@php_translation.catalogue_fetcher", "@php_translation.catalogue_writer", ~]
1416

1517
php_translation.catalogue_manager:
18+
public: true
1619
class: Translation\Bundle\Catalogue\CatalogueManager
1720

1821
php_translation.extractor:
1922
class: Translation\Extractor\Extractor
2023

2124
php_translation.configuration_manager:
25+
public: true
2226
class: Translation\Bundle\Service\ConfigurationManager
2327

2428
php_translation.importer:
29+
public: true
2530
class: Translation\Bundle\Service\Importer
2631
arguments: ["@php_translation.extractor"]
2732

2833
php_translation.cache_clearer:
34+
public: true
2935
class: Translation\Bundle\Service\CacheClearer
3036
arguments: ["%kernel.cache_dir%", "@translator", "@filesystem"]
3137

3238
php_translation.local_file_storage.abstract:
3339
class: Translation\SymfonyStorage\FileStorage
3440
abstract: true
35-
arguments: ["@translation.writer", "@translation.loader", ~, []]
41+
arguments: ["@translation.writer", "@translation.loader_or_reader", ~, []]
3642

3743
php_translation.storage.xlf_loader:
3844
class: Translation\SymfonyStorage\Loader\XliffLoader
@@ -45,5 +51,6 @@ services:
4551
- { name: translation.dumper, alias: xlf, legacy-alias: xliff }
4652

4753
php_translation.catalogue_counter:
54+
public: true
4855
class: Translation\Bundle\Catalogue\CatalogueCounter
4956
arguments: []

Resources/config/services_test.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
test.php_translation.edit_in_place.activator:
3+
public: true
4+
alias: 'php_translation.edit_in_place.activator'
5+
6+
test.php_translation.extractor.php:
7+
public: true
8+
alias: 'php_translation.extractor.php'
9+
10+
test.php_translation.extractor.twig:
11+
public: true
12+
alias: 'php_translation.extractor.twig'
13+
14+
test.php_translation.extractor:
15+
public: true
16+
alias: 'php_translation.extractor'

Tests/Functional/BundleInitializationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public function testRegisterBundle()
3636

3737
$services = [
3838
'php_translation.storage' => StorageService::class,
39-
'php_translation.extractor.twig' => TwigFileExtractor::class,
40-
'php_translation.extractor.php' => PHPFileExtractor::class,
39+
'test.php_translation.extractor.twig' => TwigFileExtractor::class,
40+
'test.php_translation.extractor.php' => PHPFileExtractor::class,
4141
'php_translation.catalogue_fetcher' => CatalogueFetcher::class,
4242
'php_translation.catalogue_writer' => CatalogueWriter::class,
4343
'php_translation.catalogue_manager' => CatalogueManager::class,
44-
'php_translation.extractor' => Extractor::class,
44+
'test.php_translation.extractor' => Extractor::class,
4545
];
4646

4747
foreach ($services as $id => $class) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Component\Translation\MessageCatalogue;
13+
use Translation\Bundle\Catalogue\CatalogueFetcher;
14+
use Translation\Bundle\Model\Configuration;
15+
use Translation\Bundle\Tests\Functional\BaseTestCase;
16+
17+
class CatalogueFetcherTest extends BaseTestCase
18+
{
19+
/**
20+
* @var CatalogueFetcher
21+
*/
22+
private $catalogueFetcher;
23+
24+
public static function setUpBeforeClass()
25+
{
26+
parent::setUpBeforeClass();
27+
28+
file_put_contents(
29+
__DIR__.'/../app/Resources/translations/messages.sv.xlf',
30+
<<<'XML'
31+
<?xml version="1.0" encoding="utf-8"?>
32+
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
33+
<file id="messages.en_US">
34+
<unit id="LCa0a2j">
35+
<segment>
36+
<source>key0</source>
37+
<target>trans0</target>
38+
</segment>
39+
</unit>
40+
<unit id="LCa0a2b">
41+
<segment>
42+
<source>key1</source>
43+
<target>trans1</target>
44+
</segment>
45+
</unit>
46+
</file>
47+
</xliff>
48+
49+
XML
50+
);
51+
}
52+
53+
public function testFetchCatalogue()
54+
{
55+
$this->bootKernel();
56+
57+
$this->catalogueFetcher = $this->getContainer()->get('php_translation.catalogue_fetcher');
58+
59+
$data = self::getDefaultData();
60+
$data['external_translations_dirs'] = [__DIR__.'/../app/Resources/translations/'];
61+
62+
$conf = new Configuration($data);
63+
64+
/** @var MessageCatalogue[] $catalogues */
65+
$catalogues = $this->catalogueFetcher->getCatalogues($conf, ['sv']);
66+
67+
$this->assertEquals('sv', $catalogues[0]->getLocale());
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public static function getDefaultData()
74+
{
75+
return [
76+
'name' => 'getName',
77+
'locales' => ['getLocales'],
78+
'project_root' => 'getProjectRoot',
79+
'output_dir' => 'getOutputDir',
80+
'dirs' => ['getDirs'],
81+
'excluded_dirs' => ['getExcludedDirs'],
82+
'excluded_names' => ['getExcludedNames'],
83+
'external_translations_dirs' => ['getExternalTranslationsDirs'],
84+
'output_format' => 'getOutputFormat',
85+
'blacklist_domains' => ['getBlacklistDomains'],
86+
'whitelist_domains' => ['getWhitelistDomains'],
87+
'xliff_version' => ['getXliffVersion'],
88+
];
89+
}
90+
91+
protected function setUp()
92+
{
93+
parent::setUp();
94+
95+
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');
96+
}
97+
}

0 commit comments

Comments
 (0)