From caf53b415c7adcbd0f890492279720eaa55a0c69 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 26 Dec 2016 18:31:10 +0100 Subject: [PATCH 1/4] Started on auto adder --- DependencyInjection/Configuration.php | 6 +++ DependencyInjection/TranslationExtension.php | 6 +++ EventListener/AutoAddMissingTranslations.php | 53 ++++++++++++++++++++ Resources/config/auto_add.yml | 6 +++ Service/StorageService.php | 32 +++++++++++- 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 EventListener/AutoAddMissingTranslations.php create mode 100644 Resources/config/auto_add.yml diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index a5d4c5a8..385096f2 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -65,6 +65,12 @@ public function getConfigTreeBuilder() ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end() ->end() ->end() + ->arrayNode('auto_add_missing_translations') + ->canBeEnabled() + ->children() + ->scalarNode('config_name')->defaultValue('default')->end() + ->end() + ->end() ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end() ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end() ->end(); diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index 08b56ada..2d8e5f74 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -61,6 +61,12 @@ public function load(array $configs, ContainerBuilder $container) $this->enableEditInPlace($container, $config); } + if ($config['auto_add_missing_translations']['enabled']) { + $loader->load('auto_add.yml'); + $container->getDefinition('php_translator.auto_adder') + ->replaceArgument(0, 'php_translation.storage.'.$config['auto_add_missing_translations']['config_name']); + } + if ($config['fallback_translation']['enabled']) { $loader->load('auto_translation.yml'); $this->enableFallbackAutoTranslator($container, $config); diff --git a/EventListener/AutoAddMissingTranslations.php b/EventListener/AutoAddMissingTranslations.php new file mode 100644 index 00000000..5cc9da52 --- /dev/null +++ b/EventListener/AutoAddMissingTranslations.php @@ -0,0 +1,53 @@ + + */ +class AutoAddMissingTranslations +{ + /** + * @var DataCollectorTranslator + */ + private $dataCollector; + + /** + * @var StorageService + */ + private $storage; + + /** + * + * @param DataCollectorTranslator $translator + * @param StorageService $storage + */ + public function __construct(StorageService $storage, DataCollectorTranslator $translator = null) + { + $this->dataCollector = $translator; + $this->storage = $storage; + + } + + public function onTerminate(Event $event) + { + if ($this->dataCollector === null) { + return; + } + + $messages = $this->dataCollector->getCollectedMessages(); + foreach ($messages as $message) { + if ($message['state'] === DataCollectorTranslator::MESSAGE_MISSING) { + $m = new Message($message); + $this->storage->create($m); + } + } + } +} diff --git a/Resources/config/auto_add.yml b/Resources/config/auto_add.yml new file mode 100644 index 00000000..612586c4 --- /dev/null +++ b/Resources/config/auto_add.yml @@ -0,0 +1,6 @@ +services: + php_translator.auto_adder: + class: Translation\Bundle\EventListener\AutoAddMissingTranslations + arguments: [ ~, '@?translator.data_collector' ] + tags: + - { name: kernel.event_listener, event: kernel.terminate, method: onTerminate, priority: 10 } diff --git a/Service/StorageService.php b/Service/StorageService.php index 93201218..9148d476 100644 --- a/Service/StorageService.php +++ b/Service/StorageService.php @@ -151,7 +151,37 @@ private function getFromStorages($storages, $locale, $domain, $key) } /** - * Update all configured storages with this message. + * Create all configured storages with this message. This will not overwrite + * existing message. + * + * {@inheritdoc} + */ + public function create(Message $message) + { + foreach ([$this->localStorages, $this->remoteStorages] as $storages) { + $this->createStorages($storages, $message); + } + } + + /** + * @param Storage[] $storages + * @param Message $message + */ + private function createStorages($storages, Message $message) + { + // Validate if message actually has data + if (empty((array) $message)) { + return; + } + + foreach ($storages as $storage) { + $storage->create($message); + } + } + + /** + * Update all configured storages with this message. If messages does not exist + * it will be created. * * {@inheritdoc} */ From aa2ffdb630ddcbb8f88675761c2f13a9f1b66013 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 26 Dec 2016 19:12:35 +0100 Subject: [PATCH 2/4] Complete the auto adder feature. --- DependencyInjection/TranslationExtension.php | 2 +- EventListener/AutoAddMissingTranslations.php | 2 +- Service/StorageService.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index 2d8e5f74..400b2bd7 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -64,7 +64,7 @@ public function load(array $configs, ContainerBuilder $container) if ($config['auto_add_missing_translations']['enabled']) { $loader->load('auto_add.yml'); $container->getDefinition('php_translator.auto_adder') - ->replaceArgument(0, 'php_translation.storage.'.$config['auto_add_missing_translations']['config_name']); + ->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name'])); } if ($config['fallback_translation']['enabled']) { diff --git a/EventListener/AutoAddMissingTranslations.php b/EventListener/AutoAddMissingTranslations.php index 5cc9da52..2eefc6b9 100644 --- a/EventListener/AutoAddMissingTranslations.php +++ b/EventListener/AutoAddMissingTranslations.php @@ -45,7 +45,7 @@ public function onTerminate(Event $event) $messages = $this->dataCollector->getCollectedMessages(); foreach ($messages as $message) { if ($message['state'] === DataCollectorTranslator::MESSAGE_MISSING) { - $m = new Message($message); + $m = new Message($message['id'], $message['domain'], $message['locale'], $message['translation']); $this->storage->create($m); } } diff --git a/Service/StorageService.php b/Service/StorageService.php index 9148d476..2af8f82d 100644 --- a/Service/StorageService.php +++ b/Service/StorageService.php @@ -175,7 +175,7 @@ private function createStorages($storages, Message $message) } foreach ($storages as $storage) { - $storage->create($message); + $storage->update($message); } } From 8a6f36059f3eef85c4f42d17124a7ac75c6aa5a9 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 26 Dec 2016 19:16:09 +0100 Subject: [PATCH 3/4] Style fix --- EventListener/AutoAddMissingTranslations.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/EventListener/AutoAddMissingTranslations.php b/EventListener/AutoAddMissingTranslations.php index 2eefc6b9..45600e18 100644 --- a/EventListener/AutoAddMissingTranslations.php +++ b/EventListener/AutoAddMissingTranslations.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Translation\Bundle\EventListener; use Translation\Common\Model\Message; @@ -8,8 +17,6 @@ use Translation\Bundle\Service\StorageService; /** - * - * * @author Tobias Nyholm */ class AutoAddMissingTranslations @@ -25,15 +32,13 @@ class AutoAddMissingTranslations private $storage; /** - * * @param DataCollectorTranslator $translator - * @param StorageService $storage + * @param StorageService $storage */ public function __construct(StorageService $storage, DataCollectorTranslator $translator = null) { $this->dataCollector = $translator; $this->storage = $storage; - } public function onTerminate(Event $event) From f216f20e845df29d7aed9635b4aee7ed1325bc45 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 26 Dec 2016 19:30:04 +0100 Subject: [PATCH 4/4] Require latest version of common --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5588b32e..57911fdd 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "symfony/translation": "^2.7 || ^3.0", "symfony/finder": "^2.7 || ^3.0", - "php-translation/common": "^0.1", + "php-translation/common": "^0.2", "php-translation/extractor": "^0.1.1" }, "require-dev": {