Chain.php 11.2 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
doctrine's avatar
doctrine committed
21
Doctrine::autoload('Doctrine_Access');
22

23 24
/**
 * Doctrine_EventListener_Chain
lsmith's avatar
lsmith committed
25
 * this class represents a chain of different listeners,
26 27
 * useful for having multiple listeners listening the events at the same time
 *
lsmith's avatar
lsmith committed
28 29 30 31 32 33 34 35
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
36 37
class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_EventListener_Interface
{
38
    /**
39
     * @var array $listeners        an array containing all listeners
40 41 42 43
     */
    private $listeners = array();
    /**
     * add
44
     * adds a listener to the chain of listeners
45
     *
46 47
     * @param object $listener
     * @param string $name
48 49
     * @return void
     */
50
    public function add($listener, $name = null)
lsmith's avatar
lsmith committed
51
    {
52 53 54 55 56 57 58 59 60 61
        if ( ! ($listener instanceof Doctrine_EventListener_Interface) &&
             ! ($listener instanceof Doctrine_Overloadable)) {
            
            throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
        }
        if ($name === null) {
            $this->listeners[] = $listener;
        } else {
            $this->listeners[$name] = $listener;
        }
62 63
    }
    /**
64
     * returns a Doctrine_EventListener on success
65 66 67 68 69
     * and null on failure
     *
     * @param mixed $key
     * @return mixed
     */
lsmith's avatar
lsmith committed
70 71
    public function get($key)
    {
lsmith's avatar
lsmith committed
72
        if ( ! isset($this->listeners[$key])) {
73
            return null;
lsmith's avatar
lsmith committed
74
        }
75 76 77 78
        return $this->listeners[$key];
    }
    /**
     * set
lsmith's avatar
lsmith committed
79
     *
80 81 82 83
     * @param mixed $key
     * @param Doctrine_EventListener $listener
     * @return void
     */
lsmith's avatar
lsmith committed
84 85
    public function set($key, Doctrine_EventListener $listener)
    {
86 87
        $this->listeners[$key] = $listener;
    }
zYne's avatar
zYne committed
88 89 90 91 92 93 94
    /**
     * onLoad
     * an event invoked when Doctrine_Record is being loaded from database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
95 96
    public function onLoad(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
97
        foreach ($this->listeners as $listener) {
98
            $listener->onLoad($record);
99 100
        }
    }
zYne's avatar
zYne committed
101 102 103 104 105 106 107 108
    /**
     * onPreLoad
     * an event invoked when Doctrine_Record is being loaded
     * from database but not yet initialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
109 110
    public function onPreLoad(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
111
        foreach ($this->listeners as $listener) {
112 113 114
            $listener->onPreLoad($record);
        }
    }
zYne's avatar
zYne committed
115 116 117 118 119 120 121
    /**
     * onSleep
     * an event invoked when Doctrine_Record is serialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
122 123
    public function onSleep(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
124
        foreach ($this->listeners as $listener) {
125 126 127
            $listener->onSleep($record);
        }
    }
zYne's avatar
zYne committed
128 129 130 131 132 133 134
    /**
     * onWakeUp
     * an event invoked when Doctrine_Record is unserialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
135 136
    public function onWakeUp(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
137
        foreach ($this->listeners as $listener) {
138 139 140
            $listener->onWakeUp($record);
        }
    }
141
    /**
142
     * postClose
143 144
     * an event invoked after Doctrine_Connection is closed
     *
145
     * @param Doctrine_Event $event
146 147
     * @return void
     */
148
    public function postClose(Doctrine_Event $event)
lsmith's avatar
lsmith committed
149
    {
lsmith's avatar
lsmith committed
150
        foreach ($this->listeners as $listener) {
151
            $listener->postClose($event);
152 153
        }
    }
154
    /**
155
     * preClose
156 157
     * an event invoked before Doctrine_Connection is closed
     *
158
     * @param Doctrine_Event $event
159 160
     * @return void
     */
161
    public function preClose(Doctrine_Event $event)
lsmith's avatar
lsmith committed
162
    {
lsmith's avatar
lsmith committed
163
        foreach ($this->listeners as $listener) {
164
            $listener->preClose($event);
165 166
        }
    }
167 168 169 170 171 172 173
    /**
     * onOpen
     * an event invoked after Doctrine_Connection is opened
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
174 175
    public function onOpen(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
176
        foreach ($this->listeners as $listener) {
177
            $listener->onOpen($connection);
178 179
        }
    }
180 181 182 183
    /**
     * onTransactionCommit
     * an event invoked after a Doctrine_Connection transaction is committed
     *
184
     * @param Doctrine_Event $event
185 186
     * @return void
     */
187
    public function postTransactionCommit(Doctrine_Event $event)
lsmith's avatar
lsmith committed
188
    {
lsmith's avatar
lsmith committed
189
        foreach ($this->listeners as $listener) {
190
            $listener->postTransactionCommit($event);
191 192
        }
    }
193 194 195 196
    /**
     * onPreTransactionCommit
     * an event invoked before a Doctrine_Connection transaction is committed
     *
197
     * @param Doctrine_Event $event
198 199
     * @return void
     */
200
    public function preTransactionCommit(Doctrine_Event $event)
lsmith's avatar
lsmith committed
201
    {
lsmith's avatar
lsmith committed
202
        foreach ($this->listeners as $listener) {
203
            $listener->preTransactionCommit($event);
204 205
        }
    }
