1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DH\DoctrineAuditBundle\Event; |
4
|
|
|
|
5
|
|
|
use DH\DoctrineAuditBundle\DBAL\Logger; |
6
|
|
|
use DH\DoctrineAuditBundle\DBAL\LoggerChain; |
7
|
|
|
use DH\DoctrineAuditBundle\Model\Transaction; |
8
|
|
|
use DH\DoctrineAuditBundle\Transaction\TransactionManager; |
9
|
|
|
use Doctrine\Common\EventSubscriber; |
10
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
11
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
12
|
|
|
use Doctrine\ORM\Events; |
13
|
|
|
|
14
|
|
|
class DoctrineSubscriber implements EventSubscriber |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var TransactionManager |
18
|
|
|
*/ |
19
|
|
|
private $transactionManager; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ?SQLLogger |
23
|
|
|
*/ |
24
|
|
|
private $loggerBackup; |
25
|
|
|
|
26
|
|
|
public function __construct(TransactionManager $transactionManager) |
27
|
|
|
{ |
28
|
|
|
$this->transactionManager = $transactionManager; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* It is called inside EntityManager#flush() after the changes to all the managed entities |
33
|
|
|
* and their associations have been computed. |
34
|
|
|
* |
35
|
|
|
* @see https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush |
36
|
|
|
* |
37
|
|
|
* @param OnFlushEventArgs $args |
38
|
|
|
* |
39
|
|
|
* @throws \Doctrine\DBAL\DBALException |
40
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException |
41
|
|
|
*/ |
42
|
|
|
public function onFlush(OnFlushEventArgs $args): void |
43
|
|
|
{ |
44
|
|
|
$em = $args->getEntityManager(); |
45
|
|
|
$transaction = new Transaction(); |
46
|
|
|
|
47
|
|
|
// extend the SQL logger |
48
|
|
|
$this->loggerBackup = $em->getConnection()->getConfiguration()->getSQLLogger(); |
49
|
|
|
$auditLogger = new Logger(function () use ($em, $transaction): void { |
50
|
|
|
// flushes pending data |
51
|
|
|
$em->getConnection()->getConfiguration()->setSQLLogger($this->loggerBackup); |
52
|
|
|
$this->transactionManager->process($transaction); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
// Initialize a new LoggerChain with the new AuditLogger + the existing SQLLoggers. |
56
|
|
|
$loggerChain = new LoggerChain(); |
57
|
|
|
$loggerChain->addLogger($auditLogger); |
58
|
|
|
if ($this->loggerBackup instanceof LoggerChain) { |
59
|
|
|
/** @var SQLLogger $logger */ |
60
|
|
|
foreach ($this->loggerBackup->getLoggers() as $logger) { |
61
|
|
|
$loggerChain->addLogger($logger); |
62
|
|
|
} |
63
|
|
|
} elseif ($this->loggerBackup instanceof SQLLogger) { |
64
|
|
|
$loggerChain->addLogger($this->loggerBackup); |
65
|
|
|
} |
66
|
|
|
$em->getConnection()->getConfiguration()->setSQLLogger($loggerChain); |
67
|
|
|
|
68
|
|
|
// Populate transaction |
69
|
|
|
$this->transactionManager->populate($transaction); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getSubscribedEvents(): array |
76
|
|
|
{ |
77
|
|
|
return [Events::onFlush]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|