# Source for file Chain.php

Documentation is available at Chain.php

1. <?php
2. /*
3. * $Id$
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_Record_Listener_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$
35. */
36. class Doctrine_Record_Listener_Chain extends Doctrine_Access implements Doctrine_Record_Listener_Interface
37. {
38. /**
39. * @var array $listeners an array containing all listeners
40. */
41. protected $_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_Record_Listener_Interface) &&
53. ! ($listener instanceof Doctrine_Overloadable)) {
55. throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. Record listeners 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_Record_Listener 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_Record_Listener $listener listener to be added
82. * @return Doctrine_Record_Listener_Chain this object
83. */
84. public function set($key, Doctrine_EventListener $listener)
85. {
86. $this->_listeners[$key] = $listener;
87. }
89. public function preSerialize(Doctrine_Event $event)
90. {
91. foreach ($this->_listeners as $listener) {
92. $listener->preSerialize($event);
93. }
94. }
96. public function postSerialize(Doctrine_Event $event)
97. {
98. foreach ($this->_listeners as $listener) {
99. $listener->preSerialize($event);
100. }
101. }
103. public function preUnserialize(Doctrine_Event $event)
104. {
105. foreach ($this->_listeners as $listener) {
106. $listener->preUnserialize($event);
107. }
108. }
110. public function postUnserialize(Doctrine_Event $event)
111. {
112. foreach ($this->_listeners as $listener) {
113. $listener->postUnserialize($event);
114. }
115. }
117. public function preSave(Doctrine_Event $event)
118. {
119. foreach ($this->_listeners as $listener) {
120. $listener->preSave($event);
121. }
122. }
124. public function postSave(Doctrine_Event $event)
125. {
126. foreach ($this->_listeners as $listener) {
127. $listener->postSave($event);
128. }
129. }
131. public function preDelete(Doctrine_Event $event)
132. {
133. foreach ($this->_listeners as $listener) {
134. $listener->preDelete($event);
135. }
136. }
138. public function postDelete(Doctrine_Event $event)
139. {
140. foreach ($this->_listeners as $listener) {
141. $listener->postDelete($event);
142. }
143. }
145. public function preUpdate(Doctrine_Event $event)
146. {
147. foreach ($this->_listeners as $listener) {
148. $listener->preUpdate($event);
149. }
150. }
152. public function postUpdate(Doctrine_Event $event)
153. {
154. foreach ($this->_listeners as $listener) {
155. $listener->postUpdate($event);
156. }
157. }
159. public function preInsert(Doctrine_Event $event)
160. {
161. foreach ($this->_listeners as $listener) {
162. $listener->preInsert($event);
163. }
164. }
166. public function postInsert(Doctrine_Event $event)
167. {
168. foreach ($this->_listeners as $listener) {
169. $listener->postInsert($event);
170. }
171. }
172. }