Completed
Push — master ( fba80d...62bf5d )
by Tobias
05:54
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 28
cts 28
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
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 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->addEditInPlaceNode($root);
42 1
        $this->addWebUINode($root);
43
44
        $root
45 1
            ->children()
46 1
                ->arrayNode('locales')
47 1
                    ->prototype('scalar')->end()
48 1
                ->end()
49 1
                ->scalarNode('default_locale')->info('Your default language or fallback locale. Default will be kernel.default_locale')->end()
50 1
                ->arrayNode('symfony_profiler')
51 1
                    ->canBeEnabled()
52 1
                    ->children()
53 1
                        ->booleanNode('allow_edit')->defaultTrue()->end()
54 1
                    ->end()
55 1
                ->end()
56 1
                ->arrayNode('auto_add_missing_translations')
57 1
                    ->canBeEnabled()
58 1
                    ->children()
59 1
                        ->scalarNode('config_name')->defaultValue('default')->end()
60 1
                    ->end()
61 1
                ->end()
62 1
                ->scalarNode('http_client')->cannotBeEmpty()->defaultValue('httplug.client')->end()
63 1
                ->scalarNode('message_factory')->cannotBeEmpty()->defaultValue('httplug.message_factory')->end()
64 1
            ->end();
65
66 1
        return $treeBuilder;
67
    }
68
69
    /**
70
     * @param ArrayNodeDefinition $root
71
     */
72 1
    private function configsNode(ArrayNodeDefinition $root)
73
    {
74 1
        $container = $this->container;
75 1
        $root->children()
76 1
            ->arrayNode('configs')
77 1
                ->useAttributeAsKey('name')
78 1
                ->prototype('array')
79 1
                    ->fixXmlConfig('dir', 'dirs')
80 1
                    ->fixXmlConfig('excluded_dir')
81 1
                    ->fixXmlConfig('excluded_name')
82 1
                    ->fixXmlConfig('blacklist_domain')
83 1
                    ->fixXmlConfig('external_translations_dir')
84 1
                    ->fixXmlConfig('whitelist_domain')
85 1
                    ->children()
86 1
                        ->arrayNode('dirs')
87 1
                            ->info('Directories we should scan for translation files')
88 1
                            ->requiresAtLeastOneElement()
89 1
                            ->prototype('scalar')
90 1
                                ->validate()
91 1
                                    ->always(function ($value) use ($container) {
92
                                        $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
93
94
                                        if ('@' === $value[0]) {
95
                                            if (false === $pos = strpos($value, '/')) {
96
                                                $bundleName = substr($value, 1);
97
                                            } else {
98
                                                $bundleName = substr($value, 1, $pos - 2);
99
                                            }
100
101
                                            $bundles = $container->getParameter('kernel.bundles');
102
                                            if (!isset($bundles[$bundleName])) {
103
                                                throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles)));
104
                                            }
105
106
                                            $ref = new \ReflectionClass($bundles[$bundleName]);
107
                                            $value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos);
108
                                        }
109
110
                                        if (!is_dir($value)) {
111
                                            throw new \Exception(sprintf('The directory "%s" does not exist.', $value));
112
                                        }
113
114
                                        return $value;
115 1
                                    })
116 1
                                ->end()
117 1
                            ->end()
118 1
                        ->end()
119 1
                        ->arrayNode('excluded_dirs')
120 1
                            ->prototype('scalar')->end()
121 1
                        ->end()
122 1
                        ->arrayNode('excluded_names')
123 1
                            ->prototype('scalar')->end()
124 1
                        ->end()
125 1
                        ->arrayNode('external_translations_dirs')
126 1
                            ->prototype('scalar')->end()
127 1
                        ->end()
128 1
                        ->enumNode('output_format')->values(['php', 'yml', 'xlf'])->defaultValue('xlf')->end()
129 1
                        ->arrayNode('blacklist_domains')
130 1
                            ->prototype('scalar')->end()
131 1
                        ->end()
132 1
                        ->arrayNode('whitelist_domains')
133 1
                            ->prototype('scalar')->end()
134 1
                        ->end()
135 1
                        ->arrayNode('remote_storage')
136 1
                            ->info('Service ids with to classes that supports remote storage of translations.')
137 1
                            ->prototype('scalar')->end()
138 1
                        ->end()
139 1
                        ->arrayNode('local_storage')
140 1
                            ->info('Service ids with to classes that supports local storage of translations.')
141 1
                            ->prototype('scalar')->end()
142 1
                        ->end()
143 1
                        ->scalarNode('output_dir')->isRequired()->cannotBeEmpty()->end()
144 1
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
145 1
                    ->end()
146 1
                ->end()
147 1
            ->end()
148 1
        ->end();
149 1
    }
150
151 1
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
152
    {
153 1
        $root->children()
154 1
            ->arrayNode('fallback_translation')
155 1
                ->canBeEnabled()
156 1
                ->children()
157 1
                    ->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end()
158 1
                    ->scalarNode('api_key')->defaultNull()->end()
159 1
                ->end()
160 1
            ->end()
161 1
        ->end();
162 1
    }
163
164 1
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
165
    {
166 1
        $root->children()
167 1
            ->arrayNode('edit_in_place')
168 1
                ->canBeEnabled()
169 1
                ->children()
170 1
                    ->scalarNode('config_name')->defaultValue('default')->end()
171 1
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
172 1
                ->end()
173 1
            ->end()
174 1
        ->end();
175 1
    }
176
177 1
    private function addWebUINode(ArrayNodeDefinition $root)
178
    {
179 1
        $root->children()
180 1
            ->arrayNode('webui')
181 1
                ->canBeEnabled()
182 1
                ->children()
183 1
                    ->booleanNode('allow_create')->defaultTrue()->end()
184 1
                    ->booleanNode('allow_delete')->defaultTrue()->end()
185 1
                ->end()
186 1
            ->end()
187 1
        ->end();
188 1
    }
189
}
190