Skip to content

Add Symfony 4 support #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ff02e83
Add Symfony 4 support
bocharsky-bw Dec 12, 2017
24882e4
Use ChildDefinition instead of DefinitionDecorator
bocharsky-bw Dec 13, 2017
a16f916
Make some services public and fix deprecated class calls
bocharsky-bw Dec 13, 2017
6fb3429
Fix more tests
bocharsky-bw Dec 13, 2017
1c4e527
Fix remaining tests: Make more services public
bocharsky-bw Dec 16, 2017
75dc718
Merge branch 'master' of github.com:php-translation/symfony-bundle in…
bocharsky-bw Dec 17, 2017
72a3306
Merge branch 'master' into patch-1
Nyholm Dec 20, 2017
7391edc
Call write() when exists instead of deprecated writeTranslations()
bocharsky-bw Dec 21, 2017
e114a06
Make php_translation.cache_clearer service public - we call it in con…
bocharsky-bw Dec 21, 2017
4844b40
Revert php-translation/symfony-storage version in composer.json
bocharsky-bw Dec 21, 2017
5887a83
Revert making php_translation.extractor.php public - use public alias
bocharsky-bw Dec 21, 2017
23762ba
Revert making php_translation.extractor.twig public - use public alias
bocharsky-bw Dec 21, 2017
cfa14a1
Revert making php_translation.extractor public - use public alias
bocharsky-bw Dec 21, 2017
4f23256
Revert making one more service public in favor of public alias
bocharsky-bw Dec 21, 2017
2931222
* Translation Loader or Reader (BC SF3.4)
dkozickis Dec 24, 2017
a54d3d6
* StyleCI
dkozickis Dec 25, 2017
d54ee4f
* Legacy wrapper for ChildDefiniton/DefinitionDecorator
dkozickis Dec 25, 2017
2d74ebc
* StyleCI fix
dkozickis Dec 25, 2017
06fc223
* One more fix for translation loader or reader
dkozickis Dec 25, 2017
9594986
Merge pull request #1 from dkozickis/patch-1
bocharsky-bw Dec 25, 2017
f40e62e
Tweaked service name to translation.loader_or_reader
bocharsky-bw Dec 25, 2017
f14ee83
Bump php-translation/symfony-storage version to ^0.3.4
bocharsky-bw Dec 25, 2017
271672f
Fix styles: Add missing header for new PHP classes
bocharsky-bw Dec 25, 2017
4469cfe
Create LegacyTranslationLoader on compilation to avoid extra checks
bocharsky-bw Dec 25, 2017
97671a8
Bump symfony-storage version to v0.4.0
bocharsky-bw Dec 28, 2017
d922523
Move test.* services to a separate file and include it for tests
bocharsky-bw Dec 28, 2017
5b0458e
Add a description for LoaderOrReaderPass class
bocharsky-bw Dec 28, 2017
66bd35e
Use new LegacyTranslationReader and LegacyTranslationWriter classes
bocharsky-bw Dec 28, 2017
b0dddbb
Make php_translation.importer public and remove its test alias
bocharsky-bw Dec 28, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions Catalogue/CatalogueFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Translation\Bundle\Catalogue;

use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
use Translation\Bundle\Model\Configuration;
use Translation\SymfonyStorage\LegacyTranslationReader;
use Translation\SymfonyStorage\TranslationLoader;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This interface does not exits anymore.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? Because I see it in master: https://github.com/php-translation/symfony-storage/blob/master/src/TranslationLoader.php and it is not even deprecated.


/**
* Fetches catalogues from source files. This will only work with local file storage
Expand All @@ -26,16 +29,20 @@
final class CatalogueFetcher
{
/**
* @var TranslationLoader
* @var TranslationReaderInterface
*/
private $loader;
private $reader;

/**
* @param TranslationLoader $loader
* @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader
*/
public function __construct(TranslationLoader $loader)
public function __construct($reader)
{
$this->loader = $loader;
if (!$reader instanceof TranslationReaderInterface) {
$reader = new LegacyTranslationReader($reader);
}

$this->reader = $reader;
}

/**
Expand All @@ -57,7 +64,7 @@ public function getCatalogues(Configuration $config, array $locales = [])
$currentCatalogue = new MessageCatalogue($locale);
foreach ($dirs as $path) {
if (is_dir($path)) {
$this->loader->loadMessages($path, $currentCatalogue);
$this->reader->read($path, $currentCatalogue);
}
}
$catalogues[] = $currentCatalogue;
Expand Down
16 changes: 10 additions & 6 deletions Catalogue/CatalogueWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
use Translation\Bundle\Model\Configuration;
use Translation\SymfonyStorage\LegacyTranslationWriter;

/**
* Write catalogues back to disk.
Expand All @@ -25,7 +27,7 @@
final class CatalogueWriter
{
/**
* @var TranslationWriter
* @var TranslationWriterInterface
*/
private $writer;

Expand All @@ -38,10 +40,12 @@ final class CatalogueWriter
* @param TranslationWriter $writer
* @param string $defaultLocale
*/
public function __construct(
TranslationWriter $writer,
$defaultLocale
) {
public function __construct(TranslationWriter $writer, $defaultLocale)
{
if (!$writer instanceof TranslationWriterInterface) {
$writer = new LegacyTranslationWriter($writer);
}

$this->writer = $writer;
$this->defaultLocale = $defaultLocale;
}
Expand All @@ -53,7 +57,7 @@ public function __construct(
public function writeCatalogues(Configuration $config, array $catalogues)
{
foreach ($catalogues as $catalogue) {
$this->writer->writeTranslations(
$this->writer->write(
$catalogue,
$config->getOutputFormat(),
[
Expand Down
38 changes: 38 additions & 0 deletions DependencyInjection/CompilerPass/LoaderOrReaderPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you write a class comment that explains the purpose of this CompilerPass?

/**
* To provide a BC layer for Symfony 2.7 to 3.3 this compiler pass
* registers an alias of whether TranslationReader or TranslationLoader
* to be able to inject it in other services.
*/
class LoaderOrReaderPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->has('translation.reader')) {
$container->setAlias('translation.loader_or_reader', 'translation.reader');

return;
}

if ($container->has('translation.loader')) {
$container->setAlias('translation.loader_or_reader', 'translation.loader');

return;
}
}
}
29 changes: 26 additions & 3 deletions DependencyInjection/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Translation\Bundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -74,6 +76,10 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('auto_translation.yml');
$this->enableFallbackAutoTranslator($container, $config);
}

if ('test' === getenv('ENV')) {
$loader->load('services_test.yml');
}
}

/**
Expand Down Expand Up @@ -105,8 +111,9 @@ private function handleConfigNode(ContainerBuilder $container, array $config)
/*
* Configure storage chain service
*/
$storageDefinition = new DefinitionDecorator('php_translation.storage.abstract');
$storageDefinition = $this->createChildDefinition('php_translation.storage.abstract');
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
$storageDefinition->setPublic(true);
$container->setDefinition('php_translation.storage.'.$name, $storageDefinition);

// Add storages
Expand All @@ -121,7 +128,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config)
continue;
}

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

