Skip to content

Commit

Permalink
Symfony 4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukashk0zzz committed Feb 15, 2018
1 parent 567134c commit 3a2b706
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 27 deletions.
27 changes: 20 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,42 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2

matrix:
fast_finish: true
allow_failures:
- php: 7.2
exclude:
- php: 5.6
env: SYMFONY_VERSION=4.0.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
- php: 7.0
env: SYMFONY_VERSION=4.0.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
- php: 7.2
env: SYMFONY_VERSION=2.8.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha

services:
- mongodb

env:
- SYMFONY_VERSION=2.8.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test
- SYMFONY_VERSION=3.0.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test
- SYMFONY_VERSION=3.1.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test
- SYMFONY_VERSION=3.2.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test
- SYMFONY_VERSION=3.3.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test

- SYMFONY_VERSION=2.8.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
# - SYMFONY_VERSION=3.0.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
# - SYMFONY_VERSION=3.1.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
# - SYMFONY_VERSION=3.2.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
# - SYMFONY_VERSION=3.3.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
# - SYMFONY_VERSION=3.4.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
- SYMFONY_VERSION=4.0.* DB=pdo_mysql DB_USER=root DB_NAME=lexik_test DEPENDENCIES=alpha
cache:
directories:
- $HOME/.composer/cache

before_install:
- if [ "$DEPENDENCIES" = "beta" ]; then composer config minimum-stability beta; fi;
- if [ "$DEPENDENCIES" = "alpha" ]; then composer config minimum-stability alpha; fi;
- if [ -x .travis/before_install.sh ]; then .travis/before_install.sh; fi;

install:
- if [ -x .travis/install.sh ]; then .travis/install.sh; fi;

script:
- php vendor/bin/phpunit -v
- php vendor/bin/phpunit -v
2 changes: 1 addition & 1 deletion .travis/before_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ]; then
TRAVIS_INI_FILE="$PHP_INI_DIR/travis.ini"
echo "memory_limit=3072M" >> "$TRAVIS_INI_FILE"

if [ "$TRAVIS_PHP_VERSION" '<' '7.0' ]; then
if [ "$TRAVIS_PHP_VERSION" '<' '7.0' ]; then
echo "extension=mongo.so" >> "$TRAVIS_INI_FILE"
else
echo "extension=mongodb.so" >> "$TRAVIS_INI_FILE"
Expand Down
21 changes: 19 additions & 2 deletions Command/ImportTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\TranslatorInterface;

/**
* Imports translation files content in the database.
Expand All @@ -19,6 +21,21 @@
*/
class ImportTranslationsCommand extends ContainerAwareCommand
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
parent::__construct();

$this->translator = $translator;
}

/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
Expand Down Expand Up @@ -71,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($bundleName) {
$bundle = $this->getApplication()->getKernel()->getBundle($bundleName);

if (null !== $bundle->getParent()) {
if (Kernel::VERSION_ID < 40000 && null !== $bundle->getParent()) {
// due to symfony's bundle inheritance if a bundle has a parent it is fetched first.
// so we tell getBundle to NOT fetch the first if a parent is present
$bundles = $this->getApplication()->getKernel()->getBundle($bundle->getParent(), false);
Expand Down Expand Up @@ -114,7 +131,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($this->input->getOption('cache-clear')) {
$this->output->writeln('<info>Removing translations cache files ...</info>');
$this->getContainer()->get('translator')->removeLocalesCacheFiles($locales);
$this->translator->removeLocalesCacheFiles($locales);
}
}

Expand Down
11 changes: 10 additions & 1 deletion DependencyInjection/LexikTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ public function buildTranslatorDefinition(ContainerBuilder $container)
$translator = new Definition();
$translator->setClass('%lexik_translation.translator.class%');

if (Kernel::VERSION_ID >= 30300) {
if (Kernel::VERSION_ID >= 40000) {
$arguments = [
new Reference('service_container'), // Will be replaced by service locator
new Reference('translator.formatter.default'),
new Parameter('kernel.default_locale'),
[], // translation loaders
new Parameter('lexik_translation.translator.options')
];
$translator->setPublic(true);
} elseif (Kernel::VERSION_ID >= 30300) {
$arguments = [
new Reference('service_container'), // Will be replaced by service locator
new Reference('translator.selector'),
Expand Down
26 changes: 16 additions & 10 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="lexik_translation.translator.class">Lexik\Bundle\TranslationBundle\Translation\Translator</parameter>
Expand Down Expand Up @@ -45,6 +45,8 @@

<parameter key="lexik_translation.token_finder.class">Lexik\Bundle\TranslationBundle\Util\Profiler\TokenFinder</parameter>

<parameter key="lexik_translation.command.import_translations.class">Lexik\Bundle\TranslationBundle\Command\ImportTranslationsCommand</parameter>

<parameter key="lexik_translation.importer.case_insensitive">false</parameter>
<parameter key="lexik_translation.token_finder.limit">15</parameter>

Expand All @@ -62,7 +64,7 @@
</service>

<!-- Managers -->
<service id="lexik_translation.trans_unit.manager" class="%lexik_translation.trans_unit.manager.class%">
<service id="lexik_translation.trans_unit.manager" class="%lexik_translation.trans_unit.manager.class%" public="true">
<argument type="service" id="lexik_translation.translation_storage" />
<argument type="service" id="lexik_translation.file.manager" />
<argument>%kernel.root_dir%</argument>
Expand All @@ -73,12 +75,12 @@
<argument>%kernel.root_dir%</argument>
</service>

<service id="lexik_translation.locale.manager" class="%lexik_translation.locale.manager.class%">
<service id="lexik_translation.locale.manager" class="%lexik_translation.locale.manager.class%" public="true">
<argument>%lexik_translation.managed_locales%</argument>
</service>

<!-- Importer -->
<service id="lexik_translation.importer.file" class="%lexik_translation.importer.file.class%">
<service id="lexik_translation.importer.file" class="%lexik_translation.importer.file.class%" public="true">
<argument type="collection" /> <!-- translation loaders -->
<argument type="service" id="lexik_translation.translation_storage" />
<argument type="service" id="lexik_translation.trans_unit.manager" />
Expand All @@ -89,7 +91,7 @@
</service>

<!-- Exporter -->
<service id="lexik_translation.exporter_collector" class="%lexik_translation.exporter_collector.class%" />
<service id="lexik_translation.exporter_collector" class="%lexik_translation.exporter_collector.class%" public="true"/>

<service id="lexik_translation.exporter.xliff" class="%lexik_translation.exporter.xliff.class%">
<tag name="lexik_translation.exporter" alias="xlf" />
Expand All @@ -110,12 +112,12 @@
</service>

<!-- Data grid -->
<service id="lexik_translation.data_grid.formatter" class="%lexik_translation.data_grid.formatter.class%">
<service id="lexik_translation.data_grid.formatter" class="%lexik_translation.data_grid.formatter.class%" public="true">
<argument type="service" id="lexik_translation.locale.manager" />
<argument>%lexik_translation.storage.type%</argument>
</service>

<service id="lexik_translation.data_grid.request_handler" class="%lexik_translation.data_grid.request_handler.class%">
<service id="lexik_translation.data_grid.request_handler" class="%lexik_translation.data_grid.request_handler.class%" public="true">
<argument type="service" id="lexik_translation.trans_unit.manager" />
<argument type="service" id="lexik_translation.file.manager" />
<argument type="service" id="lexik_translation.translation_storage" />
Expand All @@ -129,13 +131,13 @@
</service>

<!-- Overview -->
<service id="lexik_translation.overview.stats_aggregator" class="%lexik_translation.overview.stats_aggregator.class%">
<service id="lexik_translation.overview.stats_aggregator" class="%lexik_translation.overview.stats_aggregator.class%" public="true">
<argument type="service" id="lexik_translation.translation_storage" />
<argument type="service" id="lexik_translation.locale.manager" />
</service>

<!-- Form -->
<service id="lexik_translation.form.handler.trans_unit" class="%lexik_translation.form.handler.trans_unit.class%">
<service id="lexik_translation.form.handler.trans_unit" class="%lexik_translation.form.handler.trans_unit.class%" public="true">
<argument type="service" id="lexik_translation.trans_unit.manager" />
<argument type="service" id="lexik_translation.file.manager" />
<argument type="service" id="lexik_translation.translation_storage" />
Expand All @@ -150,5 +152,9 @@
<tag name="kernel.event_listener" event="lexik_translation.event.get_database_resources" method="onGetDatabaseResources" />
</service>

<!-- Command -->
<service id="lexik_translation.command.import_translations" class="%lexik_translation.command.import_translations.class%">
<argument type="service" id="translator.default" />
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Tests/Unit/Translation/Exporter/YamlExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testExport()
// export empty array
$exporter->export($outFile, array());
$expectedContent = '{ }';
$this->assertEquals($expectedContent, file_get_contents($outFile));
$this->assertEquals($expectedContent, trim(file_get_contents($outFile)));

// export array with values
$exporter->export($outFile, array(
Expand Down
5 changes: 5 additions & 0 deletions Tests/Unit/Translation/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Lexik\Bundle\TranslationBundle\Tests\Unit\BaseUnitTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\DependencyInjection\Container;

Expand Down Expand Up @@ -129,6 +130,10 @@ protected function createTranslator($em, $cacheDir)
'cache_dir' => $cacheDir,
);

if (Kernel::VERSION_ID >= 40000) {
return new TranslatorMock($container, new MessageFormatter(), 'en', $loaderIds, $options);
}

if (Kernel::VERSION_ID >= 30300) {
return new TranslatorMock($container, new MessageSelector(), 'en', $loaderIds, $options);
}
Expand Down
4 changes: 1 addition & 3 deletions Translation/Exporter/YamlExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public function export($file, $translations)
$translations = $this->flattenArray($result);
}

$ymlDumper = new Dumper();
$ymlDumper->setIndentation(2);
$ymlContent = $ymlDumper->dump($translations, 10);
$ymlContent = (new Dumper())->dump($translations, 10, 0);

$bytes = file_put_contents($file, $ymlContent);

Expand Down
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
],
"require": {
"php": "^5.6 || ^7.0",
"symfony/framework-bundle": "~2.8|~3.0"
"symfony/framework-bundle": "~2.8|~3.0|~4.0"
},
"require-dev": {
"doctrine/orm": "^2.2.3",
"doctrine/doctrine-bundle": "~1.2",
"doctrine/data-fixtures": "~1.1",
"doctrine/mongodb-odm-bundle": "~3.0",
"propel/propel-bundle": "~3.0@dev",
"propel/propel-bundle": "^3.0@dev|^4.0@dev",
"propel/propel": "@dev",
"phpunit/phpunit": "^5.7"
},
"suggest": {
Expand All @@ -35,6 +36,11 @@
"autoload": {
"psr-4": { "Lexik\\Bundle\\TranslationBundle\\": "" }
},
"autoload-dev": {
"classmap": [
"Tests/app/AppKernel.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "4.x.x-dev"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

<php>
<server name="KERNEL_DIR" value="./Tests/app/" />
<server name="KERNEL_CLASS" value="AppKernel" />
</php>

<filter>
Expand Down

0 comments on commit 3a2b706

Please sign in to comment.