206 207 208 209
    /**
     * onTransactionRollback
     * an event invoked after a Doctrine_Connection transaction is being rolled back
     *
210
     * @param Doctrine_Event $event
211 212
     * @return void
     */
213
    public function postTransactionRollback(Doctrine_Event $event)
lsmith's avatar
lsmith committed
214
    {
lsmith's avatar
lsmith committed
215
        foreach ($this->listeners as $listener) {
216
            $listener->postTransactionRollback($event);
217 218
        }
    }
219 220 221 222
    /**
     * onPreTransactionRollback
     * an event invoked before a Doctrine_Connection transaction is being rolled back
     *
223
     * @param Doctrine_Event $event
224 225
     * @return void
     */
226
    public function preTransactionRollback(Doctrine_Event $event)
lsmith's avatar
lsmith committed
227
    {
lsmith's avatar
lsmith committed
228
        foreach ($this->listeners as $listener) {
229
            $listener->preTransactionRollback($event);
230 231
        }
    }
232 233 234 235
    /**
     * onTransactionBegin
     * an event invoked after a Doctrine_Connection transaction has been started
     *
236
     * @param Doctrine_Event $event
237 238
     * @return void
     */
239
    public function postTransactionBegin(Doctrine_Event $event)
lsmith's avatar
lsmith committed
240
    {
lsmith's avatar
lsmith committed
241
        foreach ($this->listeners as $listener) {
242
            $listener->postTransactionBegin($event);
243 244
        }
    }
245 246 247 248
    /**
     * onTransactionBegin
     * an event invoked before a Doctrine_Connection transaction is being started
     *
249
     * @param Doctrine_Event $event
250 251
     * @return void
     */
252
    public function preTransactionBegin(Doctrine_Event $event)
lsmith's avatar
lsmith committed
253
    {
lsmith's avatar
lsmith committed
254
        foreach ($this->listeners as $listener) {
255
            $listener->preTransactionBegin($event);
256 257
        }
    }
258 259 260 261 262 263 264
    /**
     * onCollectionDelete
     * an event invoked after a Doctrine_Collection is being deleted
     *
     * @param Doctrine_Collection $collection
     * @return void
     */
lsmith's avatar
lsmith committed
265 266
    public function onCollectionDelete(Doctrine_Collection $collection)
    {
lsmith's avatar
lsmith committed
267
        foreach ($this->listeners as $listener) {
gnat's avatar
gnat committed
268
            $listener->onCollectionDelete($collection);
269 270
        }
    }
271 272 273 274 275 276 277
    /**
     * onCollectionDelete
     * an event invoked after a Doctrine_Collection is being deleted
     *
     * @param Doctrine_Collection $collection
     * @return void
     */
lsmith's avatar
lsmith committed
278 279
    public function onPreCollectionDelete(Doctrine_Collection $collection)
    {
lsmith's avatar
lsmith committed
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 291
    public function preConnect(Doctrine_Event $event)
    {
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 303
    public function postQuery(Doctrine_Event $event)
    {
304
        foreach ($this->listeners as $listener) {
305
            $listener->postQuery($event);
306 307 308
        }
    }

309
    public function prePrepare(Doctrine_Event $event)
310 311
    { 
        foreach ($this->listeners as $listener) {
312
            $listener->prePrepare($event);
313 314
        }
    }
315 316
    public function postPrepare(Doctrine_Event $event)
    {
317
        foreach ($this->listeners as $listener) {
318
            $listener->postPrepare($event);
319 320 321
        }
    }

322 323
    public function preExec(Doctrine_Event $event)
    {
324
        foreach ($this->listeners as $listener) {
325
            $listener->preExec($event);
326 327
        }
    }
328 329
    public function postExec(Doctrine_Event $event)
    {
330
        foreach ($this->listeners as $listener) {
331
            $listener->postExec($event);
332 333
        }
    }
zYne's avatar
zYne committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347

    public function preError(Doctrine_Event $event)
    { 
        foreach ($this->listeners as $listener) {
            $listener->preError($event);
        }
    }
    public function postError(Doctrine_Event $event)
    {
        foreach ($this->listeners as $listener) {
            $listener->postError($event);
        }
    }

348
    public function preFetch(Doctrine_Event $event)
349 350
    { 
        foreach ($this->listeners as $listener) {
351
            $listener->preFetch($event);
352 353
        }
    }
354 355
    public function postFetch(Doctrine_Event $event)
    {
356
        foreach ($this->listeners as $listener) {
357
            $listener->postFetch($event);
358 359 360
        }
    }

361
    public function preFetchAll(Doctrine_Event $event)
362 363
    { 
        foreach ($this->listeners as $listener) {
364
            $listener->preFetchAll($event);
365 366 367
        }
    }

368 369
    public function postFetchAll(Doctrine_Event $event)
    {
370
        foreach ($this->listeners as $listener) {
371
            $listener->postFetchAll($event);
372 373 374
        }
    }

zYne's avatar
zYne committed
375
    public function preStmtExecute(Doctrine_Event $event)
376
    {
377
        foreach ($this->listeners as $listener) {
zYne's avatar
zYne committed
378
            $listener->preStmtExecute($event);
379 380 381
        }
    }

zYne's avatar
zYne committed
382
    public function postStmtExecute(Doctrine_Event $event)
383
    {
384
        foreach ($this->listeners as $listener) {
zYne's avatar
zYne committed
385
            $listener->postStmtExecute($event);
386 387
        }
    }
388
}