Completed
Push — master ( d13a19...83a9fe )
by Tobias
12:57 queued 06:36
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->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
                        ->arrayNode('remote_storage')
135 1
                            ->info('Service ids with to classes that supports remote storage of translations.')
136 1
                            ->prototype('scalar')->end()
137 1
                        ->end()
138 1
                        ->arrayNode('local_storage')
139 1
                            ->info('Service ids with to classes that supports local storage of translations.')
140 1
                            ->prototype('scalar')->end()
141 1
                        ->end()
142 1
                        ->scalarNode('output_dir')->isRequired()->cannotBeEmpty()->end()
143 1
                        ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end()
144 1
                    ->end()
145 1
                ->end()
146 1
            ->end()
147 1
        ->end();
148 1
    }
149
150 1
    private function addAutoTranslateNode(ArrayNodeDefinition $root)
151
    {
152 1
        $root->children()
153 1
            ->arrayNode('fallback_translation')
154 1
                ->canBeEnabled()
155 1
                ->children()
156 1
                    ->enumNode('service')->values(['google', 'bing'])->defaultValue('google')->end()
157 1
                    ->scalarNode('api_key')->defaultNull()->end()
158 1
                ->end()
159 1
            ->end()
160 1
        ->end();
161 1
    }
162
163 1
    private function addTranslationServiceNode(ArrayNodeDefinition $root)
164
    {
165
        $root
166 1
            ->children()
167 1
                ->enumNode('storage')
168 1
                    ->info('Where translations are stored.')
169 1
                    ->values(['blackhole', 'filesystem', 'loco'])
170 1
                    ->defaultValue('filesystem')
171 1
                ->end()
172 1
            ->end();
173 1
    }
174
}
175