1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\AuditorBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use DH\Auditor\Provider\Doctrine\Auditing\Annotation\AnnotationLoader; |
6
|
|
|
use DH\Auditor\Provider\Doctrine\DoctrineProvider; |
7
|
|
|
use DH\Auditor\Provider\Doctrine\Service\AuditingService; |
8
|
|
|
use DH\Auditor\Provider\Doctrine\Service\StorageService; |
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
|
14
|
|
|
class DoctrineProviderConfigurationCompilerPass implements CompilerPassInterface |
15
|
|
|
{ |
16
|
|
|
public function process(ContainerBuilder $container): void |
17
|
|
|
{ |
18
|
|
|
if (!$container->hasDefinition(DoctrineProvider::class)) { |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$doctrineProviderConfigurationKey = 'dh_auditor.provider.doctrine.configuration'; |
23
|
|
|
if (!$container->hasParameter($doctrineProviderConfigurationKey)) { |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$providerDefinition = $container->getDefinition(DoctrineProvider::class); |
28
|
|
|
$config = $container->getParameter($doctrineProviderConfigurationKey); |
29
|
|
|
|
30
|
|
|
\assert(\is_array($config) && \array_key_exists('storage_services', $config)); |
31
|
|
|
foreach (array_unique($config['storage_services']) as $entityManagerName) { |
32
|
|
|
$entityManagerName = str_replace('@', '', $entityManagerName); |
33
|
|
|
$entityManagerReference = new Reference($entityManagerName); |
34
|
|
|
|
35
|
|
|
$service = 'dh_auditor.provider.doctrine.storage_services.'.$entityManagerName; |
36
|
|
|
$serviceDefinition = new Definition(StorageService::class, [ |
37
|
|
|
$service, |
38
|
|
|
$entityManagerReference, |
39
|
|
|
]); |
40
|
|
|
$container->setDefinition($service, $serviceDefinition); |
41
|
|
|
$serviceReference = new Reference($service); |
42
|
|
|
|
43
|
|
|
$providerDefinition->addMethodCall('registerStorageService', [$serviceReference]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
\assert(\is_array($config) && \array_key_exists('auditing_services', $config)); |
47
|
|
|
foreach (array_unique($config['auditing_services']) as $entityManagerName) { |
48
|
|
|
$entityManagerName = str_replace('@', '', $entityManagerName); |
49
|
|
|
$entityManagerReference = new Reference($entityManagerName); |
50
|
|
|
|
51
|
|
|
$service = 'dh_auditor.provider.doctrine.auditing_services.'.$entityManagerName; |
52
|
|
|
$serviceDefinition = new Definition(AuditingService::class, [ |
53
|
|
|
$service, |
54
|
|
|
$entityManagerReference, |
55
|
|
|
]); |
56
|
|
|
$container->setDefinition($service, $serviceDefinition); |
57
|
|
|
$serviceReference = new Reference($service); |
58
|
|
|
|
59
|
|
|
$annotationLoaderDefinition = new Definition(AnnotationLoader::class, [$entityManagerReference]); |
60
|
|
|
$container->setDefinition(AnnotationLoader::class, $annotationLoaderDefinition); |
61
|
|
|
|
62
|
|
|
$providerDefinition->addMethodCall('registerAuditingService', [$serviceReference]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|