1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\Config\FileLocator; |
17
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
18
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
19
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
22
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
23
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
24
|
|
|
use Translation\Bundle\Model\Configuration as ConfigurationModel; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This is the class that loads and manages your bundle configuration. |
28
|
|
|
* |
29
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
30
|
|
|
*/ |
31
|
|
|
class TranslationExtension extends Extension |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
8 |
|
public function load(array $configs, ContainerBuilder $container) |
37
|
|
|
{ |
38
|
8 |
|
$configuration = new Configuration($container); |
39
|
8 |
|
$config = $this->processConfiguration($configuration, $configs); |
40
|
8 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
41
|
|
|
|
42
|
8 |
|
$loader->load('services.yml'); |
43
|
8 |
|
$loader->load('extractors.yml'); |
44
|
|
|
|
45
|
|
|
// Add major version to extractor |
46
|
8 |
|
$container->getDefinition('php_translation.extractor.php.visitor.FormTypeChoices') |
47
|
8 |
|
->addMethodCall('setSymfonyMajorVersion', [Kernel::MAJOR_VERSION]); |
48
|
|
|
|
49
|
8 |
|
$container->setParameter('php_translation.locales', $config['locales']); |
50
|
8 |
|
$container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale')); |
51
|
8 |
|
$this->handleConfigNode($container, $config); |
52
|
|
|
|
53
|
8 |
|
if ($config['webui']['enabled']) { |
54
|
1 |
|
$this->enableWebUi($container, $config); |
55
|
|
|
} else { |
56
|
7 |
|
$container->setParameter('php_translation.webui.enabled', false); |
57
|
|
|
} |
58
|
|
|
|
59
|
8 |
|
if ($config['symfony_profiler']['enabled']) { |
60
|
8 |
|
$loader->load('symfony_profiler.yml'); |
61
|
8 |
|
$this->enableSymfonyProfiler($container, $config); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
if ($config['edit_in_place']['enabled']) { |
65
|
1 |
|
$loader->load('edit_in_place.yml'); |
66
|
1 |
|
$this->enableEditInPlace($container, $config); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
if ($config['auto_add_missing_translations']['enabled']) { |
70
|
1 |
|
$loader->load('auto_add.yml'); |
71
|
1 |
|
$container->getDefinition('php_translator.auto_adder') |
72
|
1 |
|
->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name'])); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
if ($config['fallback_translation']['enabled']) { |
76
|
1 |
|
$loader->load('auto_translation.yml'); |
77
|
1 |
|
$this->enableFallbackAutoTranslator($container, $config); |
78
|
|
|
} |
79
|
|
|
|
80
|
8 |
|
if ('test' === getenv('ENV')) { |
81
|
8 |
|
$loader->load('services_test.yml'); |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
$loader->load('console.yml'); |
85
|
8 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Handle the config node to prepare the config manager. |
89
|
|
|
* |
90
|
|
|
* @param ContainerBuilder $container |
91
|
|
|
* @param array $config |
92
|
|
|
*/ |
93
|
8 |
|
private function handleConfigNode(ContainerBuilder $container, array $config) |
94
|
|
|
{ |
95
|
8 |
|
$storageManager = $container->getDefinition('php_translation.storage_manager'); |
96
|
8 |
|
$configurationManager = $container->getDefinition('php_translation.configuration_manager'); |
97
|
|
|
// $first will be the "default" configuration. |
98
|
8 |
|
$first = null; |
99
|
8 |
|
foreach ($config['configs'] as $name => &$c) { |
100
|
8 |
|
if (null === $first || 'default' === $name) { |
101
|
8 |
|
$first = $name; |
102
|
|
|
} |
103
|
8 |
|
if (empty($c['project_root'])) { |
104
|
|
|
// Add a project root of none is set. |
105
|
8 |
|
$c['project_root'] = dirname($container->getParameter('kernel.root_dir')); |
106
|
|
|
} |
107
|
8 |
|
$c['name'] = $name; |
108
|
8 |
|
$c['locales'] = $config['locales']; |
109
|
8 |
|
$configurationServiceId = 'php_translation.configuration.'.$name; |
110
|
8 |
|
$configDef = $container->register($configurationServiceId, ConfigurationModel::class); |
111
|
8 |
|
$configDef->setPublic(false)->addArgument($c); |
112
|
8 |
|
$configurationManager->addMethodCall('addConfiguration', [$name, new Reference($configurationServiceId)]); |
113
|
|
|
|
114
|
|
|
/* |
115
|
|
|
* Configure storage chain service |
116
|
|
|
*/ |
117
|
8 |
|
$storageDefinition = $this->createChildDefinition('php_translation.storage.abstract'); |
118
|
8 |
|
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId)); |
119
|
8 |
|
$storageDefinition->setPublic(true); |
120
|
8 |
|
$container->setDefinition('php_translation.storage.'.$name, $storageDefinition); |
121
|
8 |
|
$storageManager->addMethodCall('addStorage', [$name, new Reference('php_translation.storage.'.$name)]); |
122
|
|
|
|
123
|
|
|
// Add storages |
124
|
8 |
|
foreach ($c['remote_storage'] as $serviceId) { |
125
|
|
|
$storageDefinition->addMethodCall('addRemoteStorage', [new Reference($serviceId)]); |
126
|
|
|
} |
127
|
|
|
|
128
|
8 |
|
foreach ($c['local_storage'] as $serviceId) { |
129
|
8 |
|
if ('php_translation.local_file_storage.abstract' !== $serviceId) { |
130
|
|
|
$storageDefinition->addMethodCall('addLocalStorage', [new Reference($serviceId)]); |
131
|
|
|
|
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
8 |
|
$def = $this->createChildDefinition($serviceId); |
136
|
8 |
|
$def->replaceArgument(2, [$c['output_dir']]) |
137
|
8 |
|
->replaceArgument(3, $c['local_file_storage_options']) |
138
|
8 |
|
->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]); |
139
|
8 |
|
$container->setDefinition('php_translation.single_storage.file.'.$name, $def); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
8 |
|
if (null !== $first) { |
144
|
|
|
// Create some aliases for the default storage |
145
|
8 |
|
$container->setAlias('php_translation.storage', new Alias('php_translation.storage.'.$first, true)); |
146
|
8 |
|
if ('default' !== $first) { |
147
|
|
|
$container->setAlias('php_translation.storage.default', new Alias('php_translation.storage.'.$first, true)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
8 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Handle config for WebUI. |
154
|
|
|
* |
155
|
|
|
* @param ContainerBuilder $container |
156
|
|
|
* @param array $config |
157
|
|
|
*/ |
158
|
1 |
|
private function enableWebUi(ContainerBuilder $container, array $config) |
159
|
|
|
{ |
160
|
1 |
|
$container->setParameter('php_translation.webui.enabled', true); |
161
|
1 |
|
$container->setParameter('php_translation.webui.allow_create', $config['webui']['allow_create']); |
162
|
1 |
|
$container->setParameter('php_translation.webui.allow_delete', $config['webui']['allow_delete']); |
163
|
|
|
|
164
|
1 |
|
$path = $config['webui']['file_base_path']; |
165
|
1 |
|
if (null === $path) { |
166
|
1 |
|
if ($container->hasParameter('kernel.project_dir')) { |
167
|
|
|
$path = $container->getParameter('kernel.project_dir'); |
168
|
|
|
} else { |
169
|
1 |
|
$path = $container->getParameter('kernel.root_dir').'/..'; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
$container->setParameter('php_translation.webui.file_base_path', rtrim($path, '/').'/'); |
174
|
1 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Handle config for EditInPlace. |
178
|
|
|
* |
179
|
|
|
* @param ContainerBuilder $container |
180
|
|
|
* @param array $config |
181
|
|
|
*/ |
182
|
1 |
|
private function enableEditInPlace(ContainerBuilder $container, array $config) |
183
|
|
|
{ |
184
|
1 |
|
$name = $config['edit_in_place']['config_name']; |
185
|
|
|
|
186
|
1 |
|
if ('default' !== $name && !isset($config['configs'][$name])) { |
187
|
|
|
throw new InvalidArgumentException(sprintf('There is no config named "%s".', $name)); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
$activatorRef = new Reference($config['edit_in_place']['activator']); |
191
|
|
|
|
192
|
1 |
|
$def = $container->getDefinition('php_translation.edit_in_place.response_listener'); |
193
|
1 |
|
$def->replaceArgument(0, $activatorRef); |
194
|
1 |
|
$def->replaceArgument(3, $name); |
195
|
1 |
|
$def->replaceArgument(4, $config['edit_in_place']['show_untranslatable']); |
196
|
|
|
|
197
|
1 |
|
$def = $container->getDefinition('php_translator.edit_in_place.xtrans_html_translator'); |
198
|
1 |
|
$def->replaceArgument(1, $activatorRef); |
199
|
|
|
|
200
|
1 |
|
$def = $container->getDefinition('php_translation.edit_in_place.extension.trans'); |
201
|
1 |
|
$def->addMethodCall('setActivator', [$activatorRef]); |
202
|
1 |
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Handle config for Symfony Profiler. |
206
|
|
|
* |
207
|
|
|
* @param ContainerBuilder $container |
208
|
|
|
* @param array $config |
209
|
|
|
*/ |
210
|
8 |
|
private function enableSymfonyProfiler(ContainerBuilder $container, array $config) |
211
|
|
|
{ |
212
|
8 |
|
$container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']); |
213
|
8 |
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Handle config for fallback auto translate. |
217
|
|
|
* |
218
|
|
|
* @param ContainerBuilder $container |
219
|
|
|
* @param array $config |
220
|
|
|
*/ |
221
|
1 |
|
private function enableFallbackAutoTranslator(ContainerBuilder $container, array $config) |
222
|
|
|
{ |
223
|
1 |
|
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service']; |
224
|
1 |
|
$externalTranslatorDef = $container->getDefinition($externalTranslatorId); |
225
|
1 |
|
$externalTranslatorDef->addTag('php_translation.external_translator'); |
226
|
1 |
|
$externalTranslatorDef->addArgument(new Reference($config['http_client'])); |
227
|
1 |
|
$externalTranslatorDef->addArgument(new Reference($config['message_factory'])); |
228
|
|
|
|
229
|
1 |
|
$container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']); |
230
|
1 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
8 |
|
public function getAlias() |
236
|
|
|
{ |
237
|
8 |
|
return 'translation'; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* To avoid BC break for Symfony 3.3+. |
242
|
|
|
* |
243
|
|
|
* @param $parent |
244
|
|
|
* |
245
|
|
|
* @return ChildDefinition|DefinitionDecorator |
246
|
|
|
*/ |
247
|
8 |
|
private function createChildDefinition($parent) |
248
|
|
|
{ |
249
|
8 |
|
if (class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) { |
250
|
8 |
|
return new ChildDefinition($parent); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return new DefinitionDecorator($parent); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) |
260
|
|
|
{ |
261
|
|
|
return new Configuration($container); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|