From 701dc6b0f779f145941e5b35586561b4f7fcecb0 Mon Sep 17 00:00:00 2001 From: Mathieu Lemoine Date: Mon, 26 Sep 2016 23:44:54 -0400 Subject: [PATCH 1/2] Rewrite Translatable/Translation to use Interfaces instead of Traits --- README.md | 44 +++++---- config/orm-services.yml | 9 +- .../Translatable/TranslatableInterface.php | 95 +++++++++++++++++++ .../Translatable/TranslatableMethods.php | 18 ++-- .../Translatable/TranslationInterface.php | 73 ++++++++++++++ src/Model/Translatable/TranslationMethods.php | 7 +- .../Translatable/TranslatableSubscriber.php | 46 +++++---- .../ORM/TranslatableTest.php | 4 +- .../ORM/TranslatableCustomizedEntity.php | 2 +- .../ORM/TranslatableEntity.php | 2 +- .../ORM/TranslatableEntityTranslation.php | 2 +- ...ranslatableCustomizedEntityTranslation.php | 2 +- 12 files changed, 247 insertions(+), 57 deletions(-) create mode 100644 src/Model/Translatable/TranslatableInterface.php create mode 100644 src/Model/Translatable/TranslationInterface.php diff --git a/README.md b/README.md index 3d8f8048..e7758d72 100644 --- a/README.md +++ b/README.md @@ -187,13 +187,13 @@ You now have a working `Category` that behaves like: ### translatable: -If you're working on a `Category` entity, the `Translatable` behavior expects a **CategoryTranslation** entity in the +If you're working on a `Category` entity, the `Translatable` behavior expects a **CategoryTranslation** entity in the same folder of Category entity by default. The default naming convention (or its customization via trait methods) avoids you to manually handle entity associations. It is handled automatically by the TranslationSubscriber. -In order to use the Translatable trait, you will have to create this `CategoryTranslation` entity. +In order to use the Translatable trait and interface, you will have to create this `CategoryTranslation` entity. ``` php proxyCurrentLocaleTranslation($method, $arguments); } - + // or do it with PropertyAccessor that ships with Symfony SE // if your methods don't take any required arguments public function __call($method, $arguments) @@ -478,7 +490,7 @@ parameters: ``` `datetimetz` here is a useful one to use if you are working with a Postgres database, otherwise you may encounter some -timezone issues. For more information on this see: +timezone issues. For more information on this see: http://doctrine-dbal.readthedocs.org/en/latest/reference/known-vendor-issues.html#datetime-datetimetz-and-time-types The default type is `datetime`. diff --git a/config/orm-services.yml b/config/orm-services.yml index c379010c..aa7715e3 100755 --- a/config/orm-services.yml +++ b/config/orm-services.yml @@ -4,8 +4,8 @@ parameters: knp.doctrine_behaviors.translatable_subscriber.class: Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber knp.doctrine_behaviors.translatable_subscriber.current_locale_callable.class: Knp\DoctrineBehaviors\ORM\Translatable\CurrentLocaleCallable knp.doctrine_behaviors.translatable_subscriber.default_locale_callable.class: Knp\DoctrineBehaviors\ORM\Translatable\DefaultLocaleCallable - knp.doctrine_behaviors.translatable_subscriber.translatable_trait: Knp\DoctrineBehaviors\Model\Translatable\Translatable - knp.doctrine_behaviors.translatable_subscriber.translation_trait: Knp\DoctrineBehaviors\Model\Translatable\Translation + knp.doctrine_behaviors.translatable_subscriber.translatable_traits: [ Knp\DoctrineBehaviors\Model\Translatable\Translatable, Knp\DoctrineBehaviors\Model\Translatable\TranslatableProperties ] + knp.doctrine_behaviors.translatable_subscriber.translation_traits: [ Knp\DoctrineBehaviors\Model\Translatable\Translation, Knp\DoctrineBehaviors\Model\Translatable\TranslationProperties ] knp.doctrine_behaviors.translatable_subscriber.translatable_fetch_method: LAZY knp.doctrine_behaviors.translatable_subscriber.translation_fetch_method: LAZY knp.doctrine_behaviors.softdeletable_subscriber.class: Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletableSubscriber @@ -40,8 +40,8 @@ services: - "@knp.doctrine_behaviors.reflection.class_analyzer" - "@knp.doctrine_behaviors.translatable_subscriber.current_locale_callable" - "@knp.doctrine_behaviors.translatable_subscriber.default_locale_callable" - - "%knp.doctrine_behaviors.translatable_subscriber.translatable_trait%" - - "%knp.doctrine_behaviors.translatable_subscriber.translation_trait%" + - "%knp.doctrine_behaviors.translatable_subscriber.translatable_traits%" + - "%knp.doctrine_behaviors.translatable_subscriber.translation_traits%" - "%knp.doctrine_behaviors.translatable_subscriber.translatable_fetch_method%" - "%knp.doctrine_behaviors.translatable_subscriber.translation_fetch_method%" tags: @@ -154,4 +154,3 @@ services: - "%knp.doctrine_behaviors.sluggable_subscriber.sluggable_trait%" tags: - { name: doctrine.event_subscriber } - diff --git a/src/Model/Translatable/TranslatableInterface.php b/src/Model/Translatable/TranslatableInterface.php new file mode 100644 index 00000000..48db22c5 --- /dev/null +++ b/src/Model/Translatable/TranslatableInterface.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Knp\DoctrineBehaviors\Model\Translatable; + +/** + * Translatable interface. + * + * Should be used to tag entities, that needs to be translated. + */ +interface TranslatableInterface +{ + /** + * Returns collection of translations. + * + * @return ArrayCollection + */ + public function getTranslations(); + + /** + * Returns collection of new translations. + * + * @return ArrayCollection + */ + public function getNewTranslations(); + + /** + * Adds new translation. + * + * @param TranslationInterface $translation The translation + * + * @return $this + */ + public function addTranslation(TranslationInterface $translation); + + /** + * Removes specific translation. + * + * @param TranslationInterface $translation The translation + */ + public function removeTranslation(TranslationInterface $translation); + + /** + * Returns translation for specific locale (creates new one if doesn't exists). + * If requested translation doesn't exist, it will first try to fallback default locale + * If any translation doesn't exist, it will be added to newTranslations collection. + * In order to persist new translations, call mergeNewTranslations method, before flush + * + * @param string $locale The locale (en, ru, fr) | null If null, will try with current locale + * @param bool $fallbackToDefault Whether fallback to default locale + * + * @return TranslationInterface + */ + public function translate($locale = null, $fallbackToDefault = true); + + /** + * Merges newly created translations into persisted translations. + */ + public function mergeNewTranslations(); + + /** + * @param mixed $locale the current locale + */ + public function setCurrentLocale($locale); + + /** + * @return mixed Returns the current locale + */ + public function getCurrentLocale(); + + /** + * @param mixed $locale the default locale + */ + public function setDefaultLocale($locale); + + /** + * @return mixed Returns the default locale + */ + public function getDefaultLocale(); + + /** + * Returns translation entity class name. + * + * @return string + */ + public static function getTranslationEntityClass(); +} diff --git a/src/Model/Translatable/TranslatableMethods.php b/src/Model/Translatable/TranslatableMethods.php index dc32363d..00dd2ac1 100644 --- a/src/Model/Translatable/TranslatableMethods.php +++ b/src/Model/Translatable/TranslatableMethods.php @@ -43,11 +43,11 @@ public function getNewTranslations() /** * Adds new translation. * - * @param Translation $translation The translation + * @param TranslationInterface $translation The translation * * @return $this */ - public function addTranslation($translation) + public function addTranslation(TranslationInterface $translation) { $this->getTranslations()->set((string)$translation->getLocale(), $translation); $translation->setTranslatable($this); @@ -58,9 +58,9 @@ public function addTranslation($translation) /** * Removes specific translation. * - * @param Translation $translation The translation + * @param TranslationInterface $translation The translation */ - public function removeTranslation($translation) + public function removeTranslation(TranslationInterface $translation) { $this->getTranslations()->removeElement($translation); } @@ -74,7 +74,7 @@ public function removeTranslation($translation) * @param string $locale The locale (en, ru, fr) | null If null, will try with current locale * @param bool $fallbackToDefault Whether fallback to default locale * - * @return Translation + * @return TranslationInterface */ public function translate($locale = null, $fallbackToDefault = true) { @@ -90,7 +90,7 @@ public function translate($locale = null, $fallbackToDefault = true) * @param string $locale The locale (en, ru, fr) | null If null, will try with current locale * @param bool $fallbackToDefault Whether fallback to default locale * - * @return Translation + * @return TranslationInterface */ protected function doTranslate($locale = null, $fallbackToDefault = true) { @@ -171,10 +171,10 @@ public function getDefaultLocale() /** * An extra feature allows you to proxy translated fields of a translatable entity. - * + * * @param string $method * @param array $arguments - * + * * @return mixed The translated value of the field for current locale */ protected function proxyCurrentLocaleTranslation($method, array $arguments = []) @@ -201,7 +201,7 @@ public static function getTranslationEntityClass() * @param string $locale The locale (en, ru, fr) * @param bool $withNewTranslations searched in new translations too * - * @return Translation|null + * @return TranslationInterface|null */ protected function findTranslationByLocale($locale, $withNewTranslations = true) { diff --git a/src/Model/Translatable/TranslationInterface.php b/src/Model/Translatable/TranslationInterface.php new file mode 100644 index 00000000..e61052e3 --- /dev/null +++ b/src/Model/Translatable/TranslationInterface.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Knp\DoctrineBehaviors\Model\Translatable; + +/** + * Translation interface. + * + * Should be used to tag translation entities. + */ +interface TranslationInterface +{ + /** + * Returns the translatable entity class name. + * + * @return string + */ + public static function getTranslatableEntityClass(); + + /** + * Returns object id. + * + * @return mixed + */ + public function getId(); + + /** + * Sets entity, that this translation should be mapped to. + * + * @param TranslatableInterface $translatable The translatable + * + * @return $this + */ + public function setTranslatable(TranslatableInterface $translatable); + + /** + * Returns entity, that this translation is mapped to. + * + * @return TranslatableInterface + */ + public function getTranslatable(); + + /** + * Sets locale name for this translation. + * + * @param string $locale The locale + * + * @return $this + */ + public function setLocale($locale); + + /** + * Returns this translation locale. + * + * @return string + */ + public function getLocale(); + + /** + * Tells if translation is empty + * + * @return bool true if translation is not filled + */ + public function isEmpty(); +} diff --git a/src/Model/Translatable/TranslationMethods.php b/src/Model/Translatable/TranslationMethods.php index 7b60ff73..ac3987c3 100644 --- a/src/Model/Translatable/TranslationMethods.php +++ b/src/Model/Translatable/TranslationMethods.php @@ -34,18 +34,19 @@ public static function getTranslatableEntityClass() * * @return mixed */ - public function getId() { + public function getId() + { return $this->id; } /** * Sets entity, that this translation should be mapped to. * - * @param Translatable $translatable The translatable + * @param TranslatableInterface $translatable The translatable * * @return $this */ - public function setTranslatable($translatable) + public function setTranslatable(TranslatableInterface $translatable) { $this->translatable = $translatable; diff --git a/src/ORM/Translatable/TranslatableSubscriber.php b/src/ORM/Translatable/TranslatableSubscriber.php index 18a5af12..8daeab5c 100644 --- a/src/ORM/Translatable/TranslatableSubscriber.php +++ b/src/ORM/Translatable/TranslatableSubscriber.php @@ -36,23 +36,23 @@ class TranslatableSubscriber extends AbstractSubscriber { private $currentLocaleCallable; private $defaultLocaleCallable; - private $translatableTrait; - private $translationTrait; + private $translatableTraits; + private $translationTraits; private $translatableFetchMode; private $translationFetchMode; public function __construct(ClassAnalyzer $classAnalyzer, callable $currentLocaleCallable = null, - callable $defaultLocaleCallable = null,$translatableTrait, $translationTrait, - $translatableFetchMode, $translationFetchMode) + callable $defaultLocaleCallable = null, $translatableTraits, + $translationTraits, $translatableFetchMode, $translationFetchMode) { parent::__construct($classAnalyzer, false); $this->currentLocaleCallable = $currentLocaleCallable; $this->defaultLocaleCallable = $defaultLocaleCallable; - $this->translatableTrait = $translatableTrait; - $this->translationTrait = $translationTrait; + $this->translatableTraits = is_array($translatableTraits) ? $translatableTraits : array($translatableTraits); + $this->translationTraits = is_array($translationTraits) ? $translationTraits : array($translationTraits); $this->translatableFetchMode = $this->convertFetchString($translatableFetchMode); - $this->translationFetchMode = $this->convertFetchString($translationFetchMode); + $this->translationFetchMode = $this->convertFetchString($translationFetchMode); } /** @@ -68,11 +68,11 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) return; } - if ($this->isTranslatable($classMetadata)) { + if ($this->requiresTranslatableMapping($classMetadata)) { $this->mapTranslatable($classMetadata); } - if ($this->isTranslation($classMetadata)) { + if ($this->requiresTranslationMapping($classMetadata)) { $this->mapTranslation($classMetadata); $this->mapId( $classMetadata, @@ -277,15 +277,21 @@ private function hasUniqueTranslationConstraint(ClassMetadata $classMetadata, $n } /** - * Checks if entity is translatable + * Checks if entity is translatable and should be provided with ORM Mapping * * @param ClassMetadata $classMetadata * * @return boolean */ - private function isTranslatable(ClassMetadata $classMetadata) + private function requiresTranslatableMapping(ClassMetadata $classMetadata) { - return $this->getClassAnalyzer()->hasTrait($classMetadata->reflClass, $this->translatableTrait); + foreach ($this->translatableTraits as $trait) { + if ($this->getClassAnalyzer()->hasTrait($classMetadata->reflClass, $trait)) { + return true; + } + } + + return false; } /** @@ -295,9 +301,15 @@ private function isTranslatable(ClassMetadata $classMetadata) * * @return boolean */ - private function isTranslation(ClassMetadata $classMetadata) + private function requiresTranslationMapping(ClassMetadata $classMetadata) { - return $this->getClassAnalyzer()->hasTrait($classMetadata->reflClass, $this->translationTrait); + foreach ($this->translationTraits as $trait) { + if ($this->getClassAnalyzer()->hasTrait($classMetadata->reflClass, $trait)) { + return true; + } + } + + return false; } public function postLoad(LifecycleEventArgs $eventArgs) @@ -312,11 +324,9 @@ public function prePersist(LifecycleEventArgs $eventArgs) private function setLocales(LifecycleEventArgs $eventArgs) { - $em = $eventArgs->getEntityManager(); - $entity = $eventArgs->getEntity(); - $classMetadata = $em->getClassMetadata(get_class($entity)); + $entity = $eventArgs->getEntity(); - if (!$this->getClassAnalyzer()->hasMethod($classMetadata->reflClass, 'setCurrentLocale')) { + if (!$entity instanceof TranslatableInterface) { return; } diff --git a/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php b/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php index 59a4a864..62e9dfed 100644 --- a/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php +++ b/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php @@ -34,8 +34,8 @@ function() { return 'en'; }, - 'Knp\DoctrineBehaviors\Model\Translatable\Translatable', - 'Knp\DoctrineBehaviors\Model\Translatable\Translation', + array('Knp\DoctrineBehaviors\Model\Translatable\Translatable', 'Knp\DoctrineBehaviors\Model\Translatable\TranslatableProperties'), + array('Knp\DoctrineBehaviors\Model\Translatable\Translation','Knp\DoctrineBehaviors\Model\Translatable\TranslationProperties',), 'LAZY', 'LAZY' )); diff --git a/tests/fixtures/BehaviorFixtures/ORM/TranslatableCustomizedEntity.php b/tests/fixtures/BehaviorFixtures/ORM/TranslatableCustomizedEntity.php index 1e35a184..421ce5a8 100644 --- a/tests/fixtures/BehaviorFixtures/ORM/TranslatableCustomizedEntity.php +++ b/tests/fixtures/BehaviorFixtures/ORM/TranslatableCustomizedEntity.php @@ -9,7 +9,7 @@ * @ORM\Entity * Used to test translation classes which declare custom translatable classes. */ -class TranslatableCustomizedEntity +class TranslatableCustomizedEntity implements Model\Translatable\TranslatableInterface { use Model\Translatable\Translatable; diff --git a/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntity.php b/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntity.php index 0e02e44c..04183cab 100644 --- a/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntity.php +++ b/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntity.php @@ -8,7 +8,7 @@ /** * @ORM\Entity */ -class TranslatableEntity +class TranslatableEntity implements Model\Translatable\TranslatableInterface { use Model\Translatable\Translatable; diff --git a/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntityTranslation.php b/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntityTranslation.php index 882574e1..4febaa0b 100644 --- a/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntityTranslation.php +++ b/tests/fixtures/BehaviorFixtures/ORM/TranslatableEntityTranslation.php @@ -8,7 +8,7 @@ /** * @ORM\Entity */ -class TranslatableEntityTranslation +class TranslatableEntityTranslation implements Model\Translatable\TranslationInterface { use Model\Translatable\Translation; diff --git a/tests/fixtures/BehaviorFixtures/ORM/Translation/TranslatableCustomizedEntityTranslation.php b/tests/fixtures/BehaviorFixtures/ORM/Translation/TranslatableCustomizedEntityTranslation.php index 5e0c8ed8..7153aa77 100644 --- a/tests/fixtures/BehaviorFixtures/ORM/Translation/TranslatableCustomizedEntityTranslation.php +++ b/tests/fixtures/BehaviorFixtures/ORM/Translation/TranslatableCustomizedEntityTranslation.php @@ -9,7 +9,7 @@ * @ORM\Entity * Used to test translatable classes which declare a custom translation class. */ -class TranslatableCustomizedEntityTranslation +class TranslatableCustomizedEntityTranslation implements Model\Translatable\TranslationInterface { use Model\Translatable\Translation; From 712cbd9e08b5b672ba74b13e12f4011e69e5eee7 Mon Sep 17 00:00:00 2001 From: Albin Kerouanton Date: Tue, 8 May 2018 22:46:19 +0200 Subject: [PATCH 2/2] Get rid of trait detection for Translatable behavior --- config/orm-services.yml | 4 -- .../Translatable/TranslatableSubscriber.php | 38 ++++++------------- .../ORM/TranslatableTest.php | 12 +----- 3 files changed, 13 insertions(+), 41 deletions(-) diff --git a/config/orm-services.yml b/config/orm-services.yml index aa7715e3..ec958605 100755 --- a/config/orm-services.yml +++ b/config/orm-services.yml @@ -4,8 +4,6 @@ parameters: knp.doctrine_behaviors.translatable_subscriber.class: Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber knp.doctrine_behaviors.translatable_subscriber.current_locale_callable.class: Knp\DoctrineBehaviors\ORM\Translatable\CurrentLocaleCallable knp.doctrine_behaviors.translatable_subscriber.default_locale_callable.class: Knp\DoctrineBehaviors\ORM\Translatable\DefaultLocaleCallable - knp.doctrine_behaviors.translatable_subscriber.translatable_traits: [ Knp\DoctrineBehaviors\Model\Translatable\Translatable, Knp\DoctrineBehaviors\Model\Translatable\TranslatableProperties ] - knp.doctrine_behaviors.translatable_subscriber.translation_traits: [ Knp\DoctrineBehaviors\Model\Translatable\Translation, Knp\DoctrineBehaviors\Model\Translatable\TranslationProperties ] knp.doctrine_behaviors.translatable_subscriber.translatable_fetch_method: LAZY knp.doctrine_behaviors.translatable_subscriber.translation_fetch_method: LAZY knp.doctrine_behaviors.softdeletable_subscriber.class: Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletableSubscriber @@ -40,8 +38,6 @@ services: - "@knp.doctrine_behaviors.reflection.class_analyzer" - "@knp.doctrine_behaviors.translatable_subscriber.current_locale_callable" - "@knp.doctrine_behaviors.translatable_subscriber.default_locale_callable" - - "%knp.doctrine_behaviors.translatable_subscriber.translatable_traits%" - - "%knp.doctrine_behaviors.translatable_subscriber.translation_traits%" - "%knp.doctrine_behaviors.translatable_subscriber.translatable_fetch_method%" - "%knp.doctrine_behaviors.translatable_subscriber.translation_fetch_method%" tags: diff --git a/src/ORM/Translatable/TranslatableSubscriber.php b/src/ORM/Translatable/TranslatableSubscriber.php index 8daeab5c..cc95505c 100644 --- a/src/ORM/Translatable/TranslatableSubscriber.php +++ b/src/ORM/Translatable/TranslatableSubscriber.php @@ -11,6 +11,8 @@ namespace Knp\DoctrineBehaviors\ORM\Translatable; +use Knp\DoctrineBehaviors\Model\Translatable\TranslatableInterface; +use Knp\DoctrineBehaviors\Model\Translatable\TranslationInterface; use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer; use Knp\DoctrineBehaviors\ORM\AbstractSubscriber; @@ -36,21 +38,15 @@ class TranslatableSubscriber extends AbstractSubscriber { private $currentLocaleCallable; private $defaultLocaleCallable; - private $translatableTraits; - private $translationTraits; private $translatableFetchMode; private $translationFetchMode; - public function __construct(ClassAnalyzer $classAnalyzer, callable $currentLocaleCallable = null, - callable $defaultLocaleCallable = null, $translatableTraits, - $translationTraits, $translatableFetchMode, $translationFetchMode) + public function __construct(ClassAnalyzer $classAnalyzer, callable $currentLocaleCallable = null, callable $defaultLocaleCallable = null, $translatableFetchMode, $translationFetchMode) { parent::__construct($classAnalyzer, false); $this->currentLocaleCallable = $currentLocaleCallable; $this->defaultLocaleCallable = $defaultLocaleCallable; - $this->translatableTraits = is_array($translatableTraits) ? $translatableTraits : array($translatableTraits); - $this->translationTraits = is_array($translationTraits) ? $translationTraits : array($translationTraits); $this->translatableFetchMode = $this->convertFetchString($translatableFetchMode); $this->translationFetchMode = $this->convertFetchString($translationFetchMode); } @@ -68,11 +64,11 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) return; } - if ($this->requiresTranslatableMapping($classMetadata)) { + if ($this->isTranslatable($classMetadata)) { $this->mapTranslatable($classMetadata); } - if ($this->requiresTranslationMapping($classMetadata)) { + if ($this->isTranslation($classMetadata)) { $this->mapTranslation($classMetadata); $this->mapId( $classMetadata, @@ -279,37 +275,25 @@ private function hasUniqueTranslationConstraint(ClassMetadata $classMetadata, $n /** * Checks if entity is translatable and should be provided with ORM Mapping * - * @param ClassMetadata $classMetadata + * @param ClassMetadata $metadata * * @return boolean */ - private function requiresTranslatableMapping(ClassMetadata $classMetadata) + private function isTranslatable(ClassMetadata $metadata) { - foreach ($this->translatableTraits as $trait) { - if ($this->getClassAnalyzer()->hasTrait($classMetadata->reflClass, $trait)) { - return true; - } - } - - return false; + return is_subclass_of($metadata->getName(), TranslatableInterface::class); } /** * Checks if entity is a translation * - * @param ClassMetadata $classMetadata + * @param ClassMetadata $metadata * * @return boolean */ - private function requiresTranslationMapping(ClassMetadata $classMetadata) + private function isTranslation(ClassMetadata $metadata) { - foreach ($this->translationTraits as $trait) { - if ($this->getClassAnalyzer()->hasTrait($classMetadata->reflClass, $trait)) { - return true; - } - } - - return false; + return is_subclass_of($metadata->getName(), TranslationInterface::class); } public function postLoad(LifecycleEventArgs $eventArgs) diff --git a/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php b/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php index 62e9dfed..45fb99fa 100644 --- a/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php +++ b/tests/Knp/DoctrineBehaviors/ORM/TranslatableTest.php @@ -26,16 +26,8 @@ protected function getEventManager() $em->addEventSubscriber(new \Knp\DoctrineBehaviors\ORM\Translatable\TranslatableSubscriber( new ClassAnalyzer(), - function() - { - return 'en'; - }, - function() - { - return 'en'; - }, - array('Knp\DoctrineBehaviors\Model\Translatable\Translatable', 'Knp\DoctrineBehaviors\Model\Translatable\TranslatableProperties'), - array('Knp\DoctrineBehaviors\Model\Translatable\Translation','Knp\DoctrineBehaviors\Model\Translatable\TranslationProperties',), + function() { return 'en'; }, + function() { return 'en'; }, 'LAZY', 'LAZY' ));