# Source for file Chain.php

Documentation is available at Chain.php

1. <?php
2. /*
3. * $Id: Chain.php 2186 2007-08-09 22:16:33Z jackbravo $
4. *
5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16. *
17. * This software consists of voluntary contributions made by many individuals
18. * and is licensed under the LGPL. For more information, see
19. * <http://www.phpdoctrine.com>.
20. */
21. Doctrine::autoload('Doctrine_Access');
23. /**
24. * Doctrine_EventListener_Chain
25. * this class represents a chain of different listeners,
26. * useful for having multiple listeners listening the events at the same time
27. *
28. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
29. * @package Doctrine
30. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
31. * @category Object Relational Mapping
32. * @link www.phpdoctrine.com
33. * @since 1.0
34. * @version $Revision: 2186 $
35. */
36. class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_EventListener_Interface
37. {
38. /**
39. * @var array $listeners an array containing all listeners
40. */
41. private $listeners = array();
42. /**
43. * add
44. * adds a listener to the chain of listeners
45. *
46. * @param object $listener
47. * @param string $name
48. * @return void
49. */
50. public function add($listener, $name = null)
51. {
52. if ( ! ($listener instanceof Doctrine_EventListener_Interface) &&
53. ! ($listener instanceof Doctrine_Overloadable)) {
55. throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
56. }
57. if ($name === null) {
58. $this->listeners[] = $listener;
59. } else {
60. $this->listeners[$name] = $listener;
61. }
62. }
63. /**
64. * returns a Doctrine_EventListener on success
65. * and null on failure
66. *
67. * @param mixed $key
68. * @return mixed
69. */
70. public function get($key)
71. {
72. if ( ! isset($this->listeners[$key])) {
73. return null;
74. }
75. return $this->listeners[$key];
76. }
77. /**
78. * set
79. *
80. * @param mixed $key
81. * @param Doctrine_EventListener $listener
82. * @return void
83. */
84. public function set($key, Doctrine_EventListener $listener)
85. {
86. $this->listeners[$key] = $listener;
87. }
88. /**
89. * onLoad
90. * an event invoked when Doctrine_Record is being loaded from database
91. *
92. * @param Doctrine_Record $record
93. * @return void
94. */
95. public function onLoad(Doctrine_Record $record)
96. {
97. foreach ($this->listeners as $listener) {
98. $listener->onLoad($record);
99. }
100. }
101. /**
102. * onPreLoad
103. * an event invoked when Doctrine_Record is being loaded
104. * from database but not yet initialized
105. *
106. * @param Doctrine_Record $record
107. * @return void
108. */
109. public function onPreLoad(Doctrine_Record $record)
110. {
111. foreach ($this->listeners as $listener) {
112. $listener->onPreLoad($record);
113. }
114. }
115. /**
116. * onSleep
117. * an event invoked when Doctrine_Record is serialized
118. *
119. * @param Doctrine_Record $record
120. * @return void
121. */
122. public function onSleep(Doctrine_Record $record)
123. {
124. foreach ($this->listeners as $listener) {
125. $listener->onSleep($record);
126. }
127. }
128. /**
129. * onWakeUp
130. * an event invoked when Doctrine_Record is unserialized
131. *
132. * @param Doctrine_Record $record
133. * @return void
134. */
135. public function onWakeUp(Doctrine_Record $record)
136. {
137. foreach ($this->listeners as $listener) {
138. $listener->onWakeUp($record);
139. }
140. }
141. /**
142. * postClose
143. * an event invoked after Doctrine_Connection is closed
144. *
145. * @param Doctrine_Event $event
146. * @return void
147. */
148. public function postClose(Doctrine_Event $event)
149. {
150. foreach ($this->listeners as $listener) {
151. $listener->postClose($event);
152. }
153. }
154. /**
155. * preClose
156. * an event invoked before Doctrine_Connection is closed
157. *
158. * @param Doctrine_Event $event
159. * @return void
160. */
161. public function preClose(Doctrine_Event $event)
162. {
163. foreach ($this->listeners as $listener) {
164. $listener->preClose($event);
165. }
166. }
167. /**
168. * onOpen
169. * an event invoked after Doctrine_Connection is opened
170. *
171. * @param Doctrine_Connection $connection
172. * @return void
173. */
174. public function onOpen(Doctrine_Connection $connection)
175. {
176. foreach ($this->listeners as $listener) {
177. $listener->onOpen($connection);
178. }
179. }
180. /**
181. * onTransactionCommit
182. * an event invoked after a Doctrine_Connection transaction is committed
183. *
184. * @param Doctrine_Event $event
185. * @return void
186. */
187. public function postTransactionCommit(Doctrine_Event $event)
188. {
189. foreach ($this->listeners as $listener) {
190. $listener->postTransactionCommit($event);
191. }
192. }
193. /**
194. * onPreTransactionCommit
195. * an event invoked before a Doctrine_Connection transaction is committed
196. *
197. * @param Doctrine_Event $event
198. * @return void
199. */
200. public function preTransactionCommit(Doctrine_Event $event)
201. {
202. foreach ($this->listeners as $listener) {
203. $listener->preTransactionCommit($event);
204. }
205. }
206. /**
207. * onTransactionRollback
208. * an event invoked after a Doctrine_Connection transaction is being rolled back
209. *
210. * @param Doctrine_Event $event
211. * @return void
212. */
213. public function postTransactionRollback(Doctrine_Event $event)
214. {
215. foreach ($this->listeners as $listener) {
216. $listener->postTransactionRollback($event);
217. }
218. }
219. /**
220. * onPreTransactionRollback
221. * an event invoked before a Doctrine_Connection transaction is being rolled back
222. *
223. * @param Doctrine_Event $event
224. * @return void
225. */
226. public function preTransactionRollback(Doctrine_Event $event)
227. {
228. foreach ($this->listeners as $listener) {
229. $listener->preTransactionRollback($event);
230. }
231. }
232. /**
233. * onTransactionBegin
234. * an event invoked after a Doctrine_Connection transaction has been started
235. *
236. * @param Doctrine_Event $event
237. * @return void
238. */
239. public function postTransactionBegin(Doctrine_Event $event)
240. {
241. foreach ($this->listeners as $listener) {
242. $listener->postTransactionBegin($event);
243. }
244. }
245. /**
246. * onTransactionBegin
247. * an event invoked before a Doctrine_Connection transaction is being started
248. *
249. * @param Doctrine_Event $event
250. * @return void
251. */
252. public function preTransactionBegin(Doctrine_Event $event)
253. {
254. foreach ($this->listeners as $listener) {
255. $listener->preTransactionBegin($event);
256. }
257. }
258. /**
259. * onCollectionDelete
260. * an event invoked after a Doctrine_Collection is being deleted
261. *
262. * @param Doctrine_Collection $collection
263. * @return void
264. */
265. public function onCollectionDelete(Doctrine_Collection $collection)
266. {
267. foreach ($this->listeners as $listener) {
268. $listener->onCollectionDelete($collection);
269. }
270. }
271. /**
272. * onCollectionDelete
273. * an event invoked after a Doctrine_Collection is being deleted
274. *
275. * @param Doctrine_Collection $collection
276. * @return void
277. */
278. public function onPreCollectionDelete(Doctrine_Collection $collection)
279. {
280. foreach ($this->listeners as $listener) {
281. $listener->onPreCollectionDelete($collection);
282. }
283. }
284. public function postConnect(Doctrine_Event $event)
285. {
286. foreach ($this->listeners as $listener) {
287. $listener->postConnect($event);
288. }
289. }
290. public function preConnect(Doctrine_Event $event)
291. {
292. foreach ($this->listeners as $listener) {
293. $listener->preConnect($event);
294. }
295. }
296. public function preQuery(Doctrine_Event $event)
297. {
298. foreach ($this->listeners as $listener) {
299. $listener->preQuery($event);
300. }
301. }
302. public function postQuery(Doctrine_Event $event)
303. {
304. foreach ($this->listeners as $listener) {
305. $listener->postQuery($event);
306. }
307. }
309. public function prePrepare(Doctrine_Event $event)
310. {
311. foreach ($this->listeners as $listener) {
312. $listener->prePrepare($event);
313. }
314. }
315. public function postPrepare(Doctrine_Event $event)
316. {
317. foreach ($this->listeners as $listener) {
318. $listener->postPrepare($event);
319. }
320. }
322. public function preExec(Doctrine_Event $event)
323. {
324. foreach ($this->listeners as $listener) {
325. $listener->preExec($event);
326. }
327. }
328. public function postExec(Doctrine_Event $event)
329. {
330. foreach ($this->listeners as $listener) {
331. $listener->postExec($event);
332. }
333. }
335. public function preError(Doctrine_Event $event)
336. {
337. foreach ($this->listeners as $listener) {
338. $listener->preError($event);
339. }
340. }
341. public function postError(Doctrine_Event $event)
342. {
343. foreach ($this->listeners as $listener) {
344. $listener->postError($event);
345. }
346. }
348. public function preFetch(Doctrine_Event $event)
349. {
350. foreach ($this->listeners as $listener) {
351. $listener->preFetch($event);
352. }
353. }
354. public function postFetch(Doctrine_Event $event)
355. {
356. foreach ($this->listeners as $listener) {
357. $listener->postFetch($event);
358. }
359. }
361. public function preFetchAll(Doctrine_Event $event)
362. {
363. foreach ($this->listeners as $listener) {
364. $listener->preFetchAll($event);
365. }
366. }
368. public function postFetchAll(Doctrine_Event $event)
369. {
370. foreach ($this->listeners as $listener) {
371. $listener->postFetchAll($event);
372. }
373. }
375. public function preStmtExecute(Doctrine_Event $event)
376. {
377. foreach ($this->listeners as $listener) {
378. $listener->preStmtExecute($event);
379. }
380. }
382. public function postStmtExecute(Doctrine_Event $event)
383. {
384. foreach ($this->listeners as $listener) {
385. $listener->postStmtExecute($event);
386. }
387. }
388. }