if (null !== $first) {
// Create some aliases for the default storage
$container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
$container->setAlias('php_translation.storage', new Alias('php_translation.storage.'.$first, true));
if ('default' !== $first) {
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
}
Expand Down Expand Up @@ -222,4 +229,20 @@ public function getAlias()
{
return 'translation';
}

/**
* To avoid BC break for Symfony 3.3+.
*
* @param $parent
*
* @return ChildDefinition|DefinitionDecorator
*/
private function createChildDefinition($parent)
{
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) {
return new ChildDefinition($parent);
}

return new DefinitionDecorator($parent);
}
}
11 changes: 9 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
services:
php_translation.catalogue_fetcher:
public: true
class: Translation\Bundle\Catalogue\CatalogueFetcher
arguments: ["@translation.loader"]
arguments: ["@translation.loader_or_reader"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks to @dkozickis for this idea and his help


php_translation.catalogue_writer:
public: true
class: Translation\Bundle\Catalogue\CatalogueWriter
arguments: ["@translation.writer", "%php_translation.default_locale%"]

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

php_translation.catalogue_manager:
public: true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the services need to be public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we call it from the container in WebUIController::showAction()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can inject it into controller instead, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should But that is out of scope for this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, it won't work in 2.7, will it?

class: Translation\Bundle\Catalogue\CatalogueManager

php_translation.extractor:
class: Translation\Extractor\Extractor

php_translation.configuration_manager:
public: true
class: Translation\Bundle\Service\ConfigurationManager

php_translation.importer:
public: true
class: Translation\Bundle\Service\Importer
arguments: ["@php_translation.extractor"]

php_translation.cache_clearer:
public: true
class: Translation\Bundle\Service\CacheClearer
arguments: ["%kernel.cache_dir%", "@translator", "@filesystem"]

php_translation.local_file_storage.abstract:
class: Translation\SymfonyStorage\FileStorage
abstract: true
arguments: ["@translation.writer", "@translation.loader", ~, []]
arguments: ["@translation.writer", "@translation.loader_or_reader", ~, []]

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

php_translation.catalogue_counter:
public: true
class: Translation\Bundle\Catalogue\CatalogueCounter
arguments: []
16 changes: 16 additions & 0 deletions Resources/config/services_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
test.php_translation.edit_in_place.activator:
public: true
alias: 'php_translation.edit_in_place.activator'

test.php_translation.extractor.php:
public: true
alias: 'php_translation.extractor.php'

test.php_translation.extractor.twig:
public: true
alias: 'php_translation.extractor.twig'

test.php_translation.extractor:
public: true
alias: 'php_translation.extractor'
6 changes: 3 additions & 3 deletions Tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function testRegisterBundle()

$services = [
'php_translation.storage' => StorageService::class,
'php_translation.extractor.twig' => TwigFileExtractor::class,
'php_translation.extractor.php' => PHPFileExtractor::class,
'test.php_translation.extractor.twig' => TwigFileExtractor::class,
'test.php_translation.extractor.php' => PHPFileExtractor::class,
'php_translation.catalogue_fetcher' => CatalogueFetcher::class,
'php_translation.catalogue_writer' => CatalogueWriter::class,
'php_translation.catalogue_manager' => CatalogueManager::class,
'php_translation.extractor' => Extractor::class,
'test.php_translation.extractor' => Extractor::class,
];

foreach ($services as $id => $class) {
Expand Down
97 changes: 97 additions & 0 deletions Tests/Functional/Catalogue/CatalogueFetcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Component\Translation\MessageCatalogue;
use Translation\Bundle\Catalogue\CatalogueFetcher;
use Translation\Bundle\Model\Configuration;
use Translation\Bundle\Tests\Functional\BaseTestCase;

class CatalogueFetcherTest extends BaseTestCase
{
/**
* @var CatalogueFetcher
*/
private $catalogueFetcher;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

file_put_contents(
__DIR__.'/../app/Resources/translations/messages.sv.xlf',
<<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US">
<file id="messages.en_US">
<unit id="LCa0a2j">
<segment>
<source>key0</source>
<target>trans0</target>
</segment>
</unit>
<unit id="LCa0a2b">
<segment>
<source>key1</source>
<target>trans1</target>
</segment>
</unit>
</file>
</xliff>

XML
);
}

public function testFetchCatalogue()
{
$this->bootKernel();

$this->catalogueFetcher = $this->getContainer()->get('php_translation.catalogue_fetcher');

$data = self::getDefaultData();
$data['external_translations_dirs'] = [__DIR__.'/../app/Resources/translations/'];

$conf = new Configuration($data);

/** @var MessageCatalogue[] $catalogues */
$catalogues = $this->catalogueFetcher->getCatalogues($conf, ['sv']);

$this->assertEquals('sv', $catalogues[0]->getLocale());
}

/**
* @return array
*/
public static function getDefaultData()
{
return [
'name' => 'getName',
'locales' => ['getLocales'],
'project_root' => 'getProjectRoot',
'output_dir' => 'getOutputDir',
'dirs' => ['getDirs'],
'excluded_dirs' => ['getExcludedDirs'],
'excluded_names' => ['getExcludedNames'],
'external_translations_dirs' => ['getExternalTranslationsDirs'],
'output_format' => 'getOutputFormat',
'blacklist_domains' => ['getBlacklistDomains'],
'whitelist_domains' => ['getWhitelistDomains'],
'xliff_version' => ['getXliffVersion'],
];
}

protected function setUp()
{
parent::setUp();

$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml');
}
}
Loading