|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace steevanb\DoctrineReadOnlyHydrator\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs; |
|
6
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Hydrator\SimpleObjectHydrator; |
|
7
|
|
|
use Doctrine\Common\EventSubscriber; |
|
8
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
|
9
|
|
|
use Doctrine\ORM\Event\PreFlushEventArgs; |
|
10
|
|
|
use Doctrine\ORM\Events; |
|
11
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Entity\ReadOnlyEntityInterface; |
|
12
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBeFlushedException; |
|
13
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBePersistedException; |
|
14
|
|
|
|
|
15
|
|
|
class ReadOnlySubscriber implements EventSubscriber |
|
16
|
|
|
{ |
|
17
|
|
|
/** @return array */ |
|
18
|
|
|
public function getSubscribedEvents() |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
|
|
Events::prePersist, |
|
22
|
|
|
Events::preFlush, |
|
23
|
|
|
Events::onClassMetadataNotFound, |
|
24
|
|
|
Events::postLoad |
|
25
|
|
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param LifecycleEventArgs $args |
|
30
|
|
|
* @throws ReadOnlyEntityCantBePersistedException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function prePersist(LifecycleEventArgs $args) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->isReadOnlyEntity($args->getObject())) { |
|
35
|
|
|
throw new ReadOnlyEntityCantBePersistedException($args->getObject()); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param PreFlushEventArgs $args |
|
41
|
|
|
* @throws ReadOnlyEntityCantBeFlushedException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function preFlush(PreFlushEventArgs $args) |
|
44
|
|
|
{ |
|
45
|
|
|
$unitOfWork = $args->getEntityManager()->getUnitOfWork(); |
|
46
|
|
|
$entities = array_merge( |
|
47
|
|
|
$unitOfWork->getScheduledEntityInsertions(), |
|
48
|
|
|
$unitOfWork->getScheduledEntityUpdates(), |
|
49
|
|
|
$unitOfWork->getScheduledEntityDeletions() |
|
50
|
|
|
); |
|
51
|
|
|
foreach ($entities as $entity) { |
|
52
|
|
|
if ($this->isReadOnlyEntity($entity)) { |
|
53
|
|
|
throw new ReadOnlyEntityCantBeFlushedException($entity); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** @param OnClassMetadataNotFoundEventArgs $eventArgs */ |
|
59
|
|
|
public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs) |
|
60
|
|
|
{ |
|
61
|
|
|
try { |
|
62
|
|
|
if (class_implements( |
|
63
|
|
|
$eventArgs->getClassName(), |
|
64
|
|
|
'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface' |
|
65
|
|
|
)) { |
|
66
|
|
|
$eventArgs->setFoundMetadata( |
|
67
|
|
|
$eventArgs->getObjectManager()->getClassMetadata(get_parent_class($eventArgs->getClassName())) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} catch (\Exception $exception) {} |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @param LifecycleEventArgs $eventArgs */ |
|
74
|
|
|
public function postLoad(LifecycleEventArgs $eventArgs) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($eventArgs->getObject() instanceof ReadOnlyEntityInterface) { |
|
77
|
|
|
// add ReadOnlyProxy to classMetada list |
|
78
|
|
|
// without it, you can't use Doctrine automatic id finder |
|
79
|
|
|
// like $queryBuilder->setParameter('foo', $foo) |
|
80
|
|
|
// instead of $queryBuilder->setParameter('foo', $foo->getId()) |
|
81
|
|
|
$eventArgs->getObjectManager()->getClassMetadata(get_class($eventArgs->getObject())); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param object $entity |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function isReadOnlyEntity($entity) |
|
90
|
|
|
{ |
|
91
|
|
|
return |
|
92
|
|
|
$entity instanceof ReadOnlyEntityInterface |
|
93
|
|
|
|| isset($entity->{SimpleObjectHydrator::READ_ONLY_PROPERTY}); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|