|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Algatux\InfluxDbBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use InfluxDB\Database; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
12
|
|
|
use Symfony\Component\Form\FormInterface; |
|
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This is the class that loads and manages your bundle configuration. |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://symfony.com/doc/current/cookbook/bundles/extension.html |
|
19
|
|
|
*/ |
|
20
|
|
|
final class InfluxDbExtension extends Extension |
|
21
|
|
|
{ |
|
22
|
|
|
const PROTOCOL_UDP = 'udp'; |
|
23
|
|
|
|
|
24
|
|
|
const PROTOCOL_HTTP = 'http'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
5 |
|
public function load(array $configs, ContainerBuilder $container) |
|
30
|
|
|
{ |
|
31
|
5 |
|
$configuration = new Configuration(); |
|
32
|
5 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
33
|
|
|
|
|
34
|
5 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
35
|
5 |
|
$loader->load('factory.xml'); |
|
36
|
5 |
|
$loader->load('registry.xml'); |
|
37
|
5 |
|
$loader->load('command.xml'); |
|
38
|
|
|
|
|
39
|
|
|
// If the default connection if not defined, get the first one. |
|
40
|
5 |
|
$defaultConnection = isset($config['default_connection']) ? $config['default_connection'] : key($config['connections']); |
|
41
|
5 |
|
$this->buildConnections($container, $config, $defaultConnection); |
|
42
|
|
|
|
|
43
|
5 |
|
$this->setDefaultConnectionAlias($container, $defaultConnection); |
|
44
|
|
|
|
|
45
|
5 |
|
if (interface_exists(FormInterface::class)) { |
|
46
|
5 |
|
$loader->load('form.xml'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
return $config; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ContainerBuilder $container |
|
54
|
|
|
* @param string $connection The connection name |
|
55
|
|
|
* @param array $config The connection configuration |
|
56
|
|
|
* @param string $protocol The connection protocol ('http' or 'udp') |
|
57
|
|
|
*/ |
|
58
|
5 |
|
private function createConnection(ContainerBuilder $container, $connection, array $config, $protocol) |
|
59
|
|
|
{ |
|
60
|
|
|
// Create the connection based from the abstract one. |
|
61
|
5 |
|
$connectionDefinition = new Definition(Database::class, [ |
|
62
|
5 |
|
$config['database'], |
|
63
|
5 |
|
$config['host'], |
|
64
|
5 |
|
$config['http_port'], |
|
65
|
5 |
|
$config['udp_port'], |
|
66
|
5 |
|
$config['username'], |
|
67
|
5 |
|
$config['password'], |
|
68
|
5 |
|
self::PROTOCOL_UDP === $protocol, |
|
69
|
5 |
|
$config['ssl'], |
|
70
|
5 |
|
$config['ssl_verification'], |
|
71
|
5 |
|
$config['timeout'], |
|
72
|
5 |
|
$config['connect_timeout'], |
|
73
|
|
|
]); |
|
74
|
5 |
|
$connectionDefinition->setFactory([new Reference('algatux_influx_db.connection_factory'), 'createConnection']); |
|
75
|
5 |
|
$connectionDefinition->setPublic(true); |
|
76
|
5 |
|
$connectionDefinition->setLazy(true); |
|
77
|
|
|
|
|
78
|
|
|
// E.g.: algatux_influx_db.connection.default.http |
|
79
|
5 |
|
$connectionServiceName = 'algatux_influx_db.connection.'.$connection.'.'.$protocol; |
|
80
|
5 |
|
$container->setDefinition($connectionServiceName, $connectionDefinition); |
|
81
|
|
|
|
|
82
|
|
|
// Add the connection to the registry |
|
83
|
5 |
|
$container->getDefinition('algatux_influx_db.connection_registry') |
|
84
|
5 |
|
->addMethodCall('addConnection', [$connection, $protocol, new Reference($connectionServiceName)]); |
|
85
|
5 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param ContainerBuilder $container |
|
89
|
|
|
* @param string $connection |
|
90
|
|
|
* @param array $config |
|
91
|
|
|
* @param string $defaultConnection |
|
92
|
|
|
*/ |
|
93
|
5 |
|
private function createConnectionListener(ContainerBuilder $container, $connection, array $config, string $defaultConnection) |
|
94
|
|
|
{ |
|
95
|
5 |
|
if (!$config['listener_enabled']) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$listenerArguments = [ |
|
100
|
5 |
|
$connection, |
|
101
|
5 |
|
$connection === $defaultConnection, |
|
102
|
5 |
|
new Reference('algatux_influx_db.connection.'.$connection.'.http'), |
|
103
|
|
|
]; |
|
104
|
5 |
|
if ($container->hasDefinition('algatux_influx_db.connection.'.$connection.'.udp')) { |
|
105
|
2 |
|
array_push($listenerArguments, new Reference('algatux_influx_db.connection.'.$connection.'.udp')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
5 |
|
$listenerDefinition = new Definition($config['listener_class'], $listenerArguments); |
|
109
|
5 |
|
$listenerDefinition->addTag('kernel.event_listener', [ |
|
110
|
5 |
|
'event' => 'influxdb.points_collected', |
|
111
|
|
|
'method' => 'onPointsCollected', |
|
112
|
|
|
]); |
|
113
|
5 |
|
$listenerDefinition->addTag('kernel.event_listener', [ |
|
114
|
5 |
|
'event' => 'kernel.terminate', |
|
115
|
|
|
'method' => 'onKernelTerminate', |
|
116
|
|
|
]); |
|
117
|
5 |
|
$listenerDefinition->addTag('kernel.event_listener', [ |
|
118
|
5 |
|
'event' => 'console.terminate', |
|
119
|
|
|
'method' => 'onConsoleTerminate', |
|
120
|
|
|
]); |
|
121
|
|
|
|
|
122
|
5 |
|
$container->setDefinition('algatux_influx_db.event_listener.'.$connection, $listenerDefinition); |
|
123
|
5 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param ContainerBuilder $container |
|
127
|
|
|
* @param array $config |
|
128
|
|
|
* @param string $defaultConnection |
|
129
|
|
|
*/ |
|
130
|
5 |
|
private function buildConnections(ContainerBuilder $container, array $config, string $defaultConnection) |
|
131
|
|
|
{ |
|
132
|
5 |
|
foreach ($config['connections'] as $connection => $connectionConfig) { |
|
133
|
5 |
|
$this->createConnection($container, $connection, $connectionConfig, self::PROTOCOL_HTTP); |
|
134
|
5 |
|
if ($connectionConfig['udp']) { |
|
135
|
2 |
|
$this->createConnection($container, $connection, $connectionConfig, self::PROTOCOL_UDP); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
5 |
|
$this->createConnectionListener($container, $connection, $connectionConfig, $defaultConnection); |
|
139
|
|
|
} |
|
140
|
5 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param ContainerBuilder $container |
|
144
|
|
|
* @param string $defaultConnection |
|
145
|
|
|
*/ |
|
146
|
5 |
|
private function setDefaultConnectionAlias(ContainerBuilder $container, string $defaultConnection) |
|
147
|
|
|
{ |
|
148
|
5 |
|
$container->setAlias( |
|
149
|
5 |
|
'algatux_influx_db.connection.http', |
|
150
|
5 |
|
new Alias('algatux_influx_db.connection.'.$defaultConnection.'.http', true) |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
5 |
|
if ($container->hasDefinition('algatux_influx_db.connection.'.$defaultConnection.'.udp')) { |
|
154
|
2 |
|
$container->setAlias( |
|
155
|
2 |
|
'algatux_influx_db.connection.udp', |
|
156
|
2 |
|
new Alias('algatux_influx_db.connection.'.$defaultConnection.'.udp', true) |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
// Set the default connection name on the registry constructor. |
|
161
|
5 |
|
$container->getDefinition('algatux_influx_db.connection_registry') |
|
162
|
5 |
|
->setArguments([$defaultConnection]); |
|
163
|
5 |
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|