|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Happyr\TranslationBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
10
|
|
|
|
|
11
|
|
|
class HappyrTranslationExtension extends Extension |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param array $configs |
|
15
|
|
|
* @param ContainerBuilder $container |
|
16
|
|
|
*/ |
|
17
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$configuration = new Configuration(); |
|
20
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
21
|
1 |
|
$this->copyValuesFromParentToProject('locales', $config); |
|
22
|
1 |
|
$this->copyValuesFromParentToProject('domains', $config); |
|
23
|
|
|
|
|
24
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
25
|
1 |
|
$loader->load('services.yml'); |
|
26
|
|
|
|
|
27
|
1 |
|
if ($config['auto_add_assets']) { |
|
28
|
|
|
$loader->load('autoAdd.yml'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
$container->setParameter('translation.toolbar.allow_edit', $config['allow_edit']); |
|
32
|
|
|
|
|
33
|
1 |
|
$targetDir = rtrim($config['target_dir'], '/'); |
|
34
|
1 |
|
$container->findDefinition('happyr.translation.filesystem') |
|
35
|
1 |
|
->replaceArgument(2, $targetDir) |
|
36
|
1 |
|
->replaceArgument(3, $config['file_extension']) |
|
37
|
1 |
|
->replaceArgument(4, $config['sync_empty_translations']); |
|
38
|
|
|
|
|
39
|
1 |
|
$this->configureLoaderAndDumper($container, $config['file_extension']); |
|
40
|
|
|
|
|
41
|
1 |
|
$container->getDefinition('happyr.translation.request_manager') |
|
42
|
1 |
|
->replaceArgument(0, new Reference($config['httplug_client'])) |
|
43
|
1 |
|
->replaceArgument(1, new Reference($config['httplug_message_factory'])); |
|
44
|
|
|
|
|
45
|
|
|
/* |
|
46
|
|
|
* Set alias for the translation service |
|
47
|
|
|
*/ |
|
48
|
1 |
|
$container->setAlias('happyr.translation', 'happyr.translation.service.'.$config['translation_service']); |
|
49
|
|
|
|
|
50
|
1 |
|
$container->findDefinition('happyr.translation.service.loco') |
|
51
|
1 |
|
->replaceArgument(3, $config['projects']); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Copy the parent configuration to the children. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @param array $config |
|
59
|
|
|
*/ |
|
60
|
1 |
|
private function copyValuesFromParentToProject($key, array &$config) |
|
61
|
|
|
{ |
|
62
|
1 |
|
if (empty($config[$key])) { |
|
63
|
1 |
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
foreach ($config['projects'] as &$project) { |
|
67
|
|
|
if (empty($project[$key])) { |
|
68
|
|
|
$project[$key] = $config[$key]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param ContainerBuilder $container |
|
75
|
|
|
* @param string $fileExtension |
|
76
|
|
|
*/ |
|
77
|
1 |
|
protected function configureLoaderAndDumper(ContainerBuilder $container, $fileExtension) |
|
78
|
|
|
{ |
|
79
|
|
|
switch ($fileExtension) { |
|
80
|
1 |
|
case 'xlf': |
|
81
|
1 |
|
$fileExtension = 'xliff'; |
|
82
|
1 |
|
break; |
|
83
|
|
|
case 'yml': |
|
84
|
|
|
$fileExtension = 'yaml'; |
|
85
|
|
|
break; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$loader = $container->register('happyr.translation.loader', sprintf('Symfony\Component\Translation\Loader\%sFileLoader', ucfirst($fileExtension))); |
|
89
|
1 |
|
$loader->addTag('translation.loader', ['alias' => $fileExtension]); |
|
90
|
|
|
|
|
91
|
1 |
|
$dumper = $container->register('happyr.translation.dumper', sprintf('Symfony\Component\Translation\Dumper\%sFileDumper', ucfirst($fileExtension))); |
|
92
|
1 |
|
$dumper->addTag('translation.dumper', ['alias' => $fileExtension]); |
|
93
|
1 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|