|
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\ContainerBuilder; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
19
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
22
|
|
|
use Translation\Bundle\Model\Configuration as ConfigurationModel; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This is the class that loads and manages your bundle configuration. |
|
26
|
|
|
* |
|
27
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
|
28
|
|
|
*/ |
|
29
|
|
|
class TranslationExtension extends Extension |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$configuration = new Configuration($container); |
|
37
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
38
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
39
|
|
|
|
|
40
|
1 |
|
$loader->load('services.yml'); |
|
41
|
1 |
|
$loader->load('extractors.yml'); |
|
42
|
|
|
|
|
43
|
|
|
// Add major version to extractor |
|
44
|
1 |
|
$container->getDefinition('php_translation.extractor.php.visitor.FormTypeChoices') |
|
45
|
1 |
|
->addMethodCall('setSymfonyMajorVersion', [Kernel::MAJOR_VERSION]); |
|
46
|
|
|
|
|
47
|
1 |
|
$container->setParameter('php_translation.locales', $config['locales']); |
|
48
|
1 |
|
$container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale')); |
|
49
|
1 |
|
$this->handleConfigNode($container, $config); |
|
50
|
|
|
|
|
51
|
1 |
|
if ($config['webui']['enabled']) { |
|
52
|
1 |
|
$this->enableWebUi($container, $config); |
|
53
|
1 |
|
} else { |
|
54
|
|
|
$container->setParameter('php_translation.webui.enabled', false); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
if ($config['symfony_profiler']['enabled']) { |
|
58
|
|
|
$loader->load('symfony_profiler.yml'); |
|
59
|
|
|
$this->enableSymfonyProfiler($container, $config); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
if ($config['edit_in_place']['enabled']) { |
|
63
|
1 |
|
$loader->load('edit_in_place.yml'); |
|
64
|
1 |
|
$this->enableEditInPlace($container, $config); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
if ($config['auto_add_missing_translations']['enabled']) { |
|
68
|
|
|
$loader->load('auto_add.yml'); |
|
69
|
|
|
$container->getDefinition('php_translator.auto_adder') |
|
70
|
|
|
->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name'])); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
if ($config['fallback_translation']['enabled']) { |
|
74
|
|
|
$loader->load('auto_translation.yml'); |
|
75
|
|
|
$this->enableFallbackAutoTranslator($container, $config); |
|
76
|
|
|
} |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Handle the config node to prepare the config manager. |
|
81
|
|
|
* |
|
82
|
|
|
* @param ContainerBuilder $container |
|
83
|
|
|
* @param array $config |
|
84
|
|
|
*/ |
|
85
|
1 |
|
private function handleConfigNode(ContainerBuilder $container, array $config) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$configurationManager = $container->getDefinition('php_translation.configuration_manager'); |
|
88
|
|
|
// $first will be the "default" configuration. |
|
89
|
1 |
|
$first = null; |
|
90
|
1 |
|
foreach ($config['configs'] as $name => &$c) { |
|
91
|
|
|
if ($first === null || $name === 'default') { |
|
92
|
|
|
$first = $name; |
|
93
|
|
|
} |
|
94
|
|
|
if (empty($c['project_root'])) { |
|
95
|
|
|
// Add a project root of none is set. |
|
96
|
|
|
$c['project_root'] = dirname($container->getParameter('kernel.root_dir')); |
|
97
|
|
|
} |
|
98
|
|
|
$c['name'] = $name; |
|
99
|
|
|
$c['locales'] = $config['locales']; |
|
100
|
|
|
$configurationServiceId = 'php_translation.configuration.'.$name; |
|
101
|
|
|
$configDef = $container->register($configurationServiceId, ConfigurationModel::class); |
|
102
|
|
|
$configDef->setPublic(false)->addArgument($c); |
|
103
|
|
|
$configurationManager->addMethodCall('addConfiguration', [new Reference($configurationServiceId)]); |
|
104
|
|
|
|
|
105
|
|
|
/* |
|
106
|
|
|
* Configure storage service |
|
107
|
|
|
*/ |
|
108
|
|
|
$storageDefinition = new DefinitionDecorator('php_translation.storage.abstract'); |
|
109
|
|
|
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId)); |
|
110
|
|
|
$container->setDefinition('php_translation.storage.'.$name, $storageDefinition); |
|
111
|
|
|
|
|
112
|
|
|
// Add storages |
|
113
|
|
|
if (!empty($c['remote_storage'])) { |
|
114
|
|
|
foreach ($c['remote_storage'] as $serviceId) { |
|
115
|
|
|
$storageDefinition->addMethodCall('addRemoteStorage', [new Reference($serviceId)]); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
if (!empty($c['local_storage'])) { |
|
119
|
|
|
foreach ($c['local_storage'] as $serviceId) { |
|
120
|
|
|
$storageDefinition->addMethodCall('addLocalStorage', [new Reference($serviceId)]); |
|
121
|
|
|
} |
|
122
|
|
|
} else { |
|
123
|
|
|
// If there is no local storage configured, register a file storage |
|
124
|
|
|
$def = new DefinitionDecorator('php_translation.single_storage.file.abstract'); |
|
125
|
|
|
$def->replaceArgument(2, $c['output_dir']) |
|
126
|
|
|
->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]); |
|
127
|
|
|
$container->setDefinition('php_translation.single_storage.file.'.$name, $def); |
|
128
|
|
|
} |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
if ($first !== null) { |
|
132
|
|
|
// Create some aliases for the default storage |
|
133
|
|
|
$container->setAlias('php_translation.storage', 'php_translation.sstorage.'.$first); |
|
134
|
|
|
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first); |
|
135
|
|
|
} |
|
136
|
1 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Handle config for WebUI. |
|
140
|
|
|
* |
|
141
|
|
|
* @param ContainerBuilder $container |
|
142
|
|
|
* @param array $config |
|
143
|
|
|
*/ |
|
144
|
|
|
private function enableWebUi(ContainerBuilder $container, array $config) |
|
145
|
|
|
{ |
|
146
|
|
|
$container->setParameter('php_translation.webui.enabled', true); |
|
147
|
|
|
$container->setParameter('php_translation.webui.allow_create', $config['webui']['allow_create']); |
|
148
|
|
|
$container->setParameter('php_translation.webui.allow_delete', $config['webui']['allow_delete']); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Handle config for EditInPlace. |
|
153
|
|
|
* |
|
154
|
|
|
* @param ContainerBuilder $container |
|
155
|
|
|
* @param array $config |
|
156
|
|
|
*/ |
|
157
|
|
|
private function enableEditInPlace(ContainerBuilder $container, array $config) |
|
158
|
|
|
{ |
|
159
|
|
|
$name = $config['edit_in_place']['config_name']; |
|
160
|
|
|
|
|
161
|
|
|
if ($name !== 'default' and !isset($config['configs'][$name])) { |
|
|
|
|
|
|
162
|
|
|
throw new InvalidArgumentException(sprintf('There is no config named "%s".', $name)); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$activatorRef = new Reference($config['edit_in_place']['activator']); |
|
166
|
|
|
|
|
167
|
|
|
$def = $container->getDefinition('php_translation.edit_in_place.response_listener'); |
|
168
|
|
|
$def->replaceArgument(0, $activatorRef); |
|
169
|
|
|
$def->replaceArgument(3, $name); |
|
170
|
|
|
|
|
171
|
|
|
$def = $container->getDefinition('php_translator.edit_in_place.xtrans_html_translator'); |
|
172
|
|
|
$def->replaceArgument(1, $activatorRef); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Handle config for Symfony Profiler. |
|
177
|
|
|
* |
|
178
|
|
|
* @param ContainerBuilder $container |
|
179
|
|
|
* @param array $config |
|
180
|
|
|
*/ |
|
181
|
|
|
private function enableSymfonyProfiler(ContainerBuilder $container, array $config) |
|
182
|
|
|
{ |
|
183
|
|
|
$container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Handle config for fallback auto translate. |
|
188
|
|
|
* |
|
189
|
|
|
* @param ContainerBuilder $container |
|
190
|
|
|
* @param array $config |
|
191
|
|
|
*/ |
|
192
|
|
|
private function enableFallbackAutoTranslator(ContainerBuilder $container, array $config) |
|
193
|
|
|
{ |
|
194
|
|
|
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service']; |
|
195
|
|
|
$externalTranslatorDef = $container->getDefinition($externalTranslatorId); |
|
196
|
|
|
$externalTranslatorDef->addTag('php_translation.external_translator'); |
|
197
|
|
|
$externalTranslatorDef->addArgument(new Reference($config['http_client'])); |
|
198
|
|
|
$externalTranslatorDef->addArgument(new Reference($config['message_factory'])); |
|
199
|
|
|
|
|
200
|
|
|
$container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getAlias() |
|
207
|
|
|
{ |
|
208
|
|
|
return 'translation'; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.