Completed
Push — master ( 130d29...c9c04e )
by Tobias
187:22 queued 181:29
created

Configuration::getConfigTreeBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 44
cts 44
cp 1
rs 9.0909
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 8
    public function __construct(ContainerBuilder $container)
27
    {
28 8
        $this->container = $container;
29 8
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 8
    public function getConfigTreeBuilder()
35
    {
36 8
        $treeBuilder = new TreeBuilder();
37 8
        $root = $treeBuilder->root('translation');
38
39 8
        $this->configsNode($root);
40 8
        $this->addAutoTranslateNode($root);
41 8
        $this->addEditInPlaceNode($root);
42 8
        $this->addWebUINode($root);
43
44 8
        $debug = $this->container->getParameter('kernel.debug');
45
        $root
46 8
            ->children()
47 8
                ->arrayNode('locales')
48 8
                    ->prototype('scalar')->end()
49 8
                ->end()
50 8
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
51 8
                ->arrayNode('symfony_profiler')
52 8
                    ->addDefaultsIfNotSet()
53 8
                    ->treatFalseLike(['enabled' => false])
54 8
                    ->treatTrueLike(['enabled' => true])
55 8
                    ->treatNullLike(['enabled' => $debug])
56 8
                    ->info('Extend the debug profiler with information about requests.')
57 8
                    ->children()
58 8
                        ->booleanNode('enabled')
59 8
                            ->info('Turn the symfony profiler integration on or off. Defaults to kernel debug mode.')
60 8
                            ->defaultValue($debug)
61 8
                        ->end()
62 8
                        ->scalarNode('formatter')->defaultNull()->end()
63 8
                        ->integerNode('captured_body_length')
64 8
                            ->defaultValue(0)
65 8
                            ->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 8
                        ->end()
67 8
                    ->end()
68 8
                    ->children()
69 8
                        ->booleanNode('allow_edit')->defaultTrue()->end()
70 8
                    ->end()
71 8
                ->end()
72 8
                ->arrayNode('auto_add_missing_translations')
73 8
                    ->canBeEnabled()
74 8
                    ->children()
75 8
                        ->scalarNode('config_name')->defaultValue('default')->end()
76 8
                    ->end()
77 8
                ->end()
78 8
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
79 8
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
80 8
            ->end();
81
82 8
        return $treeBuilder;
83
    }
84
85
    /**
86
     * @param ArrayNodeDefinition $root
87
     */
88 8
    private function configsNode(ArrayNodeDefinition $root)
89
    {
90 8
        $container = $this->container;
91 8
        $root->children()
92 8
            ->arrayNode('configs')
93 8
            ->addDefaultChildrenIfNoneSet('default')
94 8
                ->useAttributeAsKey('name')
95 8
                ->prototype('array')
96 8
                    ->fixXmlConfig('dir', 'dirs')
97 8
                    ->fixXmlConfig('excluded_dir')
98 8
                    ->fixXmlConfig('excluded_name')
99 8
                    ->fixXmlConfig('blacklist_domain')
100 8
                    ->fixXmlConfig('external_translations_dir')
101 8
                    ->fixXmlConfig('whitelist_domain')
102 8
                    ->children()
103 8
                        ->arrayNode('dirs')
104 8
                            ->info('Directories we should scan for translations')
105 8
                            ->prototype('scalar')
106 8
                                ->validate()
107 8
                                    ->always(function ($value) use ($container) {
108
                                        $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
109
110
                                        if ('@' === $value[0]) {
111 View Code Duplication
                                            if (false === $pos = strpos($value, '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 8
                                    })
132 8
                                ->end()
133 8
                            ->end()
134 8
                        ->end()
135 8
                        ->arrayNode('excluded_dirs')
136 8
                            ->prototype('scalar')->end()
137 8
                        ->end()
138 8
                        ->arrayNode('excluded_names')
139 8
                            ->prototype('scalar')->end()
140 8
                        ->end()
141 8
                        ->arrayNode('external_translations_dirs')
142 8
                            ->prototype('scalar')->end()
143 8
                        ->end()
144 8
                        ->enumNode('output_format')->values(['php', 'yml', 'xlf', 'po'])->defaultValue('xlf')->end()
145 8
                        ->arrayNode('blacklist_domains')
146 8
                            ->prototype('scalar')->end()
147 8
                        ->end()
148 8
                        ->arrayNode('whitelist_domains')
149 8
                            ->prototype('scalar')->end()
150 8
                        ->end()
151 8
                        ->arrayNode('remote_storage')
152 8
                            ->info('Service ids with to classes that supports remote storage of translations.')
153 8
                            ->prototype('scalar')->end()
154 8
                        ->end()
155 8
                        ->arrayNode('local_storage')
156 8
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
157 8
                            ->info('Service ids with to classes that supports local storage of translations.')
158 8
                            ->prototype('scalar')->end()
159 8
                        ->end()
160 8
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
161 8
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end()
162 8
                        ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end()
163 8
                        ->variableNode('local_file_storage_options')
164 8
                            ->info('Options passed to the local file storage\'s dumper.')
165 8
                            ->defaultValue([])
166 8
                            ->validate()
167 8
                                ->ifTrue(function ($value) {
168
                                    return !is_array($value);
169 8
                                })
170 8
                                ->thenInvalid('"local_file_storage_options" must be an array.')
171 8
                            ->end()
172 8
                        ->end()
173 8
                    ->end()
174 8
                ->end()
175 8
            ->end()
176 8
        ->end();
177 8
    }
178
179 8
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
180
    {
181 8
        $root->children()
182 8
            ->arrayNode('fallback_translation')
183 8
                ->canBeEnabled()
184 8
                ->children()
185 8
                    ->enumNode('service')->values(['google', 'yandex'])->defaultValue('google')->end()
186 8
                    ->scalarNode('api_key')->defaultNull()->end()
187 8
                ->end()
188 8
            ->end()
189 8
        ->end();
190 8
    }
191
192 8
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
193
    {
194 8
        $root->children()
195 8
            ->arrayNode('edit_in_place')
196 8
                ->canBeEnabled()
197 8
                ->children()
198 8
                    ->scalarNode('config_name')->defaultValue('default')->end()
199 8
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
200 8
                    ->scalarNode('show_untranslatable')->defaultTrue()->end()
201 8
                ->end()
202 8
            ->end()
203 8
        ->end();
204 8
    }
205
206 8
    private function addWebUINode(ArrayNodeDefinition $root)
207
    {
208 8
        $root->children()
209 8
            ->arrayNode('webui')
210 8
                ->canBeEnabled()
211 8
                ->children()
212 8
                    ->booleanNode('allow_create')->defaultTrue()->end()
213 8
                    ->booleanNode('allow_delete')->defaultTrue()->end()
214 8
                    ->scalarNode('file_base_path')->defaultNull()->info('Base path for SourceLocation\'s. Defaults to "%kernel.project_dir%".')->end()
215 8
                ->end()
216 8
            ->end()
217 8
        ->end();
218 8
    }
219
}
220