1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticaBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Elastica\Client; |
6
|
|
|
use GBProd\ElasticaBundle\Logger\ElasticaLogger; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Extension class for ElasticaExtension |
16
|
|
|
* |
17
|
|
|
* @author gbprod <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ElasticaExtension extends Extension |
20
|
|
|
{ |
21
|
|
|
const CLIENT_ID_TEMPLATE = 'elastica.%s_client'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
27
|
|
|
{ |
28
|
6 |
|
$configuration = new Configuration(); |
29
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
30
|
|
|
|
31
|
6 |
|
$loader = new Loader\YamlFileLoader( |
32
|
6 |
|
$container, |
33
|
6 |
|
new FileLocator(__DIR__.'/../Resources/config') |
34
|
6 |
|
); |
35
|
|
|
|
36
|
6 |
|
$loader->load('services.yml'); |
37
|
|
|
|
38
|
6 |
|
$this->loadLogger($config, $container); |
39
|
6 |
|
$this->loadClients($config, $container); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
6 |
|
private function loadLogger(array $config, ContainerBuilder $container) |
43
|
|
|
{ |
44
|
|
|
$definition = $container |
45
|
6 |
|
->register('elastica.logger', ElasticaLogger::class) |
46
|
6 |
|
->addArgument($this->createLoggerReference($config)) |
47
|
6 |
|
->addArgument('%kernel.debug%') |
48
|
6 |
|
; |
49
|
|
|
|
50
|
6 |
|
if ('logger' === $config['logger']) { |
51
|
4 |
|
$definition->addTag('monolog.logger', ['channel' => 'elastica']); |
52
|
4 |
|
} |
53
|
6 |
|
} |
54
|
|
|
|
55
|
6 |
|
private function createLoggerReference(array $config) |
56
|
|
|
{ |
57
|
6 |
|
if (null !== $config['logger']) { |
58
|
5 |
|
return new Reference( |
59
|
5 |
|
$config['logger'], |
60
|
|
|
ContainerInterface::IGNORE_ON_INVALID_REFERENCE |
61
|
5 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
private function loadClients(array $config, ContainerBuilder $container) |
68
|
|
|
{ |
69
|
6 |
|
foreach ($config['clients'] as $clientName => $clientConfig) { |
70
|
2 |
|
$this->loadClient($clientName, $clientConfig, $container); |
71
|
6 |
|
} |
72
|
6 |
|
} |
73
|
|
|
|
74
|
2 |
|
private function loadClient($clientName, array $clientConfig, ContainerBuilder $container) |
75
|
|
|
{ |
76
|
|
|
$container |
77
|
2 |
|
->register($this->createClientId($clientName), Client::class) |
78
|
2 |
|
->addArgument($clientConfig) |
79
|
2 |
|
->addMethodCall('setLogger', [ |
80
|
2 |
|
new Reference('elastica.logger') |
81
|
2 |
|
]) |
82
|
2 |
|
->addMethodCall('setConfigValue', [ |
83
|
2 |
|
'log', |
84
|
2 |
|
$container->getParameter('kernel.debug') |
85
|
2 |
|
]) |
86
|
|
|
; |
87
|
2 |
|
} |
88
|
|
|
|
89
|
2 |
|
private function createClientId($clientName) |
90
|
|
|
{ |
91
|
2 |
|
return sprintf( |
92
|
2 |
|
self::CLIENT_ID_TEMPLATE, |
93
|
|
|
$clientName |
94
|
2 |
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|