diff --git a/README.md b/README.md index e6c67f5..3f186e7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ Translate your doctrine objects easily with some helps [![Latest Stable Version](https://poser.pugx.org/a2lix/translation-form-bundle/v/stable)](https://packagist.org/packages/a2lix/translation-form-bundle) [![Latest Unstable Version](https://poser.pugx.org/a2lix/translation-form-bundle/v/unstable)](https://packagist.org/packages/a2lix/translation-form-bundle) [![License](https://poser.pugx.org/a2lix/translation-form-bundle/license)](https://packagist.org/packages/a2lix/translation-form-bundle) -[![SensioLabsInsight](https://insight.sensiolabs.com/projects/64aee70e-7b00-406f-8648-f7ea66e29f80/mini.png)](https://insight.sensiolabs.com/projects/64aee70e-7b00-406f-8648-f7ea66e29f80) [![Total Downloads](https://poser.pugx.org/a2lix/translation-form-bundle/downloads)](https://packagist.org/packages/a2lix/translation-form-bundle) [![Monthly Downloads](https://poser.pugx.org/a2lix/translation-form-bundle/d/monthly)](https://packagist.org/packages/a2lix/translation-form-bundle) @@ -14,7 +13,7 @@ Translate your doctrine objects easily with some helps | Branch | Tools | | --- | --- | | 2.x | [![Build Status][travis_stable_badge]][travis_stable_link] [![Coverage Status][coveralls_stable_badge]][coveralls_stable_link] [![Scrutinizer Status][scrutinizer_stable_badge]][scrutinizer_stable_link] | -| master | [![Build Status](https://travis-ci.org/a2lix/TranslationFormBundle.svg?branch=master)](https://travis-ci.org/a2lix/TranslationFormBundle) [![Code Coverage](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/?branch=master) | +| 3.x (master) | [![Build Status](https://travis-ci.org/a2lix/TranslationFormBundle.svg?branch=master)](https://travis-ci.org/a2lix/TranslationFormBundle) [![Code Coverage](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/a2lix/TranslationFormBundle/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/64aee70e-7b00-406f-8648-f7ea66e29f80/mini.png)](https://insight.sensiolabs.com/projects/64aee70e-7b00-406f-8648-f7ea66e29f80) | ## Documentation @@ -22,12 +21,11 @@ Check out the documentation on the [official website](http://a2lix.fr/bundles/tr ## Support -* `2.x` is the stable branch and it is **maintained**. It is compatible with [Gedmo](https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/translatable.md), [KnpLabs](https://github.com/KnpLabs/DoctrineBehaviors#translatable), [A2lix](https://github.com/a2lix/I18nDoctrineBundle) and [Prezent](https://github.com/Prezent/doctrine-translatable-bundle) -* `master` is the unstable branch. It uses [AutoFormBundle](https://github.com/a2lix/AutoFormBundle) and is compatible with [KnpLabs](https://github.com/KnpLabs/DoctrineBehaviors#translatable), [A2lix](https://github.com/a2lix/I18nDoctrineBundle) and [Prezent](https://github.com/Prezent/doctrine-translatable-bundle) +* `0.x` & `1.x` are old versions not maintained anymore. +* `2.x` is a stable version with low requirements (PHP5.4+/7+, Symfony2.3+/3.0+/4.0+). It is compatible with [Gedmo](https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/translatable.md), [KnpLabs](https://github.com/KnpLabs/DoctrineBehaviors#translatable), [A2lix](https://github.com/a2lix/I18nDoctrineBundle) and [Prezent](https://github.com/Prezent/doctrine-translatable-bundle) +* `3.x` is the new and rethink version that uses [AutoFormBundle](https://github.com/a2lix/AutoFormBundle) and has higher requirements (PHP7.1+, Symfony3.4+/4+). It is compatible with [KnpLabs](https://github.com/KnpLabs/DoctrineBehaviors#translatable), [A2lix](https://github.com/a2lix/I18nDoctrineBundle) and [Prezent](https://github.com/Prezent/doctrine-translatable-bundle) -For general support and questions, and if you think you found a bug or -you have a feature idea to propose, feel free to open an issue -**after looking** at the [contributing guide](CONTRIBUTING.md). +For general support and questions, and if you think you found a bug or you have a feature idea to propose, feel free to open an issue **after looking** at the [contributing guide](CONTRIBUTING.md). ## License diff --git a/composer.json b/composer.json index f2c9a14..3058284 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ ], "require": { "php": "^7.1.3", - "a2lix/auto-form-bundle": "0.1-beta.3", + "a2lix/auto-form-bundle": "^0.1", "symfony/config": "^3.4|^4.0", "symfony/dependency-injection": "^3.4|^4.0", "symfony/doctrine-bridge": "^3.4|^4.0", diff --git a/src/DependencyInjection/Compiler/LocaleProviderPass.php b/src/DependencyInjection/Compiler/LocaleProviderPass.php index fa6c5df..d100846 100644 --- a/src/DependencyInjection/Compiler/LocaleProviderPass.php +++ b/src/DependencyInjection/Compiler/LocaleProviderPass.php @@ -18,23 +18,25 @@ class LocaleProviderPass implements CompilerPassInterface { + public const DEFAULT_LOCALE_PROVIDER_KEY = 'default'; + public function process(ContainerBuilder $container): void { $localeProvider = $container->getParameter('a2lix_translation_form.locale_provider'); - if ('default' !== $localeProvider) { + if (self::DEFAULT_LOCALE_PROVIDER_KEY !== $localeProvider) { $container->setAlias('a2lix_translation_form.locale_provider.default', $localeProvider); return; } - $definition = $container->getDefinition('A2lix\TranslationFormBundle\Locale\DefaultProvider'); + $definition = $container->getDefinition('a2lix_translation_form.locale.simple_provider'); $definition->setArguments([ $container->getParameter('a2lix_translation_form.locales'), $container->getParameter('a2lix_translation_form.default_locale'), $container->getParameter('a2lix_translation_form.required_locales'), ]); - $container->setAlias('a2lix_translation_form.locale_provider.default', 'A2lix\TranslationFormBundle\Locale\DefaultProvider'); + $container->setAlias('a2lix_translation_form.locale_provider.default', 'a2lix_translation_form.locale.simple_provider'); } } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 2bee48f..e5646ec 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -13,6 +13,7 @@ namespace A2lix\TranslationFormBundle\DependencyInjection; +use A2lix\TranslationFormBundle\DependencyInjection\Compiler\LocaleProviderPass; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -25,8 +26,14 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->children() - ->scalarNode('locale_provider')->defaultValue('default')->end() - ->scalarNode('default_locale')->defaultNull()->end() + ->scalarNode('locale_provider') + ->defaultValue(LocaleProviderPass::DEFAULT_LOCALE_PROVIDER_KEY) + ->info('Set your own LocaleProvider service identifier if required') + ->end() + ->scalarNode('default_locale') + ->defaultNull() + ->info('Set your own default locale if different from the SymfonyFramework locale. eg: en') + ->end() ->arrayNode('locales') ->beforeNormalization() ->ifString() @@ -36,6 +43,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->requiresAtLeastOneElement() ->prototype('scalar')->end() + ->info('Set the list of locales to manage (default locale included). eg: [en, fr, de, es]') ->end() ->arrayNode('required_locales') ->beforeNormalization() @@ -45,8 +53,12 @@ public function getConfigTreeBuilder(): TreeBuilder }) ->end() ->prototype('scalar')->end() + ->info('Set the list of required locales to manage. eg: [en]') + ->end() + ->scalarNode('templating') + ->defaultValue('@A2lixTranslationForm/bootstrap_4_layout.html.twig') + ->info('Set your own template path if required') ->end() - ->scalarNode('templating')->defaultValue('@A2lixTranslationForm/bootstrap_4_layout.html.twig')->end() ->end() ; diff --git a/src/Form/EventListener/TranslationsFormsListener.php b/src/Form/EventListener/TranslationsFormsListener.php index 5a1eed0..db1c6df 100644 --- a/src/Form/EventListener/TranslationsFormsListener.php +++ b/src/Form/EventListener/TranslationsFormsListener.php @@ -47,7 +47,7 @@ public function submit(FormEvent $event): void foreach ($data as $locale => $translation) { // Remove useless Translation object - if (!$translation) { + if (empty($translation)) { $data->removeElement($translation); continue; } diff --git a/src/Form/EventListener/TranslationsListener.php b/src/Form/EventListener/TranslationsListener.php index 54655eb..bfba6d5 100644 --- a/src/Form/EventListener/TranslationsListener.php +++ b/src/Form/EventListener/TranslationsListener.php @@ -71,7 +71,7 @@ public function submit(FormEvent $event): void foreach ($data as $locale => $translation) { // Remove useless Translation object - if (!$translation) { + if (empty($translation)) { $data->removeElement($translation); continue; } diff --git a/src/Form/Type/TranslatedEntityType.php b/src/Form/Type/TranslatedEntityType.php index 0ff0352..12e285f 100644 --- a/src/Form/Type/TranslatedEntityType.php +++ b/src/Form/Type/TranslatedEntityType.php @@ -34,7 +34,6 @@ public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'translation_path' => 'translations', - 'translation_property' => null, 'query_builder' => function (EntityRepository $er) { return $er->createQueryBuilder('e') ->select('e, t') @@ -48,6 +47,8 @@ public function configureOptions(OptionsResolver $resolver): void return $options['translation_path'].'['.$request->getLocale().'].'.$options['translation_property']; }, ]); + + $resolver->setRequired('translation_property'); } public function getParent(): string diff --git a/src/Form/Type/TranslationsFormsType.php b/src/Form/Type/TranslationsFormsType.php index 1175dfe..a5bb521 100644 --- a/src/Form/Type/TranslationsFormsType.php +++ b/src/Form/Type/TranslationsFormsType.php @@ -58,11 +58,12 @@ public function configureOptions(OptionsResolver $resolver): void 'locales' => $this->localeProvider->getLocales(), 'default_locale' => $this->localeProvider->getDefaultLocale(), 'required_locales' => $this->localeProvider->getRequiredLocales(), - 'form_type' => null, 'form_options' => [], ]); - $resolver->setNormalizer('form_options', function (Options $options, $value) { + $resolver->setRequired('form_type'); + + $resolver->setNormalizer('form_options', function (Options $options, $value): array { // Check mandatory data_class option when AutoFormType use if (($options['form_type'] instanceof AutoFormType) && !isset($value['data_class'])) { throw new \RuntimeException(sprintf('Missing "data_class" option under "form_options" of TranslationsFormsType. Required when "form_type" use "AutoFormType".')); diff --git a/src/Locale/DefaultProvider.php b/src/Locale/SimpleProvider.php similarity index 96% rename from src/Locale/DefaultProvider.php rename to src/Locale/SimpleProvider.php index 2893ac8..a982e32 100644 --- a/src/Locale/DefaultProvider.php +++ b/src/Locale/SimpleProvider.php @@ -13,7 +13,7 @@ namespace A2lix\TranslationFormBundle\Locale; -class DefaultProvider implements LocaleProviderInterface +class SimpleProvider implements LocaleProviderInterface { /** @var array */ protected $locales; diff --git a/src/Resources/config/a2lix_form.xml b/src/Resources/config/a2lix_form.xml index 808e224..6bd1b77 100644 --- a/src/Resources/config/a2lix_form.xml +++ b/src/Resources/config/a2lix_form.xml @@ -5,38 +5,40 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + - + - + - + - - + + - - + + - + - + diff --git a/tests/Form/TypeTestCase.php b/tests/Form/TypeTestCase.php index b251924..0afe385 100644 --- a/tests/Form/TypeTestCase.php +++ b/tests/Form/TypeTestCase.php @@ -21,7 +21,7 @@ use A2lix\TranslationFormBundle\Form\EventListener\TranslationsListener; use A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType; use A2lix\TranslationFormBundle\Form\Type\TranslationsType; -use A2lix\TranslationFormBundle\Locale\DefaultProvider; +use A2lix\TranslationFormBundle\Locale\SimpleProvider; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -87,7 +87,7 @@ protected function getConfiguredAutoFormType(): AutoFormType protected function getConfiguredTranslationsType(array $locales, string $defaultLocale, array $requiredLocales): TranslationsType { $translationsListener = new TranslationsListener($this->getDoctrineORMFormManipulator()); - $localProvider = new DefaultProvider($locales, $defaultLocale, $requiredLocales); + $localProvider = new SimpleProvider($locales, $defaultLocale, $requiredLocales); return new TranslationsType($translationsListener, $localProvider); } @@ -95,7 +95,7 @@ protected function getConfiguredTranslationsType(array $locales, string $default protected function getConfiguredTranslationsFormsType(array $locales, string $defaultLocale, array $requiredLocales): TranslationsFormsType { $translationsFormsListener = new TranslationsFormsListener(); - $localProvider = new DefaultProvider($locales, $defaultLocale, $requiredLocales); + $localProvider = new SimpleProvider($locales, $defaultLocale, $requiredLocales); return new TranslationsFormsType($translationsFormsListener, $localProvider); } diff --git a/tests/Locale/DefaultProviderTest.php b/tests/Locale/SimpleProviderTest.php similarity index 83% rename from tests/Locale/DefaultProviderTest.php rename to tests/Locale/SimpleProviderTest.php index 4a7971c..c36ae06 100644 --- a/tests/Locale/DefaultProviderTest.php +++ b/tests/Locale/SimpleProviderTest.php @@ -13,9 +13,10 @@ namespace A2lix\TranslationFormBundle\Tests\Locale; -use A2lix\TranslationFormBundle\Locale\DefaultProvider; +use A2lix\TranslationFormBundle\Locale\SimpleProvider; +use PHPUnit\Framework\TestCase; -class DefaultProviderTest extends \PHPUnit\Framework\TestCase +class SimpleProviderTest extends TestCase { protected $locales; protected $defaultLocale; @@ -28,13 +29,13 @@ public function setUp(): void $this->defaultLocale = 'en'; $this->requiredLocales = ['es', 'en']; - $this->provider = new DefaultProvider($this->locales, $this->defaultLocale, $this->requiredLocales); + $this->provider = new SimpleProvider($this->locales, $this->defaultLocale, $this->requiredLocales); } public function testDefaultLocaleIsInLocales(): void { // Get mock, without the constructor being called - $mock = $this->getMockBuilder(DefaultProvider::class) + $mock = $this->getMockBuilder(SimpleProvider::class) ->disableOriginalConstructor() ->getMock(); @@ -44,7 +45,7 @@ public function testDefaultLocaleIsInLocales(): void .' Perhaps you need to add it to your `a2lix_translation_form.locales` bundle configuration?'); // Now call the constructor - $reflectedClass = new \ReflectionClass(DefaultProvider::class); + $reflectedClass = new \ReflectionClass(SimpleProvider::class); $constructor = $reflectedClass->getConstructor(); $constructor->invoke($mock, ['es', 'en'], 'de', []); } @@ -52,7 +53,7 @@ public function testDefaultLocaleIsInLocales(): void public function testRequiredLocaleAreInLocales(): void { // Get mock, without the constructor being called - $mock = $this->getMockBuilder(DefaultProvider::class) + $mock = $this->getMockBuilder(SimpleProvider::class) ->disableOriginalConstructor() ->getMock(); @@ -61,7 +62,7 @@ public function testRequiredLocaleAreInLocales(): void $this->expectExceptionMessage('Required locales should be contained in locales'); // Now call the constructor - $reflectedClass = new \ReflectionClass(DefaultProvider::class); + $reflectedClass = new \ReflectionClass(SimpleProvider::class); $constructor = $reflectedClass->getConstructor(); $constructor->invoke($mock, ['es', 'en'], 'en', ['en', 'pt']); }