Chain.php 13.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 44 45 46 47
     */
    private $listeners = array();
    /**
     * add
     *
     * @param Doctrine_EventListener $listener
     * @return void
     */
lsmith's avatar
lsmith committed
48 49
    public function add(Doctrine_EventListener $listener)
    {
50 51 52 53 54 55 56 57 58
        $this->listeners[] = $listener;
    }
    /**
     * returns a Doctrine_EvenListener on success
     * and null on failure
     *
     * @param mixed $key
     * @return mixed
     */
lsmith's avatar
lsmith committed
59 60
    public function get($key)
    {
lsmith's avatar
lsmith committed
61
        if ( ! isset($this->listeners[$key])) {
62
            return null;
lsmith's avatar
lsmith committed
63
        }
64 65 66 67
        return $this->listeners[$key];
    }
    /**
     * set
lsmith's avatar
lsmith committed
68
     *
69 70 71 72
     * @param mixed $key
     * @param Doctrine_EventListener $listener
     * @return void
     */
lsmith's avatar
lsmith committed
73 74
    public function set($key, Doctrine_EventListener $listener)
    {
75 76
        $this->listeners[$key] = $listener;
    }
zYne's avatar
zYne committed
77 78 79 80 81 82 83
    /**
     * onLoad
     * an event invoked when Doctrine_Record is being loaded from database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
84 85
    public function onLoad(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
86
        foreach ($this->listeners as $listener) {
87
            $listener->onLoad($record);
88 89
        }
    }
zYne's avatar
zYne committed
90 91 92 93 94 95 96 97
    /**
     * 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
98 99
    public function onPreLoad(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
100
        foreach ($this->listeners as $listener) {
101 102 103
            $listener->onPreLoad($record);
        }
    }
zYne's avatar
zYne committed
104 105 106 107 108 109 110
    /**
     * onSleep
     * an event invoked when Doctrine_Record is serialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
111 112
    public function onSleep(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
113
        foreach ($this->listeners as $listener) {
114 115 116
            $listener->onSleep($record);
        }
    }
zYne's avatar
zYne committed
117 118 119 120 121 122 123
    /**
     * onWakeUp
     * an event invoked when Doctrine_Record is unserialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
124 125
    public function onWakeUp(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
126
        foreach ($this->listeners as $listener) {
127 128 129
            $listener->onWakeUp($record);
        }
    }
zYne's avatar
zYne committed
130 131 132 133 134 135 136
    /**
     * onUpdate
     * an event invoked after Doctrine_Record is updated
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
137 138
    public function onUpdate(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
139
        foreach ($this->listeners as $listener) {
140 141 142
            $listener->onUpdate($record);
        }
    }
zYne's avatar
zYne committed
143 144 145 146 147 148 149
    /**
     * onPreUpdate
     * an event invoked before Doctrine_Record is updated
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
150 151
    public function onPreUpdate(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
152
        foreach ($this->listeners as $listener) {
153 154 155
            $listener->onPreUpdate($record);
        }
    }
zYne's avatar
zYne committed
156 157 158 159 160 161 162
    /**
     * onCreate
     * an event invoked when a new Doctrine_Record is created
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
163 164
    public function onCreate(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
165
        foreach ($this->listeners as $listener) {
166 167 168
            $listener->onCreate($record);
        }
    }
zYne's avatar
zYne committed
169 170 171 172 173 174 175 176
    /**
     * onPreCreate
     * an event invoked when a new Doctrine_Record
     * but not yet initialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
177 178
    public function onPreCreate(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
179
        foreach ($this->listeners as $listener) {
180 181 182
            $listener->onPreCreate($record);
        }
    }
zYne's avatar
zYne committed
183 184 185 186 187 188 189
    /**
     * onSave
     * an event invoked after a Doctrine_Record is saved (inserted / updated)
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
190 191
    public function onSave(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
192
        foreach ($this->listeners as $listener) {
193 194 195
            $listener->onSave($record);
        }
    }
zYne's avatar
zYne committed
196 197 198 199 200 201 202
    /**
     * onSave
     * an event invoked after a Doctrine_Record is saved (inserted / updated)
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
203 204
    public function onPreSave(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
205
        foreach ($this->listeners as $listener) {
206 207 208
            $listener->onPreSave($record);
        }
    }
zYne's avatar
zYne committed
209 210 211 212 213 214 215
    /**
     * onGetProperty
     * an event invoked when a property of Doctrine_Record is retrieved
     *
     * @param Doctrine_Record $record
     * @param string $property
     * @param mixed $value
216
     * @return mixed
zYne's avatar
zYne committed
217
     */
lsmith's avatar
lsmith committed
218 219
    public function onGetProperty(Doctrine_Record $record, $property, $value)
    {
lsmith's avatar
lsmith committed
220
        foreach ($this->listeners as $listener) {
pookey's avatar
pookey committed
221
            $value = $listener->onGetProperty($record, $property, $value);
222
        }
pookey's avatar
pookey committed
223
        return $value;
224
    }
zYne's avatar
zYne committed
225 226 227 228 229 230 231
    /**
     * onSetProperty
     * an event invoked when a property of Doctrine_Record is being set
     *
     * @param Doctrine_Record $record
     * @param string $property
     * @param mixed $value
232
     * @return mixed
zYne's avatar
zYne committed
233
     */
lsmith's avatar
lsmith committed
234 235
    public function onSetProperty(Doctrine_Record $record, $property, $value)
    {
lsmith's avatar
lsmith committed
236
        foreach ($this->listeners as $listener) {
pookey's avatar
pookey committed
237
            $value = $listener->onSetProperty($record, $property, $value);
238
        }
pookey's avatar
pookey committed
239
        return $value;
240
    }
zYne's avatar
zYne committed
241 242 243 244 245 246 247
    /**
     * onInsert
     * an event invoked after Doctrine_Record is inserted into database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
248 249
    public function onInsert(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
250
        foreach ($this->listeners as $listener) {
251 252 253
            $listener->onInsert($record);
        }
    }
zYne's avatar
zYne committed
254 255 256 257 258 259 260
    /**
     * onPreInsert
     * an event invoked before Doctrine_Record is inserted into database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
261 262
    public function onPreInsert(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
263
        foreach ($this->listeners as $listener) {
264 265 266
            $listener->onPreInsert($record);
        }
    }
zYne's avatar
zYne committed
267 268 269 270 271 272 273
    /**
     * onDelete
     * an event invoked after Doctrine_Record is deleted from database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
274 275
    public function onDelete(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
276
        foreach ($this->listeners as $listener) {
277 278 279
            $listener->onDelete($record);
        }
    }
zYne's avatar
zYne committed
280 281 282 283 284 285 286
    /**
     * onPreDelete
     * an event invoked before Doctrine_Record is deleted from database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
287 288
    public function onPreDelete(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
289
        foreach ($this->listeners as $listener) {
290 291 292
            $listener->onPreDelete($record);
        }
    }
293 294 295 296 297 298 299
    /**
     * onEvict
     * an event invoked after Doctrine_Record is evicted from record repository
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
300 301
    public function onEvict(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
302
        foreach ($this->listeners as $listener) {
303 304 305
            $listener->onEvict($record);
        }
    }
306 307 308 309 310 311 312
    /**
     * onPreEvict
     * an event invoked before Doctrine_Record is evicted from record repository
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
313 314
    public function onPreEvict(Doctrine_Record $record)
    {
lsmith's avatar
lsmith committed
315
        foreach ($this->listeners as $listener) {
316 317 318
            $listener->onPreEvict($record);
        }
    }
319 320 321 322 323 324 325
    /**
     * onClose
     * an event invoked after Doctrine_Connection is closed
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
326 327
    public function onClose(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
328
        foreach ($this->listeners as $listener) {
329
            $listener->onClose($connection);
330 331
        }
    }
332 333 334 335 336 337 338
    /**
     * onClose
     * an event invoked before Doctrine_Connection is closed
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
339 340
    public function onPreClose(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
341
        foreach ($this->listeners as $listener) {
342
            $listener->onPreClose($connection);
343 344
        }
    }
345 346 347 348 349 350 351
    /**
     * onOpen
     * an event invoked after Doctrine_Connection is opened
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
352 353
    public function onOpen(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
354
        foreach ($this->listeners as $listener) {
355
            $listener->onOpen($connection);
356 357
        }
    }
358 359 360 361 362 363 364
    /**
     * onTransactionCommit
     * an event invoked after a Doctrine_Connection transaction is committed
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
365 366
    public function onTransactionCommit(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
367
        foreach ($this->listeners as $listener) {
368
            $listener->onTransactionCommit($connection);
369 370
        }
    }
371 372 373 374 375 376 377
    /**
     * onPreTransactionCommit
     * an event invoked before a Doctrine_Connection transaction is committed
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
378 379
    public function onPreTransactionCommit(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
380
        foreach ($this->listeners as $listener) {
381
            $listener->onPreTransactionCommit($connection);
382 383
        }
    }
384 385 386 387 388 389 390
    /**
     * onTransactionRollback
     * an event invoked after a Doctrine_Connection transaction is being rolled back
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
391 392
    public function onTransactionRollback(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
393
        foreach ($this->listeners as $listener) {
394
            $listener->onTransactionRollback($connection);
395 396
        }
    }
397 398 399 400 401 402 403
    /**
     * onPreTransactionRollback
     * an event invoked before a Doctrine_Connection transaction is being rolled back
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
404 405
    public function onPreTransactionRollback(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
406
        foreach ($this->listeners as $listener) {
407
            $listener->onPreTransactionRollback($connection);
408 409
        }
    }
410 411 412 413 414 415 416
    /**
     * onTransactionBegin
     * an event invoked after a Doctrine_Connection transaction has been started
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
417 418
    public function onTransactionBegin(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
419
        foreach ($this->listeners as $listener) {
420
            $listener->onTransactionBegin($connection);
421 422
        }
    }
423 424 425 426 427 428 429
    /**
     * onTransactionBegin
     * an event invoked before a Doctrine_Connection transaction is being started
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
430 431
    public function onPreTransactionBegin(Doctrine_Connection $connection)
    {
lsmith's avatar
lsmith committed
432
        foreach ($this->listeners as $listener) {
433
            $listener->onPreTransactionBegin($connection);
434 435
        }
    }
436 437 438 439 440 441 442
    /**
     * onCollectionDelete
     * an event invoked after a Doctrine_Collection is being deleted
     *
     * @param Doctrine_Collection $collection
     * @return void
     */
lsmith's avatar
lsmith committed
443 444
    public function onCollectionDelete(Doctrine_Collection $collection)
    {
lsmith's avatar
lsmith committed
445
        foreach ($this->listeners as $listener) {
gnat's avatar
gnat committed
446
            $listener->onCollectionDelete($collection);
447 448
        }
    }
449 450 451 452 453 454 455
    /**
     * onCollectionDelete
     * an event invoked after a Doctrine_Collection is being deleted
     *
     * @param Doctrine_Collection $collection
     * @return void
     */
lsmith's avatar
lsmith committed
456 457
    public function onPreCollectionDelete(Doctrine_Collection $collection)
    {
lsmith's avatar
lsmith committed
458
        foreach ($this->listeners as $listener) {
459
            $listener->onPreCollectionDelete($collection);
460 461 462
        }
    }
}