1 | <?php |
||
22 | class DoctrineSubscriber implements ContainerAwareInterface, EventSubscriber |
||
23 | { |
||
24 | use ContainerAwareTrait; |
||
25 | |||
26 | /** @var \Doctrine\Common\Annotations\Reader */ |
||
27 | private $annotationReader; |
||
28 | |||
29 | private $toBeDeleted = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $entities; |
||
35 | |||
36 | public function __construct($entities = array()) |
||
37 | { |
||
38 | $this->entities = $entities; |
||
39 | } |
||
40 | |||
41 | public function getSubscribedEvents() |
||
42 | { |
||
43 | return array( |
||
44 | 'postPersist', |
||
45 | 'postUpdate', |
||
46 | 'preRemove', |
||
47 | 'postRemove' |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | public function postPersist(LifecycleEventArgs $args) |
||
52 | { |
||
53 | $this->handleEvent(DoctrineEvents::ENTITY_CREATED, $args); |
||
54 | } |
||
55 | |||
56 | public function postUpdate(LifecycleEventArgs $args) |
||
57 | { |
||
58 | $this->handleEvent(DoctrineEvents::ENTITY_UPDATED, $args); |
||
59 | } |
||
60 | |||
61 | public function preRemove(LifecycleEventArgs $args) |
||
62 | { |
||
63 | if (false === $this->isConfiguredToTrack($args->getEntity(), DoctrineEvents::ENTITY_DELETED)) { |
||
64 | return; |
||
65 | } |
||
66 | |||
67 | $className = ClassUtils::getClass($args->getEntity()); |
||
68 | |||
69 | if (!isset($this->toBeDeleted[$className])) { |
||
70 | $this->toBeDeleted[$className] = []; |
||
71 | } |
||
72 | |||
73 | $this->toBeDeleted[$className][spl_object_hash($args->getEntity())] = $this->getIdentity($args, $className); |
||
74 | } |
||
75 | |||
76 | public function postRemove(LifecycleEventArgs $args) |
||
86 | |||
87 | private function getToBeDeletedId($entity) |
||
95 | |||
96 | /** |
||
97 | * @param string $eventName |
||
98 | * @param LifecycleEventArgs $args |
||
99 | */ |
||
100 | private function handleEvent($eventName, LifecycleEventArgs $args) |
||
101 | { |
||
102 | if (true === $this->isConfiguredToTrack($args->getEntity(), $eventName)) { |
||
103 | $this->container->get('event_dispatcher')->dispatch($eventName, |
||
104 | new DoctrineEntityEvent($args, $this->getIdentity($args, ClassUtils::getClass($args->getEntity()))) |
||
105 | ); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $entity |
||
111 | * @param string $eventName |
||
112 | * @return bool |
||
113 | */ |
||
114 | private function isConfiguredToTrack($entity, $eventName = '') |
||
115 | { |
||
116 | $class = ClassUtils::getClass($entity); |
||
117 | $eventType = DoctrineEvents::getShortEventType($eventName); |
||
118 | |||
119 | if (null !== $track = $this->isAnnotatedEvent($entity, $eventType)) { |
||
120 | return $track; |
||
121 | } |
||
122 | |||
123 | if (!$this->isConfigured($class)) { |
||
124 | return FALSE; |
||
125 | } |
||
126 | |||
127 | if ($this->shouldTrackAllEventType($class)) { |
||
128 | return TRUE; |
||
129 | } |
||
130 | |||
131 | return $this->shouldTrackEventType($eventType, $class); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param $entity |
||
136 | * @param string $eventType |
||
137 | * @return bool|null |
||
138 | */ |
||
139 | protected function isAnnotatedEvent($entity, $eventType) |
||
149 | |||
150 | /** |
||
151 | * @param $entity |
||
152 | * @return null|object |
||
153 | */ |
||
154 | protected function hasAnnotation($entity) |
||
163 | |||
164 | /** |
||
165 | * @return \Doctrine\Common\Annotations\Reader |
||
166 | */ |
||
167 | protected function getAnnotationReader() |
||
171 | |||
172 | /** |
||
173 | * @param $object |
||
174 | * @return \ReflectionClass |
||
175 | */ |
||
176 | protected function getReflectionClassFromObject($object) |
||
182 | |||
183 | /** |
||
184 | * @param string $eventType |
||
185 | * @param string $class |
||
186 | * @return bool |
||
187 | */ |
||
188 | private function shouldTrackEventType($eventType, $class) |
||
189 | { |
||
190 | return (is_array($this->entities[$class]) && in_array($eventType, $this->entities[$class])); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $class |
||
195 | * @return bool |
||
196 | */ |
||
197 | private function shouldTrackAllEventType($class) |
||
198 | { |
||
199 | return empty($this->entities[$class]); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $class |
||
204 | * @return bool |
||
205 | */ |
||
206 | protected function isConfigured($class) |
||
207 | { |
||
208 | return isset($this->entities[$class]); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param \Doctrine\Common\Annotations\Reader $annotationReader |
||
213 | */ |
||
214 | public function setAnnotationReader($annotationReader = null) |
||
218 | |||
219 | /** |
||
220 | * @param LifecycleEventArgs $args |
||
221 | * @param $className |
||
222 | * @return array |
||
223 | */ |
||
224 | protected function getIdentity(LifecycleEventArgs $args, $className) |
||
228 | |||
229 | /** |
||
230 | * @param $entity |
||
231 | * @return boolean |
||
232 | */ |
||
233 | private function isScheduledForDelete($entity) |
||
234 | { |
||
235 | $originalClassName = ClassUtils::getClass($entity); |
||
236 | |||
237 | return isset($this->toBeDeleted[$originalClassName]) && isset($this->toBeDeleted[$originalClassName][spl_object_hash($entity)]); |
||
238 | } |
||
239 | } |
||
240 |