-
Notifications
You must be signed in to change notification settings - Fork 13
/
ONGRTranslationsExtension.php
200 lines (180 loc) · 6.82 KB
/
ONGRTranslationsExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\TranslationsBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* This is the class that loads and manages bundle configuration.
*/
class ONGRTranslationsExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
$loader->load('filters_container.yml');
$container->setParameter('ongr_translations.managed_locales', $config['managed_locales']);
$container->setParameter('ongr_translations.formats', $config['formats']);
$container->setParameter('ongr_translations.domains', $config['domains']);
$this->validateBundles($container, $config['bundles']);
$this->setElasticsearchStorage($config['repository'], $container);
$this->setFiltersManager($config['repository'], $container);
$this->setTranslationManager($config['repository'], $container);
$this->setControllerManager($config['repository'], 'list', $container);
$this->setHistoryManager($this->editRepositoryName($config['repository']), $container);
if ($config['history']) {
$this->setEditMessageEvent($this->editRepositoryName($config['repository']), $container);
}
}
/**
* Adds translations manager.
*
* @param string $repositoryId Elasticsearch repository id.
* @param ContainerBuilder $container Service container.
*/
private function setTranslationManager($repositoryId, ContainerBuilder $container)
{
$definition = new Definition(
'ONGR\TranslationsBundle\Translation\TranslationManager',
[
new Reference($repositoryId),
new Reference('event_dispatcher'),
]
);
$container->setDefinition('ongr_translations.translation_manager', $definition);
}
/**
* Adds history manager.
*
* @param string $repositoryId
* @param ContainerBuilder $container
*/
private function setHistoryManager($repositoryId, ContainerBuilder $container)
{
$definition = new Definition(
'ONGR\TranslationsBundle\Translation\HistoryManager',
[
new Reference($repositoryId),
]
);
$container->setDefinition('ongr_translations.history_manager', $definition);
}
/**
* Sets elasticsearch storage for translations.
*
* @param string $repositoryId Elasticsearch repository id.
* @param ContainerBuilder $container Service container.
*/
private function setElasticsearchStorage($repositoryId, ContainerBuilder $container)
{
$definition = new Definition(
'ONGR\TranslationsBundle\Storage\ElasticsearchStorage',
[
new Reference($repositoryId),
]
);
$container->setDefinition('ongr_translations.storage', $definition);
}
/**
* Adds filter manager for displaying translations gui.
*
* @param string $repositoryId Elasticsearch repository id.
* @param ContainerBuilder $container Service container.
*/
private function setFiltersManager($repositoryId, ContainerBuilder $container)
{
$definition = new Definition(
'ONGR\FilterManagerBundle\Search\FilterManager',
[
new Reference('ongr_translations.filters_container'),
new Reference($repositoryId),
]
);
$container->setDefinition('ongr_translations.filter_manager', $definition);
}
/**
* Injects elasticsearch repository to controller and sets it into service container.
*
* @param string $repositoryId Elasticsearch repository id.
* @param string $controllerName Controller name to which add repository.
* @param ContainerBuilder $container Service container.
*/
private function setControllerManager($repositoryId, $controllerName, ContainerBuilder $container)
{
$definition = new Definition(
sprintf('ONGR\TranslationsBundle\Controller\%sController', ucfirst($controllerName)),
[
new Reference($repositoryId),
]
);
$definition->addMethodCall('setContainer', [new Reference('service_container')]);
$container->setDefinition(sprintf('ongr_translations.controller.%s', $controllerName), $definition);
}
/**
* Validates configured bundles and sets into service container as parameter.
*
* @param ContainerBuilder $container Service container.
* @param array $bundles Bundles array.
*
* @throws InvalidConfigurationException
*/
private function validateBundles($container, $bundles)
{
foreach ($bundles as $bundle) {
if (!class_exists($bundle)) {
throw new InvalidConfigurationException(
"Invalid bundle namespace '{$bundle}'."
);
}
}
$container->setParameter('ongr_translations.bundles', $bundles);
}
/**
* Validates edit message event.
*
* @param string $repositoryId
* @param ContainerBuilder $container
*/
private function setEditMessageEvent($repositoryId, ContainerBuilder $container)
{
$definition = new Definition(
'ONGR\TranslationsBundle\Event\HistoryListener',
[
new Reference($repositoryId),
]
);
$definition->addTag(
'kernel.event_listener',
['event' => 'translation.history.add', 'method' => 'addToHistory']
);
$container->setDefinition('ongr_translations.es_manager', $definition);
}
/**
* Edits repository name.
*
* @param string $repository
*
* @return string
*/
private function editRepositoryName($repository)
{
return substr_replace($repository, 'history', strrpos($repository, '.') + 1);
}
}