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\Exception\LogicException; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Extension class for ElasticaExtension |
17
|
|
|
* |
18
|
|
|
* @author gbprod <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ElasticaExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
const CLIENT_ID_TEMPLATE = 'elastica.%s_client'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
11 |
|
public function load(array $configs, ContainerBuilder $container) |
28
|
|
|
{ |
29
|
11 |
|
$configuration = new Configuration(); |
30
|
11 |
|
$config = $this->processConfiguration($configuration, $configs); |
31
|
|
|
|
32
|
11 |
|
$loader = new Loader\YamlFileLoader( |
33
|
11 |
|
$container, |
34
|
11 |
|
new FileLocator(__DIR__ . '/../Resources/config') |
35
|
11 |
|
); |
36
|
|
|
|
37
|
11 |
|
$loader->load('services.yml'); |
38
|
|
|
|
39
|
11 |
|
$this->loadLogger($config, $container); |
40
|
11 |
|
$this->loadClients($config, $container); |
41
|
11 |
|
$this->setupAutowire($config, $container); |
42
|
10 |
|
} |
43
|
|
|
|
44
|
11 |
|
private function loadLogger(array $config, ContainerBuilder $container) |
45
|
|
|
{ |
46
|
|
|
$definition = $container |
47
|
11 |
|
->register('elastica.logger', ElasticaLogger::class) |
48
|
11 |
|
->addArgument($this->createLoggerReference($config)) |
49
|
11 |
|
->addArgument('%kernel.debug%') |
50
|
11 |
|
->setPublic(true); |
51
|
|
|
|
52
|
11 |
|
if ('logger' === $config['logger']) { |
53
|
9 |
|
$definition->addTag('monolog.logger', ['channel' => 'elastica']); |
54
|
9 |
|
} |
55
|
11 |
|
} |
56
|
|
|
|
57
|
11 |
|
private function createLoggerReference(array $config) |
58
|
|
|
{ |
59
|
11 |
|
if (null !== $config['logger']) { |
60
|
10 |
|
return new Reference( |
61
|
10 |
|
$config['logger'], |
62
|
|
|
ContainerInterface::IGNORE_ON_INVALID_REFERENCE |
63
|
10 |
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param array $config |
71
|
|
|
* @param ContainerBuilder $container |
72
|
|
|
*/ |
73
|
11 |
|
private function loadClients(array $config, ContainerBuilder $container) |
74
|
|
|
{ |
75
|
11 |
|
foreach ($config['clients'] as $clientName => $clientConfig) { |
76
|
6 |
|
$this->loadClient($clientName, $clientConfig, $container); |
77
|
11 |
|
} |
78
|
11 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $clientName |
82
|
|
|
* @param array $clientConfig |
83
|
|
|
* @param ContainerBuilder $container |
84
|
|
|
*/ |
85
|
6 |
|
private function loadClient($clientName, array $clientConfig, ContainerBuilder $container) |
86
|
|
|
{ |
87
|
|
|
$container |
88
|
6 |
|
->register($this->createClientId($clientName), Client::class) |
89
|
6 |
|
->addArgument($clientConfig) |
90
|
6 |
|
->addMethodCall('setLogger', [ |
91
|
6 |
|
new Reference('elastica.logger') |
92
|
6 |
|
]) |
93
|
6 |
|
->addMethodCall('setConfigValue', [ |
94
|
6 |
|
'log', |
95
|
6 |
|
$container->getParameter('kernel.debug') |
96
|
6 |
|
]) |
97
|
6 |
|
->setPublic(true); |
98
|
6 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Configure service auto-wiring for default Elastica client |
102
|
|
|
* for Symfony 3.3+ |
103
|
|
|
* |
104
|
|
|
* @param array $config |
105
|
|
|
* @param ContainerBuilder $container |
106
|
|
|
* @throws LogicException |
107
|
|
|
*/ |
108
|
11 |
|
private function setupAutowire(array $config, ContainerBuilder $container) |
109
|
|
|
{ |
110
|
11 |
|
if (!method_exists($container, 'autowire')) { |
111
|
|
|
// This container have no support for services auto-wiring |
112
|
|
|
return; |
113
|
|
|
} |
114
|
11 |
|
if (!$config['autowire']) { |
115
|
|
|
// Auto-wiring for default client is explicitly disabled |
116
|
1 |
|
return; |
117
|
|
|
} |
118
|
10 |
|
if (!array_key_exists('default', $config['clients'])) { |
119
|
|
|
// No "default" client is available |
120
|
6 |
|
return; |
121
|
|
|
} |
122
|
4 |
|
if ($container->hasDefinition(Client::class)) { |
123
|
1 |
|
throw new LogicException('Default Elasticsearch client autowiring setup is enabled, ' . |
124
|
1 |
|
'but Elastica client service is already defined in container'); |
125
|
|
|
} |
126
|
3 |
|
$container->setAlias(Client::class, $this->createClientId('default')); |
127
|
3 |
|
} |
128
|
|
|
|
129
|
6 |
|
private function createClientId($clientName) |
130
|
|
|
{ |
131
|
6 |
|
return sprintf( |
132
|
6 |
|
self::CLIENT_ID_TEMPLATE, |
133
|
|
|
$clientName |
134
|
6 |
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|