|
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
|
1 |
|
public function __construct(ContainerBuilder $container) |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->container = $container; |
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function getConfigTreeBuilder() |
|
35
|
|
|
{ |
|
36
|
1 |
|
$treeBuilder = new TreeBuilder(); |
|
37
|
1 |
|
$root = $treeBuilder->root('translation'); |
|
38
|
|
|
|
|
39
|
1 |
|
$this->configsNode($root); |
|
40
|
1 |
|
$this->addAutoTranslateNode($root); |
|
41
|
1 |
|
$this->addTranslationServiceNode($root); |
|
42
|
|
|
|
|
43
|
|
|
$root |
|
44
|
1 |
|
->children() |
|
45
|
1 |
|
->arrayNode('locales') |
|
46
|
1 |
|
->prototype('scalar')->end() |
|
47
|
1 |
|
->end() |
|
48
|
1 |
|
->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end() |
|
49
|
1 |
|
->arrayNode('symfony_profiler') |
|
50
|
1 |
|
->canBeEnabled() |
|
51
|
1 |
|
->children() |
|
52
|
1 |
|
->booleanNode('allow_edit')->defaultTrue()->end() |
|
53
|
1 |
|
->end() |
|
54
|
1 |
|
->end() |
|
55
|
1 |
|
->arrayNode('webui') |
|
56
|
1 |
|
->canBeEnabled() |
|
57
|
1 |
|
->children() |
|
58
|
1 |
|
->booleanNode('allow_add')->defaultTrue()->end() |
|
59
|
1 |
|
->end() |
|
60
|
1 |
|
->end() |
|
61
|
1 |
|
->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end() |
|
62
|
1 |
|
->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end() |
|
63
|
1 |
|
->end(); |
|
64
|
|
|
|
|
65
|
1 |
|
return $treeBuilder; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param ArrayNodeDefinition $root |
|
70
|
|
|
*/ |
|
71
|
1 |
|
private function configsNode(ArrayNodeDefinition $root) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$container = $this->container; |
|
74
|
1 |
|
$root->children() |
|
75
|
1 |
|
->arrayNode('configs') |
|
76
|
1 |
|
->useAttributeAsKey('name') |
|
77
|
1 |
|
->prototype('array') |
|
78
|
1 |
|
->fixXmlConfig('dir', 'dirs') |
|
79
|
1 |
|
->fixXmlConfig('excluded_dir') |
|
80
|
1 |
|
->fixXmlConfig('excluded_name') |
|
81
|
1 |
|
->fixXmlConfig('blacklist_domain') |
|
82
|
1 |
|
->fixXmlConfig('external_translations_dir') |
|
83
|
1 |
|
->fixXmlConfig('whitelist_domain') |
|
84
|
1 |
|
->children() |
|
85
|
1 |
|
->arrayNode('dirs') |
|
86
|
1 |
|
->info('Directories we should scan for translation files') |
|
87
|
1 |
|
->requiresAtLeastOneElement() |
|
88
|
1 |
|
->prototype('scalar') |
|
89
|
1 |
|
->validate() |
|
90
|
1 |
|
->always(function ($value) use ($container) { |
|
91
|
|
|
$value = str_replace(DIRECTORY_SEPARATOR, '/', $value); |
|
92
|
|
|
|
|
93
|
|
|
if ('@' === $value[0]) { |
|
94
|
|
|
if (false === $pos = strpos($value, '/')) { |
|
95
|
|
|
$bundleName = substr($value, 1); |
|
96
|
|
|
} else { |
|
97
|
|
|
$bundleName = substr($value, 1, $pos - 2); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
|
101
|
|
|
if (!isset($bundles[$bundleName])) { |
|
102
|
|
|
throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles))); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$ref = new \ReflectionClass($bundles[$bundleName]); |
|
106
|
|
|
$value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!is_dir($value)) { |
|
110
|
|
|
throw new \Exception(sprintf('The directory "%s" does not exist.', $value)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $value; |
|
114
|
1 |
|
}) |
|
115
|
1 |
|
->end() |
|
116
|
1 |
|
->end() |
|
117
|
1 |
|
->end() |
|
118
|
1 |
|
->arrayNode('excluded_dirs') |
|
119
|
1 |
|
->prototype('scalar')->end() |
|
120
|
1 |
|
->end() |
|
121
|
1 |
|
->arrayNode('excluded_names') |
|
122
|
1 |
|
->prototype('scalar')->end() |
|
123
|
1 |
|
->end() |
|
124
|
1 |
|
->arrayNode('external_translations_dirs') |
|
125
|
1 |
|
->prototype('scalar')->end() |
|
126
|
1 |
|
->end() |
|
127
|
1 |
|
->enumNode('output_format')->values(['php', 'yml', 'xlf'])->defaultValue('xlf')->end() |
|
128
|
1 |
|
->arrayNode('blacklist_domains') |
|
129
|
1 |
|
->prototype('scalar')->end() |
|
130
|
1 |
|
->end() |
|
131
|
1 |
|
->arrayNode('whitelist_domains') |
|
132
|
1 |
|
->prototype('scalar')->end() |
|
133
|
1 |
|
->end() |
|
134
|
1 |
|
->scalarNode('output_dir')->isRequired()->cannotBeEmpty()->end() |
|
135
|
1 |
|
->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end() |
|
136
|
1 |
|
->end() |
|
137
|
1 |
|
->end() |
|
138
|
1 |
|
->end() |
|
139
|
1 |
|
->end(); |
|
140
|
1 |
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
private function addAutoTranslateNode(ArrayNodeDefinition $root) |
|
143
|
|
|
{ |
|
144
|
1 |
|
$root->children() |
|
145
|
1 |
|
->arrayNode('fallback_translation') |
|
146
|
1 |
|
->canBeEnabled() |
|
147
|
1 |
|
->children() |
|
148
|
1 |
|
->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end() |
|
149
|
1 |
|
->scalarNode('api_key')->defaultNull()->end() |
|
150
|
1 |
|
->end() |
|
151
|
1 |
|
->end() |
|
152
|
1 |
|
->end(); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
private function addTranslationServiceNode(ArrayNodeDefinition $root) |
|
156
|
|
|
{ |
|
157
|
|
|
$root |
|
158
|
1 |
|
->children() |
|
159
|
1 |
|
->enumNode('storage') |
|
160
|
1 |
|
->info('Where translations are stored.') |
|
161
|
1 |
|
->values(['blackhole', 'filesystem', 'loco']) |
|
162
|
1 |
|
->defaultValue('filesystem') |
|
163
|
1 |
|
->end() |
|
164
|
1 |
|
->end(); |
|
165
|
1 |
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|