1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Thomas Rabaix <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class AddAuditEntityCompilerPass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
public function process(ContainerBuilder $container): void |
25
|
|
|
{ |
26
|
|
|
if (!$container->hasDefinition('simplethings_entityaudit.config')) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$auditedEntities = $container->getParameter('simplethings.entityaudit.audited_entities'); |
31
|
|
|
$force = $container->getParameter('sonata_doctrine_orm_admin.audit.force'); |
32
|
|
|
|
33
|
|
|
foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) { |
34
|
|
|
if ('orm' !== $attributes[0]['manager_type']) { |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (true === $force && isset($attributes[0]['audit']) && false === $attributes[0]['audit']) { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (false === $force && (!isset($attributes[0]['audit']) || false === $attributes[0]['audit'])) { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$definition = $container->getDefinition($id); |
47
|
|
|
$auditedEntities[] = $this->getModelName($container, $definition->getArgument(1)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$auditedEntities = array_unique($auditedEntities); |
51
|
|
|
|
52
|
|
|
$container->setParameter('simplethings.entityaudit.audited_entities', $auditedEntities); |
53
|
|
|
|
54
|
|
|
$auditManager = $container->getDefinition('sonata.admin.audit.manager'); |
55
|
|
|
$auditManager->addMethodCall('setReader', ['sonata.admin.audit.orm.reader', $auditedEntities]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getModelName(ContainerBuilder $container, string $name): string |
59
|
|
|
{ |
60
|
|
|
if ('%' === $name[0]) { |
61
|
|
|
return $container->getParameter(substr($name, 1, -1)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $name; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|