Completed
Push — master ( 584a71...a5bdca )
by Tobias
24:17 queued 18:42
created

TranslationExtension::enableEditInPlace()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 2
nop 2
crap 3.0052
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\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Symfony\Component\DependencyInjection\Loader;
21
use Symfony\Component\HttpKernel\Kernel;
22
use Translation\Bundle\Model\Configuration as ConfigurationModel;
23
24
/**
25
 * This is the class that loads and manages your bundle configuration.
26
 *
27
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
28
 */
29
class TranslationExtension extends Extension
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 19
    public function load(array $configs, ContainerBuilder $container)
35
    {
36 19
        $configuration = new Configuration($container);
37 19
        $config = $this->processConfiguration($configuration, $configs);
38 19
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
39
40 19
        $loader->load('services.yml');
41 19
        $loader->load('extractors.yml');
42
43
        // Add major version to extractor
44 19
        $container->getDefinition('php_translation.extractor.php.visitor.FormTypeChoices')
45 19
            ->addMethodCall('setSymfonyMajorVersion', [Kernel::MAJOR_VERSION]);
46
47 19
        $container->setParameter('php_translation.locales', $config['locales']);
48 19
        $container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale'));
49 19
        $this->handleConfigNode($container, $config);
50
51 19
        if ($config['webui']['enabled']) {
52 12
            $this->enableWebUi($container, $config);
53 12
        } else {
54 7
            $container->setParameter('php_translation.webui.enabled', false);
55
        }
56
57 19
        if ($config['symfony_profiler']['enabled']) {
58 19
            $loader->load('symfony_profiler.yml');
59 19
            $this->enableSymfonyProfiler($container, $config);
60 19
        }
61
62 19
        if ($config['edit_in_place']['enabled']) {
63 12
            $loader->load('edit_in_place.yml');
64 12
            $this->enableEditInPlace($container, $config);
65 12
        }
66
67 19
        if ($config['auto_add_missing_translations']['enabled']) {
68 1
            $loader->load('auto_add.yml');
69 1
            $container->getDefinition('php_translator.auto_adder')
70 1
                ->replaceArgument(0, new Reference('php_translation.storage.'.$config['auto_add_missing_translations']['config_name']));
71 1
        }
72
73 19
        if ($config['fallback_translation']['enabled']) {
74 1
            $loader->load('auto_translation.yml');
75 1
            $this->enableFallbackAutoTranslator($container, $config);
76 1
        }
77 19
    }
78
79
    /**
80
     * Handle the config node to prepare the config manager.
81
     *
82
     * @param ContainerBuilder $container
83
     * @param array            $config
84
     */
85 19
    private function handleConfigNode(ContainerBuilder $container, array $config)
86
    {
87 19
        $configurationManager = $container->getDefinition('php_translation.configuration_manager');
88
        // $first will be the "default" configuration.
89 19
        $first = null;
90 19
        foreach ($config['configs'] as $name => &$c) {
91 19
            if ($first === null || $name === 'default') {
92 19
                $first = $name;
93 19
            }
94 19
            if (empty($c['project_root'])) {
95
                // Add a project root of none is set.
96 19
                $c['project_root'] = dirname($container->getParameter('kernel.root_dir'));
97 19
            }
98 19
            $c['name'] = $name;
99 19
            $c['locales'] = $config['locales'];
100 19
            $configurationServiceId = 'php_translation.configuration.'.$name;
101 19
            $configDef = $container->register($configurationServiceId, ConfigurationModel::class);
102 19
            $configDef->setPublic(false)->addArgument($c);
103 19
            $configurationManager->addMethodCall('addConfiguration', [$name, new Reference($configurationServiceId)]);
104
105
            /*
106
             * Configure storage chain service
107
             */
108 19
            $storageDefinition = new DefinitionDecorator('php_translation.storage.abstract');
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
109 19
            $storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
110 19
            $container->setDefinition('php_translation.storage.'.$name, $storageDefinition);
111
112
            // Add storages
113 19
            foreach ($c['remote_storage'] as $serviceId) {
114
                $storageDefinition->addMethodCall('addRemoteStorage', [new Reference($serviceId)]);
115 19
            }
116
117 19
            foreach ($c['local_storage'] as $serviceId) {
118 19
                if ($serviceId !== 'php_translation.local_file_storage.abstract') {
119
                    $storageDefinition->addMethodCall('addLocalStorage', [new Reference($serviceId)]);
120
121
                    continue;
122
                }
123
124 19
                $def = new DefinitionDecorator($serviceId);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
125 19
                $def->replaceArgument(2, [$c['output_dir']])
126 19
                    ->replaceArgument(3, [$c['local_file_storage_options']])
127 19
                    ->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]);
128 19
                $container->setDefinition('php_translation.single_storage.file.'.$name, $def);
129 19
            }
130 19
        }
131
132 19
        if ($first !== null) {
133
            // Create some aliases for the default storage
134 19
            $container->setAlias('php_translation.storage', 'php_translation.storage.'.$first);
135 19
            if ($first !== 'default') {
136 7
                $container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first);
137 7
            }
138 19
        }
139 19
    }
140
141
    /**
142
     * Handle config for WebUI.
143
     *
144
     * @param ContainerBuilder $container
145
     * @param array            $config
146
     */
147 12
    private function enableWebUi(ContainerBuilder $container, array $config)
148
    {
149 12
        $container->setParameter('php_translation.webui.enabled', true);
150 12
        $container->setParameter('php_translation.webui.allow_create', $config['webui']['allow_create']);
151 12
        $container->setParameter('php_translation.webui.allow_delete', $config['webui']['allow_delete']);
152
153 12
        $path = $config['webui']['file_base_path'];
154 12
        if (null === $path) {
155 12
            if ($container->hasParameter('kernel.project_dir')) {
156
                $path = $container->getParameter('kernel.project_dir');
157
            } else {
158 12
                $path = $container->getParameter('kernel.root_dir').'/..';
159
            }
160 12
        }
161
162 12
        $container->setParameter('php_translation.webui.file_base_path', rtrim($path, '/').'/');
163 12
    }
164
165
    /**
166
     * Handle config for EditInPlace.
167
     *
168
     * @param ContainerBuilder $container
169
     * @param array            $config
170
     */
171 12
    private function enableEditInPlace(ContainerBuilder $container, array $config)
172
    {
173 12
        $name = $config['edit_in_place']['config_name'];
174
175 12
        if ($name !== 'default' && !isset($config['configs'][$name])) {
176
            throw new InvalidArgumentException(sprintf('There is no config named "%s".', $name));
177
        }
178
179 12
        $activatorRef = new Reference($config['edit_in_place']['activator']);
180
181 12
        $def = $container->getDefinition('php_translation.edit_in_place.response_listener');
182 12
        $def->replaceArgument(0, $activatorRef);
183 12
        $def->replaceArgument(3, $name);
184 12
        $def->replaceArgument(4, $config['edit_in_place']['show_untranslatable']);
185
186 12
        $def = $container->getDefinition('php_translator.edit_in_place.xtrans_html_translator');
187 12
        $def->replaceArgument(1, $activatorRef);
188 12
    }
189
190
    /**
191
     * Handle config for Symfony Profiler.
192
     *
193
     * @param ContainerBuilder $container
194
     * @param array            $config
195
     */
196 19
    private function enableSymfonyProfiler(ContainerBuilder $container, array $config)
197
    {
198 19
        $container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);
199 19
    }
200
201
    /**
202
     * Handle config for fallback auto translate.
203
     *
204
     * @param ContainerBuilder $container
205
     * @param array            $config
206
     */
207 1
    private function enableFallbackAutoTranslator(ContainerBuilder $container, array $config)
208
    {
209 1
        $externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service'];
210 1
        $externalTranslatorDef = $container->getDefinition($externalTranslatorId);
211 1
        $externalTranslatorDef->addTag('php_translation.external_translator');
212 1
        $externalTranslatorDef->addArgument(new Reference($config['http_client']));
213 1
        $externalTranslatorDef->addArgument(new Reference($config['message_factory']));
214
215 1
        $container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']);
216 1
    }
217
218
    /**
219
     * {@inheritdoc}
220
     */
221 19
    public function getAlias()
222
    {
223 19
        return 'translation';
224
    }
225
}
226