|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the sauls/object-registry-bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
|
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
|
7
|
|
|
* @copyright 2018 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
namespace Sauls\Bundle\ObjectRegistryBundle\Event; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\EventArgs; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
19
|
|
|
|
|
20
|
|
|
class GenericDoctrineObjectEvent extends GenericObjectEvent |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var EventArgs |
|
24
|
|
|
*/ |
|
25
|
|
|
private $parent; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $changeSet; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(object $subject = null, EventArgs $parent, array $arguments = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($subject, $arguments); |
|
35
|
|
|
|
|
36
|
|
|
$this->parent = $parent; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getEntity(): object |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->getSubject(); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getParent(): EventArgs |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->parent; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function setParent(EventArgs $parent): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->parent = $parent; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the entire changeset (or just chengeset of an attribute if key is setted). |
|
56
|
|
|
* |
|
57
|
|
|
* @param string|null $key |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getChangeSet($key = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$changeSet = $this->registerChangeSet(); |
|
64
|
|
|
if (null === $key) { |
|
65
|
|
|
return $changeSet; |
|
66
|
|
|
} |
|
67
|
|
|
if (false === array_key_exists($key, $changeSet)) { |
|
68
|
|
|
return false; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
return $changeSet[$key]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
private function registerChangeSet() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->changeSet = null === $this->changeSet |
|
79
|
|
|
? $this->getUnitOfWork()->getEntityChangeSet($this->subject) |
|
80
|
|
|
: $this->changeSet; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return UnitOfWork |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getUnitOfWork() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getEntityManager()->getUnitOfWork(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return EntityManagerInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getEntityManager(): EntityManagerInterface |
|
95
|
|
|
{ |
|
96
|
|
|
if (\method_exists($this->parent, 'getEntityManager')) { |
|
97
|
|
|
return $this->parent->getEntityManager(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (\method_exists($this->parent, 'getObjectManager')) { |
|
101
|
|
|
return $this->parent->getObjectManager(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
throw new \RuntimeException(sprintf( |
|
105
|
|
|
'Method `getEntityManager` or `getObjectManager` does not exist on parent event class `%s`', |
|
106
|
|
|
\get_class($this->parent)) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|