Skip to content

Commit

Permalink
Revert & Improvements (#309)
Browse files Browse the repository at this point in the history
* Best practices
* Rename DefaultProvider to SimpleProvider
* Some improvements
  • Loading branch information
webda2l authored Apr 3, 2018
1 parent 3a4561f commit 96db1c0
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 39 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -14,20 +13,19 @@ 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

Check out the documentation on the [official website](http://a2lix.fr/bundles/translation-form).

## 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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/DependencyInjection/Compiler/LocaleProviderPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
18 changes: 15 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
;

Expand Down
2 changes: 1 addition & 1 deletion src/Form/EventListener/TranslationsFormsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Form/EventListener/TranslationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Form/Type/TranslatedEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Form/Type/TranslationsFormsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace A2lix\TranslationFormBundle\Locale;

class DefaultProvider implements LocaleProviderInterface
class SimpleProvider implements LocaleProviderInterface
{
/** @var array */
protected $locales;
Expand Down
20 changes: 11 additions & 9 deletions src/Resources/config/a2lix_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,40 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults public="false" />

<!-- Locale Providers -->
<service id="A2lix\TranslationFormBundle\Locale\DefaultProvider"></service>
<service id="a2lix_translation_form.locale.simple_provider" class="A2lix\TranslationFormBundle\Locale\SimpleProvider"></service>

<!-- Form Listeners -->
<service id="A2lix\TranslationFormBundle\Form\EventListener\TranslationsListener">
<service id="a2lix_translation_form.form.event_listener.translations_listener" class="A2lix\TranslationFormBundle\Form\EventListener\TranslationsListener">
<argument type="service" id="a2lix_auto_form.manipulator.default"/>
</service>

<service id="A2lix\TranslationFormBundle\Form\EventListener\TranslationsFormsListener"></service>
<service id="a2lix_translation_form.form.event_listener.translations_form_listener" class="A2lix\TranslationFormBundle\Form\EventListener\TranslationsFormsListener"></service>

<!-- Form Types -->
<service id="A2lix\TranslationFormBundle\Form\Type\TranslationsType">
<argument type="service" id="A2lix\TranslationFormBundle\Form\EventListener\TranslationsListener"/>
<service id="a2lix_translation_form.form.type.translations_type" class="A2lix\TranslationFormBundle\Form\Type\TranslationsType">
<argument type="service" id="a2lix_translation_form.form.event_listener.translations_listener"/>
<argument type="service" id="a2lix_translation_form.locale_provider.default"/>

<tag name="form.type"/>
</service>

<service id="A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType">
<argument type="service" id="A2lix\TranslationFormBundle\Form\EventListener\TranslationsFormsListener"/>
<service id="a2lix_translation_form.form.type.translations_forms_type" class="A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType">
<argument type="service" id="a2lix_translation_form.form.event_listener.translations_form_listener"/>
<argument type="service" id="a2lix_translation_form.locale_provider.default"/>

<tag name="form.type"/>
</service>

<service id="A2lix\TranslationFormBundle\Form\Type\TranslationsLocalesSelectorType">
<service id="a2lix_translation_form.form.type.translations_locales_selector_type" class="A2lix\TranslationFormBundle\Form\Type\TranslationsLocalesSelectorType">
<argument type="service" id="a2lix_translation_form.locale_provider.default"/>

<tag name="form.type"/>
</service>

<service id="A2lix\TranslationFormBundle\Form\Type\TranslatedEntityType">
<service id="a2lix_translation_form.form.type.translated_entity_type" class="A2lix\TranslationFormBundle\Form\Type\TranslatedEntityType">
<argument type="service" id="request_stack"/>

<tag name="form.type"/>
Expand Down
6 changes: 3 additions & 3 deletions tests/Form/TypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,15 +87,15 @@ 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);
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -44,15 +45,15 @@ 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', []);
}

public function testRequiredLocaleAreInLocales(): void
{
// Get mock, without the constructor being called
$mock = $this->getMockBuilder(DefaultProvider::class)
$mock = $this->getMockBuilder(SimpleProvider::class)
->disableOriginalConstructor()
->getMock();

Expand All @@ -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']);
}
Expand Down

0 comments on commit 96db1c0

Please sign in to comment.