Completed
Push — master ( 28dd7c...705fb1 )
by Tobias
07:56
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
            ->addDefaultChildrenIfNoneSet('default')
78 1
                ->useAttributeAsKey('name')
79 1
                ->prototype('array')
80 1
                    ->fixXmlConfig('dir', 'dirs')
81 1
                    ->fixXmlConfig('excluded_dir')
82 1
                    ->fixXmlConfig('excluded_name')
83 1
                    ->fixXmlConfig('blacklist_domain')
84 1
                    ->fixXmlConfig('external_translations_dir')
85 1
                    ->fixXmlConfig('whitelist_domain')
86 1
                    ->children()
87 1
                        ->arrayNode('dirs')
88 1
                            ->info('Directories we should scan for translations')
89 1
                            ->prototype('scalar')
90 1
                                ->validate()
91
                                    ->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
                            ->defaultValue(['php_translation.local_file_storage.abstract'])
141 1
                            ->info('Service ids with to classes that supports local storage of translations.')
142 1
                            ->prototype('scalar')->end()
143 1
                        ->end()
144 1
                        ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end()
145 1
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
146 1
                        ->variableNode('local_file_storage_options')
147 1
                            ->info('Options passed to the local file storage\'s dumper.')
148 1
                            ->defaultValue([])
149 1
                            ->validate()
150 1
                                ->ifTrue(function ($value) {
151
                                    return !is_array($value);
152 1
                                })
153 1
                                ->thenInvalid('"local_file_storage_options" must be an array.')
154 1
                            ->end()
155 1
                        ->end()
156 1
                    ->end()
157 1
                ->end()
158 1
            ->end()
159 1
        ->end();
160 1
    }
161
162 1
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
163
    {
164 1
        $root->children()
165 1
            ->arrayNode('fallback_translation')
166 1
                ->canBeEnabled()
167 1
                ->children()
168 1
                    ->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end()
169 1
                    ->scalarNode('api_key')->defaultNull()->end()
170 1
                ->end()
171 1
            ->end()
172 1
        ->end();
173 1
    }
174
175 1
    private function addEditInPlaceNode(ArrayNodeDefinition $root)
176
    {
177 1
        $root->children()
178 1
            ->arrayNode('edit_in_place')
179 1
                ->canBeEnabled()
180 1
                ->children()
181 1
                    ->scalarNode('config_name')->defaultValue('default')->end()
182 1
                    ->scalarNode('activator')->cannotBeEmpty()->defaultValue('php_translation.edit_in_place.activator')->end()
183 1
                ->end()
184 1
            ->end()
185 1
        ->end();
186 1
    }
187
188 1
    private function addWebUINode(ArrayNodeDefinition $root)
189
    {
190 1
        $root->children()
191 1
            ->arrayNode('webui')
192 1
                ->canBeEnabled()
193 1
                ->children()
194 1
                    ->booleanNode('allow_create')->defaultTrue()->end()
195 1
                    ->booleanNode('allow_delete')->defaultTrue()->end()
196 1
                ->end()
197 1
            ->end()
198 1
        ->end();
199 1
    }
200
}
201