-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
SonataTranslationExtension.php
116 lines (98 loc) · 4.25 KB
/
SonataTranslationExtension.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
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\TranslationBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* @author Nicolas Bastien <nbastien.pro@gmail.com>
*/
class SonataTranslationExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
$container->setParameter('sonata_translation.locales', $config['locales']);
$container->setParameter('sonata_translation.default_locale', $config['default_locale']);
$isEnabled = false;
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
if ($config['locale_switcher']) {
$loader->load('service_locale_switcher.xml');
}
$bundles = $container->getParameter('kernel.bundles');
if (array_key_exists('SonataDoctrineORMAdminBundle', $bundles)) {
$loader->load('service_orm.xml');
}
$translationTargets = [];
if ($config['gedmo']['enabled']) {
$isEnabled = true;
$loader->load('service_gedmo.xml');
$translationTargets['gedmo']['implements'] = array_merge(
['Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface'],
$config['gedmo']['implements']
);
$translationTargets['gedmo']['instanceof'] = $config['gedmo']['instanceof'];
}
if ($config['knplabs']['enabled']) {
$isEnabled = true;
$loader->load('service_knplabs.xml');
$translationTargets['knplabs']['implements'] = array_merge(
['Sonata\TranslationBundle\Model\TranslatableInterface'],
$config['knplabs']['implements']
);
$translationTargets['knplabs']['instanceof'] = $config['knplabs']['instanceof'];
}
if ($config['phpcr']['enabled']) {
$isEnabled = true;
$loader->load('service_phpcr.xml');
$translationTargets['phpcr']['implements'] = array_merge(
[
'Sonata\TranslationBundle\Model\Phpcr\TranslatableInterface',
'Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface',
],
$config['phpcr']['implements']
);
$translationTargets['phpcr']['instanceof'] = $config['phpcr']['instanceof'];
}
if (true === $isEnabled) {
$loader->load('block.xml');
$loader->load('listener.xml');
$loader->load('twig.xml');
}
$container->setParameter('sonata_translation.targets', $translationTargets);
$this->configureChecker($container, $translationTargets);
}
/**
* @param ContainerBuilder $container
* @param array $translationTargets
*/
protected function configureChecker(ContainerBuilder $container, $translationTargets)
{
if (!$container->hasDefinition('sonata_translation.checker.translatable')) {
return;
}
$translatableCheckerDefinition = $container->getDefinition('sonata_translation.checker.translatable');
$supportedInterfaces = [];
$supportedModels = [];
foreach ($translationTargets as $targets) {
$supportedInterfaces = array_merge($supportedInterfaces, $targets['implements']);
$supportedModels = array_merge($supportedModels, $targets['instanceof']);
}
$translatableCheckerDefinition->addMethodCall('setSupportedInterfaces', [$supportedInterfaces]);
$translatableCheckerDefinition->addMethodCall('setSupportedModels', [$supportedModels]);
}
}