1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[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 ONGR\TranslationsBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* TranslatorPass to load translations loaders. |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class ManagerPass implements CompilerPassInterface |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function process(ContainerBuilder $container) |
28
|
|
|
{ |
29
|
|
|
$formats = $container->getParameter('ongr_translations.formats'); |
30
|
|
|
$loadersContainer = new Definition('Symfony\Component\HttpFoundation\ParameterBag'); |
31
|
|
|
|
32
|
|
|
foreach ($container->findTaggedServiceIds('translation.loader') as $id => $attributes) { |
33
|
|
|
if (!empty($formats)) { |
34
|
|
|
if (array_intersect($attributes[0], $formats)) { |
35
|
|
|
$this->addLoader($loadersContainer, $attributes, $id); |
36
|
|
|
} |
37
|
|
|
} else { |
38
|
|
|
$this->addLoader($loadersContainer, $attributes, $id); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$container->setDefinition('ongr_translations.loaders_container', $loadersContainer); |
43
|
|
|
|
44
|
|
|
$this->setImportLoadersContainer($container, $loadersContainer); |
45
|
|
|
$this->setExportLoadersContainer($container, $loadersContainer); |
46
|
|
|
$this->setComponentDirectories($container); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Adds loader to LoadersContainer definition. |
51
|
|
|
* |
52
|
|
|
* @param Definition $loadersContainer |
53
|
|
|
* @param array $attributes |
54
|
|
|
* @param string $id |
55
|
|
|
*/ |
56
|
|
|
public function addLoader($loadersContainer, $attributes, $id) |
57
|
|
|
{ |
58
|
|
|
$loadersContainer->addMethodCall( |
59
|
|
|
'set', |
60
|
|
|
[$attributes[0]['alias'], new Reference($id)] |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set import service LoadersContainer. |
66
|
|
|
* |
67
|
|
|
* @param ContainerBuilder $container |
68
|
|
|
* @param Definition $loadersContainer |
69
|
|
|
*/ |
70
|
|
|
public function setImportLoadersContainer(ContainerBuilder $container, $loadersContainer) |
71
|
|
|
{ |
72
|
|
|
if ($container->hasDefinition('ongr_translations.file_import')) { |
73
|
|
|
$container->findDefinition('ongr_translations.file_import')->replaceArgument(0, $loadersContainer); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set export service LoadersContainer. |
79
|
|
|
* |
80
|
|
|
* @param ContainerBuilder $container |
81
|
|
|
* @param Definition $loadersContainer |
82
|
|
|
*/ |
83
|
|
|
public function setExportLoadersContainer(ContainerBuilder $container, $loadersContainer) |
84
|
|
|
{ |
85
|
|
|
if ($container->hasDefinition('ongr_translations.export')) { |
86
|
|
|
$container->findDefinition('ongr_translations.export')->replaceArgument(0, $loadersContainer); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ContainerBuilder $container |
92
|
|
|
*/ |
93
|
|
|
private function setComponentDirectories(ContainerBuilder $container) |
94
|
|
|
{ |
95
|
|
|
$container->setParameter( |
96
|
|
|
'ongr_translations.component_directories', |
97
|
|
|
[ |
98
|
|
|
'Symfony\Component\Validator\ValidatorBuilder', |
99
|
|
|
'Symfony\Component\Form\Form', |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.