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
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
21
|
|
|
use Translation\Bundle\Service\StorageService; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This is the class that loads and manages your bundle configuration. |
25
|
|
|
* |
26
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
27
|
|
|
*/ |
28
|
|
|
class TranslationExtension extends Extension |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
34
|
|
|
{ |
35
|
1 |
|
$configuration = new Configuration($container); |
36
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
37
|
1 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
38
|
|
|
|
39
|
1 |
|
$loader->load('services.yml'); |
40
|
1 |
|
$loader->load('extractors.yml'); |
41
|
|
|
|
42
|
|
|
// Add major version to extractor |
43
|
1 |
|
$container->getDefinition('php_translation.extractor.php.visitor.FormTypeChoices') |
44
|
1 |
|
->addMethodCall('setSymfonyMajorVersion', [Kernel::MAJOR_VERSION]); |
45
|
|
|
|
46
|
1 |
|
$container->setParameter('php_translation.locales', $config['locales']); |
47
|
1 |
|
$container->setParameter('php_translation.default_locale', isset($config['default_locale']) ? $config['default_locale'] : $container->getParameter('kernel.default_locale')); |
48
|
|
|
|
49
|
1 |
|
if ($config['webui']['enabled']) { |
50
|
1 |
|
$this->enableWebUi($container, $config); |
|
|
|
|
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
if ($config['symfony_profiler']['enabled']) { |
54
|
|
|
$loader->load('symfony_profiler.yml'); |
55
|
|
|
$this->enableSymfonyProfiler($container, $config); |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
if ($config['fallback_translation']['enabled']) { |
59
|
|
|
$loader->load('auto_translation.yml'); |
60
|
|
|
$this->enableFallbackAutoTranslator($container, $config); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$first = null; |
64
|
1 |
|
foreach ($config['configs'] as $name => &$c) { |
65
|
|
|
if ($first === null || $name === 'default') { |
66
|
|
|
$first = $name; |
67
|
|
|
} |
68
|
|
|
if (empty($c['project_root'])) { |
69
|
|
|
$c['project_root'] = dirname($container->getParameter('kernel.root_dir')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$storageDefinition = $container->register('php_translation.storage.'.$name, StorageService::class); |
73
|
|
|
|
74
|
|
|
// Register a file storage |
75
|
|
|
$def = new DefinitionDecorator('php_translation.single_storage.file.abstract'); |
76
|
|
|
$def->replaceArgument(2, $c['output_dir']) |
77
|
|
|
->addTag('php_translation.storage', ['type' => 'local', 'name' => $name]); |
78
|
|
|
$container->setDefinition('php_translation.single_storage.file.'.$name, $def); |
79
|
|
|
|
80
|
|
|
// Add storages |
81
|
|
|
if (!empty($c['remote_storage'])) { |
82
|
|
|
foreach ($c['remote_storage'] as $serviceId) { |
83
|
|
|
$storageDefinition->addMethodCall('addRemoteStorage', [new Reference($serviceId)]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
if (!empty($c['local_storage'])) { |
87
|
|
|
foreach ($c['local_storage'] as $serviceId) { |
88
|
|
|
$storageDefinition->addMethodCall('addLocalStorage', [new Reference($serviceId)]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
|
93
|
1 |
|
if ($first !== null) { |
94
|
|
|
// Create some aliases for the default storage |
95
|
|
|
$container->setAlias('php_translation.storage', 'php_translation.storage.'.$first); |
96
|
|
|
$container->setAlias('php_translation.storage.default', 'php_translation.storage.'.$first); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$container->getDefinition('php_translation.configuration_manager') |
100
|
1 |
|
->replaceArgument(0, $config['configs']); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
private function enableWebUi(ContainerBuilder $container, $config) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function enableSymfonyProfiler(ContainerBuilder $container, $config) |
108
|
|
|
{ |
109
|
|
|
$container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function enableFallbackAutoTranslator(ContainerBuilder $container, $config) |
113
|
|
|
{ |
114
|
|
|
$externalTranslatorId = 'php_translation.translator_service.'.$config['fallback_translation']['service']; |
115
|
|
|
$externalTranslatorDef = $container->getDefinition($externalTranslatorId); |
116
|
|
|
$externalTranslatorDef->addTag('php_translation.external_translator'); |
117
|
|
|
$externalTranslatorDef->addArgument(new Reference($config['http_client'])); |
118
|
|
|
$externalTranslatorDef->addArgument(new Reference($config['message_factory'])); |
119
|
|
|
|
120
|
|
|
$container->setParameter('php_translation.translator_service.api_key', $config['fallback_translation']['api_key']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getAlias() |
124
|
|
|
{ |
125
|
|
|
return 'translation'; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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: