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\Reference; |
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
19
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This is the class that loads and manages your bundle configuration. |
23
|
|
|
* |
24
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
25
|
|
|
*/ |
26
|
|
|
class TranslationExtension extends Extension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
32
|
|
|
{ |
33
|
1 |
|
$configuration = new Configuration($container); |
34
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
35
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
36
|
|
|
|
37
|
1 |
|
$loader->load('services.yml'); |
38
|
1 |
|
$loader->load('extractors.yml'); |
39
|
|
|
|
40
|
1 |
|
$container->setParameter('php_translation.locales', $config['locales']); |
41
|
1 |
|
$container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale')); |
42
|
|
|
|
43
|
1 |
|
if ($config['webui']['enabled']) { |
44
|
1 |
|
$this->enableWebUi($container, $config); |
|
|
|
|
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
if ($config['fallback_translation']['enabled']) { |
48
|
|
|
$loader->load('auto_translation.yml'); |
49
|
|
|
$this->enableFallbackAutoTranslator($container, $config); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
foreach ($config['configs'] as $name => &$c) { |
53
|
|
|
if (empty($c['project_root'])) { |
54
|
|
|
$c['project_root'] = dirname($container->getParameter('kernel.root_dir')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$def = new DefinitionDecorator('php_translation.storage.file.abstract'); |
58
|
|
|
$def->replaceArgument(2, $c['output_dir']); |
59
|
|
|
$container->setDefinition('php_translation.storage.file.'.$name, $def); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
1 |
|
$container->getDefinition('php_translation.configuration_manager') |
63
|
1 |
|
->replaceArgument(0, $config['configs']); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
private function enableWebUi(ContainerBuilder $container, $config) |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
private function enableFallbackAutoTranslator(ContainerBuilder $container, $config) |
71
|
|
|
{ |
72
|
|
|
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service']; |
73
|
|
|
$externalTranslatorDef = $container->getDefinition($externalTranslatorId); |
74
|
|
|
$externalTranslatorDef->addTag('php_translation.external_translator'); |
75
|
|
|
$externalTranslatorDef->addArgument(new Reference($config['http_client'])); |
76
|
|
|
$externalTranslatorDef->addArgument(new Reference($config['message_factory'])); |
77
|
|
|
|
78
|
|
|
$container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function getAlias() |
82
|
|
|
{ |
83
|
1 |
|
return 'translation'; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: