Chain.php 12.9 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 25 26 27 28 29 30 31 32
/**
 * Doctrine_EventListener_Chain
 * this class represents a chain of different listeners, 
 * useful for having multiple listeners listening the events at the same time
 *
 * @author      Konsta Vesterinen
 * @package     Doctrine ORM
 * @url         www.phpdoctrine.com
 * @license     LGPL
 */
class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_EventListener_Interface {
33
    /**
34
     * @var array $listeners        an array containing all listeners
35 36 37 38 39 40 41 42
     */
    private $listeners = array();
    /**
     * add
     *
     * @param Doctrine_EventListener $listener
     * @return void
     */
43
    public function add(Doctrine_EventListener $listener) {
44 45 46 47 48 49 50 51 52
        $this->listeners[] = $listener;
    }
    /**
     * returns a Doctrine_EvenListener on success
     * and null on failure
     *
     * @param mixed $key
     * @return mixed
     */
53
    public function get($key) {
54 55 56 57 58 59 60 61 62 63 64 65
        if( ! isset($this->listeners[$key]))
            return null;

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