|
5 | 5 | namespace MinVWS\AuditLoggerBundle; |
6 | 6 |
|
7 | 7 | use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; |
| 8 | +use MinVWS\AuditLogger\AuditLogger; |
| 9 | +use MinVWS\AuditLogger\Handlers\EncryptionHandler; |
| 10 | +use MinVWS\AuditLogger\Loggers\FileLogger; |
| 11 | +use MinVWS\AuditLogger\Loggers\PsrLogger; |
| 12 | +use MinVWS\AuditLoggerBundle\Loggers\DoctrineLogger; |
| 13 | +use MinVWS\AuditLoggerBundle\Loggers\RabbitmqLogger; |
| 14 | +use OldSound\RabbitMqBundle\OldSoundRabbitMqBundle; |
| 15 | +use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; |
8 | 16 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Definition; |
| 18 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 19 | +use Symfony\Component\DependencyInjection\Reference; |
9 | 20 | use Symfony\Component\HttpKernel\Bundle\AbstractBundle; |
10 | 21 |
|
11 | 22 | class AuditLoggerBundle extends AbstractBundle |
12 | 23 | { |
13 | | - public function build(ContainerBuilder $container) |
| 24 | + public function configure(DefinitionConfigurator $definition): void |
| 25 | + { |
| 26 | + $definition->rootNode() |
| 27 | + ->children() |
| 28 | + ->arrayNode('encryption') |
| 29 | + ->children() |
| 30 | + ->scalarNode('public_key')->end() |
| 31 | + ->scalarNode('private_key')->end() |
| 32 | + ->end() |
| 33 | + ->end() |
| 34 | + ->arrayNode('loggers') |
| 35 | + ->isRequired() |
| 36 | + ->children() |
| 37 | + ->arrayNode('psr_logger') |
| 38 | + ->canBeUnset() |
| 39 | + ->children() |
| 40 | + ->booleanNode('enabled')->defaultFalse()->end() |
| 41 | + ->booleanNode('encrypted')->defaultFalse()->end() |
| 42 | + ->booleanNode('log_pii')->defaultFalse()->end() |
| 43 | + ->end() |
| 44 | + ->end() |
| 45 | + ->arrayNode('doctrine_logger') |
| 46 | + ->canBeUnset() |
| 47 | + ->children() |
| 48 | + ->booleanNode('enabled')->defaultFalse()->end() |
| 49 | + ->booleanNode('encrypted')->defaultFalse()->end() |
| 50 | + ->booleanNode('log_pii')->defaultFalse()->end() |
| 51 | + ->end() |
| 52 | + ->end() |
| 53 | + ->arrayNode('file_logger') |
| 54 | + ->canBeUnset() |
| 55 | + ->children() |
| 56 | + ->booleanNode('enabled')->defaultFalse()->end() |
| 57 | + ->booleanNode('encrypted')->defaultFalse()->end() |
| 58 | + ->booleanNode('log_pii')->defaultFalse()->end() |
| 59 | + ->scalarNode('path')->end() |
| 60 | + ->end() |
| 61 | + ->end() |
| 62 | + ->arrayNode('rabbitmq_logger') |
| 63 | + ->canBeUnset() |
| 64 | + ->children() |
| 65 | + ->booleanNode('enabled')->defaultFalse()->end() |
| 66 | + ->booleanNode('encrypted')->defaultFalse()->end() |
| 67 | + ->booleanNode('log_pii')->defaultFalse()->end() |
| 68 | + ->scalarNode('producer_service')->isRequired()->end() |
| 69 | + ->scalarNode('routing_key')->defaultValue("")->end() |
| 70 | + ->arrayNode('additional_events') |
| 71 | + ->beforeNormalization()->castToArray()->end() |
| 72 | + ->end() |
| 73 | + ->end() |
| 74 | + ->end() |
| 75 | + ->end() |
| 76 | + ->end(); |
| 77 | + } |
| 78 | + |
| 79 | + public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void |
| 80 | + { |
| 81 | + $container->import('Resources/config/services.yml'); |
| 82 | + |
| 83 | + if (isset($config['loggers']['psr_logger'])) { |
| 84 | + $builder->getDefinition(PsrLogger::class)->setArgument(2, $config['loggers']['psr_logger']['log_pii']); |
| 85 | + |
| 86 | + // Create and add encryption class |
| 87 | + $definition = new Definition(EncryptionHandler::class); |
| 88 | + $definition->setArgument(0, $config['loggers']['psr_logger']['encrypted']); |
| 89 | + $definition->setArgument(1, $config['encryption']['public_key']); |
| 90 | + $definition->setArgument(2, $config['encryption']['private_key']); |
| 91 | + $builder->getDefinition(PsrLogger::class)->setArgument(0, $definition); |
| 92 | + |
| 93 | + $definition = $builder->getDefinition(AuditLogger::class); |
| 94 | + $definition->addMethodCall('addLogger', [new Reference(PsrLogger::class)]); |
| 95 | + } |
| 96 | + |
| 97 | + if (isset($config['loggers']['file_logger'])) { |
| 98 | + $builder->getDefinition(FileLogger::class)->setArgument(1, $config['loggers']['file_logger']['path']); |
| 99 | + $builder->getDefinition(FileLogger::class)->setArgument(2, $config['loggers']['file_logger']['log_pii']); |
| 100 | + |
| 101 | + // Create and add encryption class |
| 102 | + $definition = new Definition(EncryptionHandler::class); |
| 103 | + $definition->setArgument(0, $config['loggers']['file_logger']['encrypted']); |
| 104 | + $definition->setArgument(1, $config['encryption']['public_key']); |
| 105 | + $definition->setArgument(2, $config['encryption']['private_key']); |
| 106 | + $builder->getDefinition(FileLogger::class)->setArgument(0, $definition); |
| 107 | + |
| 108 | + $definition = $builder->getDefinition(AuditLogger::class); |
| 109 | + $definition->addMethodCall('addLogger', [new Reference(FileLogger::class)]); |
| 110 | + } |
| 111 | + |
| 112 | + if (isset($config['loggers']['doctrine_logger'])) { |
| 113 | + $builder |
| 114 | + ->getDefinition(DoctrineLogger::class) |
| 115 | + ->setArgument(2, $config['loggers']['doctrine_logger']['log_pii']); |
| 116 | + |
| 117 | + // Create and add encryption class |
| 118 | + $definition = new Definition(EncryptionHandler::class); |
| 119 | + $definition->setArgument(0, $config['loggers']['doctrine_logger']['encrypted']); |
| 120 | + $definition->setArgument(1, $config['encryption']['public_key']); |
| 121 | + $definition->setArgument(2, $config['encryption']['private_key']); |
| 122 | + $builder->getDefinition(DoctrineLogger::class)->setArgument(0, $definition); |
| 123 | + |
| 124 | + $definition = $builder->getDefinition(AuditLogger::class); |
| 125 | + $definition->addMethodCall('addLogger', [new Reference(DoctrineLogger::class)]); |
| 126 | + } |
| 127 | + |
| 128 | + if (isset($config['loggers']['rabbitmq_logger'])) { |
| 129 | + if (!class_exists(OldSoundRabbitMqBundle::class, false)) { |
| 130 | + throw new \Exception( |
| 131 | + 'RabbitMQ logger is configured to log data, but the RabbitMQ bundle is not installed. ' |
| 132 | + . 'Please try and run "composer require php-amqplib/rabbitmq-bundle"', |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + // Create and add encryption class |
| 137 | + $definition = new Definition(EncryptionHandler::class); |
| 138 | + $definition->setArgument(0, $config['loggers']['rabbitmq_logger']['encrypted']); |
| 139 | + $definition->setArgument(1, $config['encryption']['public_key']); |
| 140 | + $definition->setArgument(2, $config['encryption']['private_key']); |
| 141 | + $builder->getDefinition(RabbitmqLogger::class)->setArgument(0, $definition); |
| 142 | + |
| 143 | + $builder |
| 144 | + ->getDefinition(RabbitmqLogger::class) |
| 145 | + ->setArgument(1, new Reference($config['loggers']['rabbitmq_logger']['producer_service'])); |
| 146 | + $builder |
| 147 | + ->getDefinition(RabbitmqLogger::class) |
| 148 | + ->setArgument(2, $config['loggers']['rabbitmq_logger']['routing_key']); |
| 149 | + $builder |
| 150 | + ->getDefinition(RabbitmqLogger::class) |
| 151 | + ->setArgument(3, $config['loggers']['rabbitmq_logger']['additional_events']); |
| 152 | + |
| 153 | + $definition = $builder->getDefinition(AuditLogger::class); |
| 154 | + $definition->addMethodCall('addLogger', [new Reference(RabbitmqLogger::class)]); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + public function build(ContainerBuilder $container): void |
14 | 159 | { |
15 | 160 | parent::build($container); |
16 | 161 |
|
|
0 commit comments