-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSqlLoggerPass.php
51 lines (44 loc) · 1.85 KB
/
SqlLoggerPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Okvpn\Bundle\DatadogBundle\DependencyInjection\CompilerPass;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class SqlLoggerPass implements CompilerPassInterface
{
private $connections;
public function __construct(array $connections)
{
$this->connections = $connections;
}
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (false === $container->hasDefinition('okvpn_datadog.logger.sql')) {
return;
}
foreach ($this->connections as $name) {
$configuration = sprintf('doctrine.dbal.%s_connection.configuration', $name);
if (false === $container->hasDefinition($configuration)) {
continue;
}
$loggerId = 'okvpn_datadog.sql_logger.' . $name;
$container->setDefinition($loggerId, new ChildDefinition('okvpn_datadog.logger.sql'));
$configuration = $container->getDefinition($configuration);
if ($configuration->hasMethodCall('setSQLLogger')) {
$chainLoggerId = 'doctrine.dbal.logger.chain.' . $name;
if ($container->hasDefinition($chainLoggerId)) {
$chainLogger = $container->getDefinition($chainLoggerId);
$chainLogger->addMethodCall('addLogger', [new Reference($loggerId)]);
} else {
$configuration->addMethodCall('setSQLLogger', [new Reference($loggerId)]);
}
} else {
$configuration->addMethodCall('setSQLLogger', [new Reference($loggerId)]);
}
}
}
}