| 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.org>. |
| 20 | */ |
| 21 | Doctrine::autoload('Doctrine_Access'); |
| 22 | |
| 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 | * @package Doctrine |
| 29 | * @subpackage Record |
| 30 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 31 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 32 | * @link www.phpdoctrine.org |
| 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 | /** |
| 44 | * add |
| 45 | * adds a listener to the chain of listeners |
| 46 | * |
| 47 | * @param object $listener |
| 48 | * @param string $name |
| 49 | * @return void |
| 50 | */ |
| 51 | public function add($listener, $name = null) |
| 52 | { |
| 53 | if ( ! ($listener instanceof Doctrine_Record_Listener_Interface) && |
| 54 | ! ($listener instanceof Doctrine_Overloadable)) { |
| 55 | |
| 56 | throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. Record listeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable"); |
| 57 | } |
| 58 | if ($name === null) { |
| 59 | $this->_listeners[] = $listener; |
| 60 | } else { |
| 61 | $this->_listeners[$name] = $listener; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * returns a Doctrine_Record_Listener on success |
| 67 | * and null on failure |
| 68 | * |
| 69 | * @param mixed $key |
| 70 | * @return mixed |
| 71 | */ |
| 72 | public function get($key) |
| 73 | { |
| 74 | if ( ! isset($this->_listeners[$key])) { |
| 75 | return null; |
| 76 | } |
| 77 | return $this->_listeners[$key]; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * set |
| 82 | * |
| 83 | * @param mixed $key |
| 84 | * @param Doctrine_Record_Listener $listener listener to be added |
| 85 | * @return Doctrine_Record_Listener_Chain this object |
| 86 | */ |
| 87 | public function set($key, Doctrine_EventListener $listener) |
| 88 | { |
| 89 | $this->_listeners[$key] = $listener; |
| 90 | } |
| 91 | |
| 92 | public function preSerialize(Doctrine_Event $event) |
| 93 | { |
| 94 | foreach ($this->_listeners as $listener) { |
| 95 | $listener->preSerialize($event); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function postSerialize(Doctrine_Event $event) |
| 100 | { |
| 101 | foreach ($this->_listeners as $listener) { |
| 102 | $listener->preSerialize($event); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | public function preUnserialize(Doctrine_Event $event) |
| 107 | { |
| 108 | foreach ($this->_listeners as $listener) { |
| 109 | $listener->preUnserialize($event); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public function postUnserialize(Doctrine_Event $event) |
| 114 | { |
| 115 | foreach ($this->_listeners as $listener) { |
| 116 | $listener->postUnserialize($event); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function preSave(Doctrine_Event $event) |
| 121 | { |
| 122 | foreach ($this->_listeners as $listener) { |
| 123 | $listener->preSave($event); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public function postSave(Doctrine_Event $event) |
| 128 | { |
| 129 | foreach ($this->_listeners as $listener) { |
| 130 | $listener->postSave($event); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public function preDelete(Doctrine_Event $event) |
| 135 | { |
| 136 | foreach ($this->_listeners as $listener) { |
| 137 | $listener->preDelete($event); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public function postDelete(Doctrine_Event $event) |
| 142 | { |
| 143 | foreach ($this->_listeners as $listener) { |
| 144 | $listener->postDelete($event); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public function preUpdate(Doctrine_Event $event) |
| 149 | { |
| 150 | foreach ($this->_listeners as $listener) { |
| 151 | $listener->preUpdate($event); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | public function postUpdate(Doctrine_Event $event) |
| 156 | { |
| 157 | foreach ($this->_listeners as $listener) { |
| 158 | $listener->postUpdate($event); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | public function preInsert(Doctrine_Event $event) |
| 163 | { |
| 164 | foreach ($this->_listeners as $listener) { |
| 165 | $listener->preInsert($event); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public function postInsert(Doctrine_Event $event) |
| 170 | { |
| 171 | foreach ($this->_listeners as $listener) { |
| 172 | $listener->postInsert($event); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | |
| 177 | public function preHydrate(Doctrine_Event $event) |
| 178 | { |
| 179 | foreach ($this->_listeners as $listener) { |
| 180 | $listener->preHydrate($event); |
| 181 | } |
| 182 | } |
| 183 | public function postHydrate(Doctrine_Event $event) |
| 184 | { |
| 185 | foreach ($this->_listeners as $listener) { |
| 186 | $listener->postHydrate($event); |
| 187 | } |
| 188 | } |
| 189 | } |