Completed
Pull Request — 5.2 (#911)
by Alexander
01:41
created

ONGRElasticsearchExtension::load()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.9848
c 0
b 0
f 0
cc 5
nc 8
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
        $container->setParameter('es.cache', $config['cache']);
50
        $container->setParameter('es.analysis', $config['analysis']);
51
        $container->setParameter('es.managers', $config['managers']);
52
        $definition = new Definition(
53
            'ONGR\ElasticsearchBundle\Service\ManagerFactory',
54
            [
55
                new Reference('es.metadata_collector'),
56
                new Reference('es.result_converter'),
57
                $config['profiler'] ? new Reference('es.tracer') : null,
58
                new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
59
            ]
60
        );
61
        $definition->addMethodCall('setEventDispatcher', [new Reference('event_dispatcher')]);
62
        $definition->addMethodCall(
63
            'setStopwatch',
64
            [
65
                new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE)
66
            ]
67
        );
68
        $definition->setPublic(true);
69
        $container->setDefinition('es.manager_factory', $definition);
70
    }
71
}
72