Skip to content

Commit

Permalink
Some code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sadikoff committed Dec 27, 2017
1 parent 921068c commit 13db9b0
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 101 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ Requirements
------------
* Symfony flex with Symfony 3.4|4.0
* i18n Doctrine strategy of your choice
* [KnpDoctrineExtension](https://github.com/KnpLabs/DoctrineBehaviors#translatable)
* [KnpLabs/DoctrineBehaviors](https://github.com/KnpLabs/DoctrineBehaviors#translatable) with `knplabs/doctrine-behaviors`
* [Atlantic18/DoctrineExtensions](https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/translatable.md) with `stof/doctrine-extensions-bundle`
* [A2lixI18nDoctrineBundle](https://github.com/a2lix/I18nDoctrineBundle) [Deprecated - will be removed from bundle in v.4.0.6]
* [PrezentDoctrineTranslatableBundle](https://github.com/Prezent/doctrine-translatable-bundle/blob/master/Resources/doc/index.md) [Deprecated - will be removed from bundle in v.4.0.6]

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
},
"suggest": {
"knplabs/doctrine-behaviors": "For Knp strategy",
"a2lix/i18n-doctrine-bundle": "For A2lix strategy",
"prezent/doctrine-translatable-bundle": "For Prezent strategy"
"stof/doctrine-extensions-bundle": "For Atlantic18/DoctrineExtensions"
},
"config": {
"preferred-install": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php

namespace Koff\Bundle\I18nFormBundle\ObjectInfo;
namespace Koff\Bundle\I18nFormBundle\Extractor;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Koff\Bundle\I18nFormBundle\Form\Type\AutoFormType;

class DoctrineInfo implements ObjectInfoInterface
/**
* Class DoctrineEntityFieldsExtractor.
*
* @author David ALLIX
* @author Sadicov Vladimir <[email protected]>
*/
class DoctrineEntityFieldsExtractor implements FieldsExtractorInterface
{
/** @var EntityManagerInterface */
private $entityManager;
Expand Down
15 changes: 15 additions & 0 deletions src/Extractor/FieldsExtractorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Koff\Bundle\I18nFormBundle\Extractor;

/**
* Interface EntityInfoInterface.
*
* @author Sadicov Vladimir <[email protected]>
*/
interface FieldsExtractorInterface
{
public function getFieldsConfig($class);

public function getAssociationTargetClass($class, $fieldName);
}
5 changes: 5 additions & 0 deletions src/Form/EventListener/KoffI18nListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

/**
* Class KoffI18nListener.
*
* @author Sadicov Vladimir <[email protected]>
*/
abstract class KoffI18nListener implements EventSubscriberInterface
{
/**
Expand Down
18 changes: 6 additions & 12 deletions src/Form/EventListener/TranslationsFormsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@
namespace Koff\Bundle\I18nFormBundle\Form\EventListener;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

/**
* Class TranslationsFormsListener.
*
* @author David ALLIX
* @author Sadicov Vladimir <[email protected]>
*/
class TranslationsFormsListener extends KoffI18nListener
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::SUBMIT => 'submit',
];
}

/**
* @param FormEvent $event
*/
Expand Down
27 changes: 6 additions & 21 deletions src/Form/EventListener/TranslationsListener.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
<?php

/*
* This file is part of A2lix projects.
*
* (c) David ALLIX
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Koff\Bundle\I18nFormBundle\Form\EventListener;

use Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulatorInterface;
use Koff\Bundle\I18nFormBundle\Form\Type\AutoFormType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;

/**
* Class TranslationsListener.
*
* @author David ALLIX
* @author Sadicov Vladimir <[email protected]>
*/
class TranslationsListener extends KoffI18nListener
{
/** @var FormManipulatorInterface */
Expand All @@ -30,17 +26,6 @@ public function __construct(FormManipulatorInterface $formManipulator)
$this->formManipulator = $formManipulator;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::SUBMIT => 'submit',
];
}

/**
* @param FormEvent $event
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Koff\Bundle\I18nFormBundle\Form\Manipulator;

use Doctrine\Common\Util\ClassUtils;
use Koff\Bundle\I18nFormBundle\ObjectInfo\ObjectInfoInterface;
use Koff\Bundle\I18nFormBundle\Extractor\FieldsExtractorInterface;
use Symfony\Component\Form\FormInterface;

/**
Expand All @@ -12,21 +12,21 @@
* @author David ALLIX
* @author Sadicov Vladimir <[email protected]>
*/
class DefaultManipulator implements FormManipulatorInterface
class FormManipulator implements FormManipulatorInterface
{
/** @var ObjectInfoInterface */
private $objectInfo;
/** @var FieldsExtractorInterface */
private $fieldsExtractor;

/** @var array */
private $globalExcludedFields;

/**
* @param ObjectInfoInterface $objectInfo
* @param array $globalExcludedFields
* @param FieldsExtractorInterface $fieldsExtractor
* @param array $globalExcludedFields
*/
public function __construct(ObjectInfoInterface $objectInfo, array $globalExcludedFields = [])
public function __construct(FieldsExtractorInterface $fieldsExtractor, array $globalExcludedFields = [])
{
$this->objectInfo = $objectInfo;
$this->fieldsExtractor = $fieldsExtractor;
$this->globalExcludedFields = $globalExcludedFields;
}

Expand All @@ -39,7 +39,7 @@ public function getFieldsConfig(FormInterface $form)
$formOptions = $form->getConfig()->getOptions();
$formFields = $formOptions['fields'];

$objectFields = $this->objectInfo->getFieldsConfig($class);
$objectFields = $this->fieldsExtractor->getFieldsConfig($class);
$objectFields = $this->filterObjectFields($objectFields, $formOptions['excluded_fields']);

if (empty($formFields)) {
Expand All @@ -49,9 +49,13 @@ public function getFieldsConfig(FormInterface $form)
$this->checkUnknownFields($formFields, $objectFields, $class);

$fieldsConfig = $this->filterFields($formFields);
array_walk($fieldsConfig, function (&$v, $k, $d) {
$v += $d[$k];
}, $objectFields);
array_walk(
$fieldsConfig,
function (&$v, $k, $d) {
$v += $d[$k];
},
$objectFields
);

return $fieldsConfig;
}
Expand Down Expand Up @@ -93,7 +97,7 @@ private function getDataClass(FormInterface $form)
continue;
}

return $this->objectInfo->getAssociationTargetClass($dataClass, $form->getName());
return $this->fieldsExtractor->getAssociationTargetClass($dataClass, $form->getName());
}
}

Expand All @@ -112,8 +116,11 @@ private function filterObjectFields(array $objectFieldsConfig, array $formExclud

private function filterFields($fields)
{
return array_filter($fields, function ($v) {
return !(null === $v || (array_key_exists('display', $v) && !$v['display']));
});
return array_filter(
$fields,
function ($v) {
return !(null === $v || (array_key_exists('display', $v) && !$v['display']));
}
);
}
}
2 changes: 1 addition & 1 deletion src/Form/Type/TranslationsFormsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Koff\Bundle\I18nFormBundle\Form\Type;

use Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsFormsListener;
use Koff\Bundle\I18nFormBundle\Locale\LocaleProviderInterface;
use Koff\Bundle\I18nFormBundle\Provider\LocaleProviderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/TranslationsLocalesSelectorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Koff\Bundle\I18nFormBundle\Form\Type;

use Koff\Bundle\I18nFormBundle\Locale\LocaleProviderInterface;
use Koff\Bundle\I18nFormBundle\Provider\LocaleProviderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/TranslationsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Koff\Bundle\I18nFormBundle\Form\Type;

use Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsListener;
use Koff\Bundle\I18nFormBundle\Locale\LocaleProviderInterface;
use Koff\Bundle\I18nFormBundle\Provider\LocaleProviderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand Down
10 changes: 0 additions & 10 deletions src/ObjectInfo/ObjectInfoInterface.php

This file was deleted.

20 changes: 8 additions & 12 deletions src/Locale/DefaultProvider.php → src/Provider/LocaleProvider.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php

/*
* This file is part of A2lix projects.
*
* (c) Gonzalo Vilaseca <[email protected]> . Reiss Clothing Ltd.
namespace Koff\Bundle\I18nFormBundle\Provider;

/**
* Class LocaleProvider.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @author Gonzalo Vilaseca <[email protected]> . Reiss Clothing Ltd.
*/

namespace Koff\Bundle\I18nFormBundle\Locale;

class DefaultProvider implements LocaleProviderInterface
class LocaleProvider implements LocaleProviderInterface
{
/** @var array */
protected $locales;
Expand All @@ -31,10 +27,10 @@ public function __construct(array $locales, $defaultLocale, array $requiredLocal
{
if (!in_array($defaultLocale, $locales, true)) {
if (count($locales)) {
throw new \InvalidArgumentException(sprintf('Default locale `%s` not found within the configured locales `[%s]`. Perhaps you need to add it to your `koff_i18n_form.locales` bundle configuration?', $defaultLocale, implode(',', $locales)));
throw new \InvalidArgumentException(sprintf('Default locale `%s` not found within the configured locales `[%s]`. Perhaps you need to add it to your `i18n_form.locales` bundle configuration?', $defaultLocale, implode(',', $locales)));
}

throw new \InvalidArgumentException(sprintf('No locales were configured, but expected at least the default locale `%s`. Perhaps you need to add it to your `koff_i18n_form.locales` bundle configuration?', $defaultLocale));
throw new \InvalidArgumentException(sprintf('No locales were configured, but expected at least the default locale `%s`. Perhaps you need to add it to your `i18n_form.locales` bundle configuration?', $defaultLocale));
}

if (array_diff($requiredLocales, $locales)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<?php

/*
* This file is part of A2lix projects.
*
* (c) Gonzalo Vilaseca <[email protected]> . Reiss Clothing Ltd.
namespace Koff\Bundle\I18nFormBundle\Provider;

/**
* Interface LocaleProviderInterface.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @author Gonzalo Vilaseca <[email protected]> . Reiss Clothing Ltd.
*/

namespace Koff\Bundle\I18nFormBundle\Locale;

interface LocaleProviderInterface
{
/**
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
<defaults public="false" />

<!-- Locale -->
<service id="koff_i18n_form.locale_provider" class="Koff\Bundle\I18nFormBundle\Locale\DefaultProvider">
<service id="koff_i18n_form.locale_provider" class="Koff\Bundle\I18nFormBundle\Provider\LocaleProvider">
<argument/>
<argument/>
<argument/>
</service>
<!-- /Locale -->

<!-- Manipulator -->
<service id="koff_i18n_form.doctrine_info" class="Koff\Bundle\I18nFormBundle\ObjectInfo\DoctrineInfo">
<service id="koff_i18n_form.fields_extractor" class="Koff\Bundle\I18nFormBundle\Extractor\DoctrineEntityFieldsExtractor">
<argument type="service" id="Doctrine\ORM\EntityManagerInterface" />
</service>

<service id="koff_i18n_form.form_manipulator" class="Koff\Bundle\I18nFormBundle\Form\Manipulator\DefaultManipulator">
<argument type="service" id="koff_i18n_form.doctrine_info" />
<service id="koff_i18n_form.form_manipulator" class="Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulator">
<argument type="service" id="koff_i18n_form.fields_extractor" />
<argument type="collection"/>
</service>
<!-- /Manipulator -->
Expand Down
10 changes: 5 additions & 5 deletions tests/Form/TypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

abstract class TypeTestCase extends BaseTypeTestCase
{
/** @var \Koff\Bundle\I18nFormBundle\Form\Manipulator\DefaultManipulator */
/** @var \Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulator */
protected $defaultFormManipulator;

protected function setUp()
Expand Down Expand Up @@ -55,9 +55,9 @@ protected function getDefaultFormManipulator()

$config = Setup::createAnnotationMetadataConfiguration([__DIR__ . '/../Fixtures/Entity'], true, null, null, false);
$entityManager = EntityManager::create(['driver' => 'pdo_sqlite'], $config);
$doctrineInfo = new \Koff\Bundle\I18nFormBundle\ObjectInfo\DoctrineInfo($entityManager);
$doctrineInfo = new \Koff\Bundle\I18nFormBundle\Extractor\DoctrineEntityFieldsExtractor($entityManager);

return $this->defaultFormManipulator = new \Koff\Bundle\I18nFormBundle\Form\Manipulator\DefaultManipulator($doctrineInfo, ['id', 'locale', 'translatable']);
return $this->defaultFormManipulator = new \Koff\Bundle\I18nFormBundle\Form\Manipulator\FormManipulator($doctrineInfo, ['id', 'locale', 'translatable']);
}

protected function getConfiguredAutoFormType()
Expand All @@ -70,15 +70,15 @@ protected function getConfiguredAutoFormType()
protected function getConfiguredTranslationsType($locales, $defaultLocale, $requiredLocales)
{
$translationsListener = new \Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsListener($this->getDefaultFormManipulator());
$localProvider = new \Koff\Bundle\I18nFormBundle\Locale\DefaultProvider($locales, $defaultLocale, $requiredLocales);
$localProvider = new \Koff\Bundle\I18nFormBundle\Provider\LocaleProvider($locales, $defaultLocale, $requiredLocales);

return new \Koff\Bundle\I18nFormBundle\Form\Type\TranslationsType($translationsListener, $localProvider);
}

protected function getConfiguredTranslationsFormsType($locales, $defaultLocale, $requiredLocales)
{
$translationsFormsListener = new \Koff\Bundle\I18nFormBundle\Form\EventListener\TranslationsFormsListener();
$localProvider = new \Koff\Bundle\I18nFormBundle\Locale\DefaultProvider($locales, $defaultLocale, $requiredLocales);
$localProvider = new \Koff\Bundle\I18nFormBundle\Provider\LocaleProvider($locales, $defaultLocale, $requiredLocales);

return new \Koff\Bundle\I18nFormBundle\Form\Type\TranslationsFormsType($translationsFormsListener, $localProvider);
}
Expand Down
Loading

0 comments on commit 13db9b0

Please sign in to comment.