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