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\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
16
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This is the class that validates and merges configuration from your app/config files. |
21
|
|
|
*/ |
22
|
|
|
class Configuration implements ConfigurationInterface |
23
|
|
|
{ |
24
|
|
|
private $container; |
25
|
|
|
|
26
|
12 |
|
public function __construct(ContainerBuilder $container) |
27
|
|
|
{ |
28
|
12 |
|
$this->container = $container; |
29
|
12 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
12 |
|
public function getConfigTreeBuilder() |
35
|
|
|
{ |
36
|
12 |
|
$treeBuilder = new TreeBuilder(); |
37
|
12 |
|
$root = $treeBuilder->root('translation'); |
38
|
|
|
|
39
|
12 |
|
$this->configsNode($root); |
40
|
12 |
|
$this->addAutoTranslateNode($root); |
41
|
12 |
|
$this->addEditInPlaceNode($root); |
42
|
12 |
|
$this->addWebUINode($root); |
43
|
|
|
|
44
|
12 |
|
$debug = $this->container->getParameter('kernel.debug'); |
45
|
|
|
$root |
46
|
12 |
|
->children() |
47
|
12 |
|
->arrayNode('locales') |
48
|
12 |
|
->prototype('scalar')->end() |
49
|
12 |
|
->end() |
50
|
12 |
|
->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end() |
51
|
12 |
|
->arrayNode('symfony_profiler') |
52
|
12 |
|
->addDefaultsIfNotSet() |
53
|
12 |
|
->treatFalseLike(['enabled' => false]) |
54
|
12 |
|
->treatTrueLike(['enabled' => true]) |
55
|
12 |
|
->treatNullLike(['enabled' => $debug]) |
56
|
12 |
|
->info('Extend the debug profiler with information about requests.') |
57
|
12 |
|
->children() |
58
|
12 |
|
->booleanNode('enabled') |
59
|
12 |
|
->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.') |
60
|
12 |
|
->defaultValue($debug) |
61
|
12 |
|
->end() |
62
|
12 |
|
->scalarNode('formatter')->defaultNull()->end() |
63
|
12 |
|
->integerNode('captured_body_length') |
64
|
12 |
|
->defaultValue(0) |
65
|
12 |
|
->info('Limit long HTTP message bodies to x characters. If set to 0 we do not read the message body. Only available with the default formatter (FullHttpMessageFormatter).') |
66
|
12 |
|
->end() |
67
|
12 |
|
->end() |
68
|
12 |
|
->children() |
69
|
12 |
|
->booleanNode('allow_edit')->defaultTrue()->end() |
70
|
12 |
|
->end() |
71
|
12 |
|
->end() |
72
|
12 |
|
->arrayNode('auto_add_missing_translations') |
73
|
12 |
|
->canBeEnabled() |
74
|
12 |
|
->children() |
75
|
12 |
|
->scalarNode('config_name')->defaultValue('default')->end() |
76
|
12 |
|
->end() |
77
|
12 |
|
->end() |
78
|
12 |
|
->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end() |
79
|
12 |
|
->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end() |
80
|
12 |
|
->end(); |
81
|
|
|
|
82
|
12 |
|
return $treeBuilder; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ArrayNodeDefinition $root |
87
|
|
|
*/ |
88
|
12 |
|
private function configsNode(ArrayNodeDefinition $root) |
89
|
|
|
{ |
90
|
12 |
|
$container = $this->container; |
91
|
12 |
|
$root->children() |
92
|
12 |
|
->arrayNode('configs') |
93
|
12 |
|
->addDefaultChildrenIfNoneSet('default') |
94
|
12 |
|
->useAttributeAsKey('name') |
95
|
12 |
|
->prototype('array') |
96
|
12 |
|
->fixXmlConfig('dir', 'dirs') |
97
|
12 |
|
->fixXmlConfig('excluded_dir') |
98
|
12 |
|
->fixXmlConfig('excluded_name') |
99
|
12 |
|
->fixXmlConfig('blacklist_domain') |
100
|
12 |
|
->fixXmlConfig('external_translations_dir') |
101
|
12 |
|
->fixXmlConfig('whitelist_domain') |
102
|
12 |
|
->children() |
103
|
12 |
|
->arrayNode('dirs') |
104
|
12 |
|
->info('Directories we should scan for translations') |
105
|
12 |
|
->prototype('scalar') |
106
|
12 |
|
->validate() |
107
|
|
|
->always(function ($value) use ($container) { |
108
|
|
|
$value = str_replace(DIRECTORY_SEPARATOR, '/', $value); |
109
|
|
|
|
110
|
|
|
if ('@' === $value[0]) { |
111
|
|
|
if (false === $pos = strpos($value, '/')) { |
112
|
|
|
$bundleName = substr($value, 1); |
113
|
|
|
} else { |
114
|
|
|
$bundleName = substr($value, 1, $pos - 2); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
118
|
|
|
if (!isset($bundles[$bundleName])) { |
119
|
|
|
throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles))); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$ref = new \ReflectionClass($bundles[$bundleName]); |
123
|
|
|
$value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!is_dir($value)) { |
127
|
|
|
throw new \Exception(sprintf('The directory "%s" does not exist.', $value)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $value; |
131
|
12 |
|
}) |
132
|
12 |
|
->end() |
133
|
12 |
|
->end() |
134
|
12 |
|
->end() |
135
|
12 |
|
->arrayNode('excluded_dirs') |
136
|
12 |
|
->prototype('scalar')->end() |
137
|
12 |
|
->end() |
138
|
12 |
|
->arrayNode('excluded_names') |
139
|
12 |
|
->prototype('scalar')->end() |
140
|
12 |
|
->end() |
141
|
12 |
|
->arrayNode('external_translations_dirs') |
142
|
12 |
|
->prototype('scalar')->end() |
143
|
12 |
|
->end() |
144
|
12 |
|
->enumNode('output_format')->values(['php', 'yml', 'xlf'])->defaultValue('xlf')->end() |
145
|
12 |
|
->arrayNode('blacklist_domains') |
146
|
12 |
|
->prototype('scalar')->end() |
147
|
12 |
|
->end() |
148
|
12 |
|
->arrayNode('whitelist_domains') |
149
|
12 |
|
->prototype('scalar')->end() |
150
|
12 |
|
->end() |
151
|
12 |
|
->arrayNode('remote_storage') |
152
|
12 |
|
->info('Service ids with to classes that supports remote storage of translations.') |
153
|
12 |
|
->prototype('scalar')->end() |
154
|
12 |
|
->end() |
155
|
12 |
|
->arrayNode('local_storage') |
156
|
12 |
|
->defaultValue(['php_translation.local_file_storage.abstract']) |
157
|
12 |
|
->info('Service ids with to classes that supports local storage of translations.') |
158
|
12 |
|
->prototype('scalar')->end() |
159
|
12 |
|
->end() |
160
|
12 |
|
->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end() |
161
|
12 |
|
->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end() |
162
|
12 |
|
->variableNode('local_file_storage_options') |
163
|
12 |
|
->info('Options passed to the local file storage\'s dumper.') |
164
|
12 |
|
->defaultValue([]) |
165
|
12 |
|
->validate() |
166
|
12 |
|
->ifTrue(function ($value) { |
167
|
|
|
return !is_array($value); |
168
|
12 |
|
}) |
169
|
12 |
|
->thenInvalid('"local_file_storage_options" must be an array.') |
170
|
12 |
|
->end() |
171
|
12 |
|
->end() |
172
|
12 |
|
->end() |
173
|
12 |
|
->end() |
174
|
12 |
|
->end() |
175
|
12 |
|
->end(); |
176
|
12 |
|
} |
177
|
|
|
|
178
|
12 |
|
private function addAutoTranslateNode(ArrayNodeDefinition $root) |
179
|
|
|
{ |
180
|
12 |
|
$root->children() |
181
|
12 |
|
->arrayNode('fallback_translation') |
182
|
12 |
|
->canBeEnabled() |
183
|
12 |
|
->children() |
184
|
12 |
|
->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end() |
185
|
12 |
|
->scalarNode('api_key')->defaultNull()->end() |
186
|
12 |
|
->end() |
187
|
12 |
|
->end() |
188
|
12 |
|
->end(); |
189
|
12 |
|
} |
190
|
|
|
|
191
|
12 |
|
private function addEditInPlaceNode(ArrayNodeDefinition $root) |
192
|
|
|
{ |
193
|
12 |
|
$root->children() |
194
|
12 |
|
->arrayNode('edit_in_place') |
195
|
12 |
|
->canBeEnabled() |
196
|
12 |
|
->children() |
197
|
12 |
|
->scalarNode('config_name')->defaultValue('default')->end() |
198
|
12 |
|
->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end() |
199
|
12 |
|
->scalarNode('show_untranslatable')->defaultTrue()->end() |
200
|
12 |
|
->end() |
201
|
12 |
|
->end() |
202
|
12 |
|
->end(); |
203
|
12 |
|
} |
204
|
|
|
|
205
|
12 |
|
private function addWebUINode(ArrayNodeDefinition $root) |
206
|
|
|
{ |
207
|
12 |
|
$root->children() |
208
|
12 |
|
->arrayNode('webui') |
209
|
12 |
|
->canBeEnabled() |
210
|
12 |
|
->children() |
211
|
12 |
|
->booleanNode('allow_create')->defaultTrue()->end() |
212
|
12 |
|
->booleanNode('allow_delete')->defaultTrue()->end() |
213
|
12 |
|
->end() |
214
|
12 |
|
->end() |
215
|
12 |
|
->end(); |
216
|
12 |
|
} |
217
|
|
|
} |
218
|
|
|
|