Skip to content

Commit

Permalink
Merge pull request #8 from Warxcell/on_change_listener
Browse files Browse the repository at this point in the history
On change listener
  • Loading branch information
Warxcell authored Apr 3, 2017
2 parents 32d9e94 + 706ae62 commit 3713be8
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 84 deletions.
123 changes: 123 additions & 0 deletions src/CurrentTranslationLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace ObjectBG\TranslationBundle;

use ObjectBG\TranslationBundle\Entity\Language;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class CurrentTranslationLoader
{
/**
* @var Container
*/
private $container;

/**
* @var PropertyAccessor
*/
private $propertyAccessor;

/**
* @var bool
*/
private $fallback = true;
/**
* @var array
*/
private $managedEntities = [];

public function __construct(Container $container)
{
$this->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;
}
}
88 changes: 5 additions & 83 deletions src/EventListener/CurrentTranslationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,112 +3,34 @@
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;

public function __construct(Container $container)
{
$this->container = $container;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}

public function doFallback($trueFalse)
{
$this->fallback = $trueFalse;
}

public function getSubscribedEvents()
{
return array('postLoad');
}

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)
public function postLoad($event)
{
$entity = $event->getEntity();
if (!$entity instanceof TranslatableInterface) {
throw new \RuntimeException('Entity is not translatable');
}

$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 {
$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);
}
}
6 changes: 5 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
<argument type="service" id="object_bg.translation.service.translation" />
</service>

<service id="object_bg.translation.current_translation_loader" class="ObjectBG\TranslationBundle\EventListener\CurrentTranslationLoader">
<service id="object_bg.translation.current_translation_loader" class="ObjectBG\TranslationBundle\CurrentTranslationLoader">
<argument type="service" id="service_container" />
</service>

<service id="object_bg.translation.current_translation_loader_listener" class="ObjectBG\TranslationBundle\EventListener\CurrentTranslationLoader">
<tag name="doctrine.event_subscriber" />
<argument type="service" id="service_container" />
</service>
Expand Down
9 changes: 9 additions & 0 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
class Translator extends OriginalTranslator
{

public function setLocale($locale)
{
$return = parent::setLocale($locale);

$this->container->get('object_bg.translation.current_translation_loader')->flush();

return $return;
}

/**
* @param string $locale
*/
Expand Down

0 comments on commit 3713be8

Please sign in to comment.