1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\AutoFallbackTranslationBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Happyr\AutoFallbackTranslationBundle\Service\GoogleTranslator; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Tobias Nyholm <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class HappyrAutoFallbackTranslationExtension extends Extension |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param array $configs |
20
|
|
|
* @param ContainerBuilder $container |
21
|
|
|
*/ |
22
|
|
|
public function load(array $configs, ContainerBuilder $container) |
23
|
|
|
{ |
24
|
|
|
$configuration = new Configuration(); |
25
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
26
|
|
|
|
27
|
|
|
if (!$config['enabled']) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
32
|
|
|
$loader->load('services.yml'); |
33
|
|
|
|
34
|
|
|
switch ($config['translation_service']) { |
35
|
|
|
case 'google': |
36
|
|
|
$translatorServiceDef = $container->register('happyr.translation.auto.service', GoogleTranslator::class); |
37
|
|
|
$translatorServiceDef->addArgument($config['google_key']); |
38
|
|
|
break; |
39
|
|
|
default: |
40
|
|
|
throw new \RuntimeException('You must choose a translation service for AutoFallbackTranslatorBundle.'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$translatorServiceDef |
44
|
|
|
->addMethodCall('setHttpClient', [new Reference($config['http_client'])]) |
45
|
|
|
->addMethodCall('setMessageFactory', [new Reference($config['message_factory'])]) |
46
|
|
|
->addMethodCall('setLogger', [new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)]); |
47
|
|
|
|
48
|
|
|
$container->findDefinition('happyr.translation.auto_fallback_translator') |
49
|
|
|
->replaceArgument(0, $config['default_locale']) |
50
|
|
|
->replaceArgument(2, $translatorServiceDef) |
51
|
|
|
->setDecoratedService('translator', null, 10); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|