Completed
Pull Request — 5.2 (#882)
by Alexander
01:28
created

ONGRElasticsearchExtension::load()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 8.5937
c 0
b 0
f 0
cc 6
nc 16
nop 2
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\ElasticsearchBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\DependencyInjection\Loader;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\HttpKernel\Kernel;
22
use Symfony\Component\HttpKernel\KernelEvents;
23
24
/**
25
 * This is the class that loads and manages bundle configuration.
26
 */
27
class ONGRElasticsearchExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $configs, ContainerBuilder $container)
33
    {
34
        $configuration = new Configuration();
35
        $config = $this->processConfiguration($configuration, $configs);
36
37
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
38
        $loader->load('services.yml');
39
40
        if (Kernel::MAJOR_VERSION >= 4) {
41
            $loader->load('services4.yaml');
42
        }
43
44
        $config['cache'] = isset($config['cache']) ?
45
            $config['cache'] : !$container->getParameter('kernel.debug');
46
        $config['profiler'] = isset($config['profiler']) ?
47
            $config['profiler'] : $container->getParameter('kernel.debug');
48
49
        
50
        $managers = $config['managers'];
51
        foreach ($managers as &$manager) {
52
            $manager['mappings'] = array_unique($manager['mappings']);
53
        }
54
        
55
        $container->setParameter('es.cache', $config['cache']);
56
        $container->setParameter('es.analysis', $config['analysis']);
57
        $container->setParameter('es.managers', $managers);
58
        $definition = new Definition(
59
            'ONGR\ElasticsearchBundle\Service\ManagerFactory',
60
            [
61
                new Reference('es.metadata_collector'),
62
                new Reference('es.result_converter'),
63
                $config['profiler'] ? new Reference('es.tracer') : null,
64
            ]
65
        );
66
        $definition->addMethodCall('setEventDispatcher', [new Reference('event_dispatcher')]);
67
        $definition->addMethodCall(
68
            'setStopwatch',
69
            [
70
                new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE)
71
            ]
72
        );
73
        $definition->setPublic(true);
74
        $container->setDefinition('es.manager_factory', $definition);
75
    }
76
}
77