From 706ae628888980e9a2451da062101e2a29f5830a Mon Sep 17 00:00:00 2001 From: Bozhidar Hristov Date: Mon, 3 Apr 2017 15:49:08 +0300 Subject: [PATCH] Separate listener from service --- src/CurrentTranslationLoader.php | 123 ++++++++++++++++++ .../CurrentTranslationLoader.php | 99 +------------- src/Resources/config/services.xml | 6 +- 3 files changed, 132 insertions(+), 96 deletions(-) create mode 100644 src/CurrentTranslationLoader.php diff --git a/src/CurrentTranslationLoader.php b/src/CurrentTranslationLoader.php new file mode 100644 index 0000000..86aa518 --- /dev/null +++ b/src/CurrentTranslationLoader.php @@ -0,0 +1,123 @@ +container = $container; + $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); + } + + /** + * @param $trueFalse + */ + public function doFallback($trueFalse) + { + $this->fallback = $trueFalse; + } + + /** + * Reinitialize all current translations of all managed entities + */ + public function flush() + { + foreach ($this->managedEntities as $entity) { + $this->initializeCurrentTranslation($entity); + } + } + + /** + * @param TranslatableInterface $entity + */ + public function initializeCurrentTranslation(TranslatableInterface $entity) + { + $translationService = $this->container->get('object_bg.translation.service.translation'); + $CurrentLanguage = $translationService->getCurrentLanguage(); + $success = $this->initializeTranslation($entity, $CurrentLanguage); + + if ($success == false && $this->fallback === true) { + $this->initializeFallbackTranslation($entity); + } + } + + /** + * @param TranslatableInterface $entity + */ + private function initializeFallbackTranslation(TranslatableInterface $entity) + { + $translationService = $this->container->get('object_bg.translation.service.translation'); + $fallbackLocales = $translationService->getFallbackLocales(); + + foreach ($fallbackLocales as $fallbackLocale) { + if ($this->initializeTranslation($entity, $fallbackLocale)) { + break; + } + } + } + + /** + * @param TranslatableInterface $entity + * @param $languageOrLocale + * @return bool + */ + public function initializeTranslation(TranslatableInterface $entity, $languageOrLocale) + { + $oid = spl_object_hash($entity); + $this->managedEntities[$oid] = $entity; + + $translationService = $this->container->get('object_bg.translation.service.translation'); + + $translations = $this->propertyAccessor->getValue($entity, $translationService->getTranslationsField($entity)); + + if (!$translations) { + return false; + } + $propertyAccessor = $this->propertyAccessor; + + $currentTranslation = $translations->filter( + function ($item) use ($translationService, $languageOrLocale, $propertyAccessor) { + $translationLanguage = $propertyAccessor->getValue($item, $translationService->getLanguageField($item)); + + if ($languageOrLocale instanceof Language) { + return $translationLanguage == $languageOrLocale; + } else { + return $translationLanguage->getLocale() == $languageOrLocale; + } + } + )->first(); + + if (!$currentTranslation) { + return false; + } + $currentTranslationField = $translationService->getCurrentTranslationField($entity); + $this->propertyAccessor->setValue($entity, $currentTranslationField, $currentTranslation); + + return true; + } +} diff --git a/src/EventListener/CurrentTranslationLoader.php b/src/EventListener/CurrentTranslationLoader.php index 49f71cc..0776398 100644 --- a/src/EventListener/CurrentTranslationLoader.php +++ b/src/EventListener/CurrentTranslationLoader.php @@ -3,43 +3,19 @@ namespace ObjectBG\TranslationBundle\EventListener; use Doctrine\Common\EventSubscriber; -use ObjectBG\TranslationBundle\Entity\Language; use ObjectBG\TranslationBundle\TranslatableInterface; use Symfony\Component\DependencyInjection\Container; -use Symfony\Component\PropertyAccess\PropertyAccess; -use Symfony\Component\PropertyAccess\PropertyAccessor; class CurrentTranslationLoader implements EventSubscriber { - /** * @var Container */ private $container; - /** - * @var PropertyAccessor - */ - private $propertyAccessor; - - /** - * @var bool - */ - private $fallback = true; - /** - * @var array - */ - private $managed = []; - public function __construct(Container $container) { $this->container = $container; - $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); - } - - public function doFallback($trueFalse) - { - $this->fallback = $trueFalse; } public function getSubscribedEvents() @@ -47,81 +23,14 @@ public function getSubscribedEvents() return array('postLoad'); } - public function flush() - { - foreach ($this->managed as $entity) { - $this->initializeCurrentTranslation($entity); - } - } - public function postLoad($event) { - $Entity = $event->getEntity(); - if (!$Entity instanceof TranslatableInterface) { - return; - } - - $this->initializeCurrentTranslation($Entity); - } - - public function initializeCurrentTranslation($entity) - { - $translationService = $this->container->get('object_bg.translation.service.translation'); - $CurrentLanguage = $translationService->getCurrentLanguage(); - $success = $this->initializeTranslation($entity, $CurrentLanguage); - - if ($success == false && $this->fallback === true) { - $this->initializeFallbackTranslation($entity); - } - } - - private function initializeFallbackTranslation($entity) - { - $translationService = $this->container->get('object_bg.translation.service.translation'); - $fallbacks = $translationService->getFallbackLocales(); - - foreach ($fallbacks as $fallback) { - if ($this->initializeTranslation($entity, $fallback)) { - break; - } - } - } - - public function initializeTranslation($entity, $languageOrLocale) - { + $entity = $event->getEntity(); if (!$entity instanceof TranslatableInterface) { - throw new \RuntimeException('Entity is not translatable'); - } - $oid = spl_object_hash($entity); - $this->managed[$oid] = $entity; - - $translationService = $this->container->get('object_bg.translation.service.translation'); - - $translations = $this->propertyAccessor->getValue($entity, $translationService->getTranslationsField($entity)); - - if (!$translations) { - return false; - } - $propertyAccessor = $this->propertyAccessor; - - $currentTranslation = $translations->filter( - function ($item) use ($translationService, $languageOrLocale, $propertyAccessor) { - $translationLanguage = $propertyAccessor->getValue($item, $translationService->getLanguageField($item)); - - if ($languageOrLocale instanceof Language) { - return $translationLanguage == $languageOrLocale; - } else { - return $translationLanguage->getLocale() == $languageOrLocale; - } - } - )->first(); - - if (!$currentTranslation) { - return false; + return; } - $currentTranslationField = $translationService->getCurrentTranslationField($entity); - $this->propertyAccessor->setValue($entity, $currentTranslationField, $currentTranslation); - return true; + $loader = $this->container->get('object_bg.translation.current_translation_loader'); + $loader->initializeCurrentTranslation($entity); } } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index f4e7c30..b09969d 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -24,7 +24,11 @@ - + + + + +