|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or |
|
8
|
|
|
* modify it under the terms of the GNU General Public License |
|
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
10
|
|
|
* of the License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program; if not, write to the Free Software |
|
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace App\EventSubscriber; |
|
23
|
|
|
|
|
24
|
|
|
use App\Entity\Base\AbstractDBElement; |
|
25
|
|
|
use App\Entity\LogSystem\AbstractLogEntry; |
|
26
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry; |
|
27
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry; |
|
28
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry; |
|
29
|
|
|
use App\Services\LogSystem\EventLogger; |
|
30
|
|
|
use Doctrine\Common\EventSubscriber; |
|
31
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
|
32
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
|
33
|
|
|
use Doctrine\ORM\Events; |
|
34
|
|
|
use Doctrine\Persistence\Event\LifecycleEventArgs; |
|
35
|
|
|
|
|
36
|
|
|
class EventLoggerSubscriber implements EventSubscriber |
|
37
|
|
|
{ |
|
38
|
|
|
protected $logger; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(EventLogger $logger) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->logger = $logger; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function onFlush(OnFlushEventArgs $eventArgs) |
|
46
|
|
|
{ |
|
47
|
|
|
$em = $eventArgs->getEntityManager(); |
|
48
|
|
|
$uow = $em->getUnitOfWork(); |
|
49
|
|
|
|
|
50
|
|
|
/* |
|
51
|
|
|
* Log changes and deletions of entites. |
|
52
|
|
|
* We can not log persist here, because the entities do not have IDs yet... |
|
53
|
|
|
*/ |
|
54
|
|
|
|
|
55
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
|
56
|
|
|
if ($this->validEntity($entity)) { |
|
57
|
|
|
$log = new ElementEditedLogEntry($entity); |
|
58
|
|
|
$this->logger->log($log); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ($uow->getScheduledEntityDeletions() as $entity) { |
|
63
|
|
|
if ($this->validEntity($entity)) { |
|
64
|
|
|
$log = new ElementDeletedLogEntry($entity); |
|
65
|
|
|
$this->logger->log($log); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$uow->computeChangeSets(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function postPersist(LifecycleEventArgs $args) |
|
74
|
|
|
{ |
|
75
|
|
|
//Create an log entry |
|
76
|
|
|
|
|
77
|
|
|
/** @var AbstractDBElement $entity */ |
|
78
|
|
|
$entity = $args->getObject(); |
|
79
|
|
|
if ($this->validEntity($entity)) { |
|
80
|
|
|
$log = new ElementCreatedLogEntry($entity); |
|
81
|
|
|
$this->logger->log($log); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function postFlush(PostFlushEventArgs $args) |
|
86
|
|
|
{ |
|
87
|
|
|
$em = $args->getEntityManager(); |
|
88
|
|
|
$uow = $em->getUnitOfWork(); |
|
89
|
|
|
// If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here. |
|
90
|
|
|
if ($uow->hasPendingInsertions()) { |
|
91
|
|
|
$em->flush(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Check if the given entity can be logged. |
|
97
|
|
|
* @param object $entity |
|
98
|
|
|
* @return bool True, if the given entity can be logged. |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function validEntity(object $entity): bool |
|
101
|
|
|
{ |
|
102
|
|
|
//Dont log logentries itself! |
|
103
|
|
|
if ($entity instanceof AbstractDBElement && !$entity instanceof AbstractLogEntry) { |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @inheritDoc |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getSubscribedEvents() |
|
114
|
|
|
{ |
|
115
|
|
|
return[ |
|
116
|
|
|
Events::onFlush, |
|
117
|
|
|
Events::postPersist, |
|
118
|
|
|
Events::postFlush |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
} |