Chain.php 11.3 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  $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
19
 * <http://www.phpdoctrine.org>.
20
 */
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
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
30
 * @subpackage  EventListener
lsmith's avatar
lsmith committed
31 32 33 34 35
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @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
     */
zYne's avatar
zYne committed
41
    protected $_listeners = array();
42

43 44
    /**
     * add
45
     * adds a listener to the chain of listeners
46
     *
47 48
     * @param object $listener
     * @param string $name
49 50
     * @return void
     */
51
    public function add($listener, $name = null)
lsmith's avatar
lsmith committed
52
    {
53 54 55 56 57 58
        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) {
zYne's avatar
zYne committed
59
            $this->_listeners[] = $listener;
60
        } else {
zYne's avatar
zYne committed
61
            $this->_listeners[$name] = $listener;
62
        }
63
    }
64

65
    /**
66
     * returns a Doctrine_EventListener on success
67 68 69 70 71
     * and null on failure
     *
     * @param mixed $key
     * @return mixed
     */
lsmith's avatar
lsmith committed
72 73
    public function get($key)
    {
zYne's avatar
zYne committed
74
        if ( ! isset($this->_listeners[$key])) {
75
            return null;
lsmith's avatar
lsmith committed
76
        }
zYne's avatar
zYne committed
77
        return $this->_listeners[$key];
78
    }
79

80 81
    /**
     * set
lsmith's avatar
lsmith committed
82
     *
83 84 85 86
     * @param mixed $key
     * @param Doctrine_EventListener $listener
     * @return void
     */
lsmith's avatar
lsmith committed
87 88
    public function set($key, Doctrine_EventListener $listener)
    {
zYne's avatar
zYne committed
89
        $this->_listeners[$key] = $listener;
90
    }
91

zYne's avatar
zYne committed
92 93 94 95 96 97 98
    /**
     * onLoad
     * an event invoked when Doctrine_Record is being loaded from database
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
99 100
    public function onLoad(Doctrine_Record $record)
    {
zYne's avatar
zYne committed
101
        foreach ($this->_listeners as $listener) {
102
            $listener->onLoad($record);
103 104
        }
    }
105

zYne's avatar
zYne committed
106 107 108 109 110 111 112 113
    /**
     * 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
114 115
    public function onPreLoad(Doctrine_Record $record)
    {
zYne's avatar
zYne committed
116
        foreach ($this->_listeners as $listener) {
117 118 119
            $listener->onPreLoad($record);
        }
    }
120

zYne's avatar
zYne committed
121 122 123 124 125 126 127
    /**
     * onSleep
     * an event invoked when Doctrine_Record is serialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
128 129
    public function onSleep(Doctrine_Record $record)
    {
zYne's avatar
zYne committed
130
        foreach ($this->_listeners as $listener) {
131 132 133
            $listener->onSleep($record);
        }
    }
134

zYne's avatar
zYne committed
135 136 137 138 139 140 141
    /**
     * onWakeUp
     * an event invoked when Doctrine_Record is unserialized
     *
     * @param Doctrine_Record $record
     * @return void
     */
lsmith's avatar
lsmith committed
142 143
    public function onWakeUp(Doctrine_Record $record)
    {
zYne's avatar
zYne committed
144
        foreach ($this->_listeners as $listener) {
145 146 147
            $listener->onWakeUp($record);
        }
    }
148

149
    /**
150
     * postClose
151 152
     * an event invoked after Doctrine_Connection is closed
     *
153
     * @param Doctrine_Event $event
154 155
     * @return void
     */
156
    public function postClose(Doctrine_Event $event)
lsmith's avatar
lsmith committed
157
    {
zYne's avatar
zYne committed
158
        foreach ($this->_listeners as $listener) {
159
            $listener->postClose($event);
160 161
        }
    }
162

163
    /**
164
     * preClose
165 166
     * an event invoked before Doctrine_Connection is closed
     *
167
     * @param Doctrine_Event $event
168 169
     * @return void
     */
170
    public function preClose(Doctrine_Event $event)
lsmith's avatar
lsmith committed
171
    {
zYne's avatar
zYne committed
172
        foreach ($this->_listeners as $listener) {
173
            $listener->preClose($event);
174 175
        }
    }
176

177 178 179 180 181 182 183
    /**
     * onOpen
     * an event invoked after Doctrine_Connection is opened
     *
     * @param Doctrine_Connection $connection
     * @return void
     */
lsmith's avatar
lsmith committed
184 185
    public function onOpen(Doctrine_Connection $connection)
    {
zYne's avatar
zYne committed
186
        foreach ($this->_listeners as $listener) {
187
            $listener->onOpen($connection);
188 189
        }
    }
190

191 192 193 194
    /**
     * onTransactionCommit
     * an event invoked after a Doctrine_Connection transaction is committed
     *
195
     * @param Doctrine_Event $event
196 197
     * @return void
     */
198
    public function postTransactionCommit(Doctrine_Event $event)
lsmith's avatar
lsmith committed
199
    {
zYne's avatar
zYne committed
200
        foreach ($this->_listeners as $listener) {
201
            $listener->postTransactionCommit($event);
202 203
        }
    }
204

205 206 207 208
    /**
     * onPreTransactionCommit
     * an event invoked before a Doctrine_Connection transaction is committed
     *
209
     * @param Doctrine_Event $event
210 211
     * @return void
     */
212
    public function preTransactionCommit(Doctrine_Event $event)
lsmith's avatar
lsmith committed
213
    {
zYne's avatar
zYne committed
214
        foreach ($this->_listeners as $listener) {
215
            $listener->preTransactionCommit($event);
216 217
        }
    }
218

219 220 221 222
    /**
     * onTransactionRollback
     * an event invoked after a Doctrine_Connection transaction is being rolled back
     *
223
     * @param Doctrine_Event $event
224 225
     * @return void
     */
226
    public function postTransactionRollback(Doctrine_Event $event)
lsmith's avatar
lsmith committed
227
    {
zYne's avatar
zYne committed
228
        foreach ($this->_listeners as $listener) {
229
            $listener->postTransactionRollback($event);
230 231
        }
    }
232

233 234 235 236
    /**
     * onPreTransactionRollback
     * an event invoked before a Doctrine_Connection transaction is being rolled back
     *
237
     * @param Doctrine_Event $event
238 239
     * @return void
     */
240
    public function preTransactionRollback(Doctrine_Event $event)
lsmith's avatar
lsmith committed
241
    {
zYne's avatar
zYne committed
242
        foreach ($this->_listeners as $listener) {
243
            $listener->preTransactionRollback($event);
244 245
        }
    }
246

247 248 249 250
    /**
     * onTransactionBegin
     * an event invoked after a Doctrine_Connection transaction has been started
     *
251
     * @param Doctrine_Event $event
252 253
     * @return void
     */
254
    public function postTransactionBegin(Doctrine_Event $event)
lsmith's avatar
lsmith committed
255
    {
zYne's avatar
zYne committed
256
        foreach ($this->_listeners as $listener) {
257
            $listener->postTransactionBegin($event);
258 259
        }
    }
260

261 262 263 264
    /**
     * onTransactionBegin
     * an event invoked before a Doctrine_Connection transaction is being started
     *
265
     * @param Doctrine_Event $event
266 267
     * @return void
     */
268
    public function preTransactionBegin(Doctrine_Event $event)
lsmith's avatar
lsmith committed
269
    {
zYne's avatar
zYne committed
270
        foreach ($this->_listeners as $listener) {
271
            $listener->preTransactionBegin($event);
272 273
        }
    }
274

275 276 277 278 279 280 281
    /**
     * onCollectionDelete
     * an event invoked after a Doctrine_Collection is being deleted
     *
     * @param Doctrine_Collection $collection
     * @return void
     */
lsmith's avatar
lsmith committed
282 283
    public function onCollectionDelete(Doctrine_Collection $collection)
    {
zYne's avatar
zYne committed
284
        foreach ($this->_listeners as $listener) {
gnat's avatar
gnat committed
285
            $listener->onCollectionDelete($collection);
286 287
        }
    }
288

289 290 291 292 293 294 295
    /**
     * onCollectionDelete
     * an event invoked after a Doctrine_Collection is being deleted
     *
     * @param Doctrine_Collection $collection
     * @return void
     */
lsmith's avatar
lsmith committed
296 297
    public function onPreCollectionDelete(Doctrine_Collection $collection)
    {
zYne's avatar
zYne committed
298
        foreach ($this->_listeners as $listener) {
299
            $listener->onPreCollectionDelete($collection);
300 301
        }
    }
302
    public function postConnect(Doctrine_Event $event)
303
    {
zYne's avatar
zYne committed
304
        foreach ($this->_listeners as $listener) {
305
            $listener->postConnect($event);
306 307
        }
    }
308 309
    public function preConnect(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
310
        foreach ($this->_listeners as $listener) {
311
            $listener->preConnect($event);
312 313
        }
    }
314
    public function preQuery(Doctrine_Event $event)
315
    { 
zYne's avatar
zYne committed
316
        foreach ($this->_listeners as $listener) {
317
            $listener->preQuery($event);
318 319
        }
    }
320 321
    public function postQuery(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
322
        foreach ($this->_listeners as $listener) {
323
            $listener->postQuery($event);
324 325 326
        }
    }

327
    public function prePrepare(Doctrine_Event $event)
328
    { 
zYne's avatar
zYne committed
329
        foreach ($this->_listeners as $listener) {
330
            $listener->prePrepare($event);
331 332
        }
    }
333 334
    public function postPrepare(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
335
        foreach ($this->_listeners as $listener) {
336
            $listener->postPrepare($event);
337 338 339
        }
    }

340 341
    public function preExec(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
342
        foreach ($this->_listeners as $listener) {
343
            $listener->preExec($event);
344 345
        }
    }
346 347
    public function postExec(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
348
        foreach ($this->_listeners as $listener) {
349
            $listener->postExec($event);
350 351
        }
    }
zYne's avatar
zYne committed
352 353 354

    public function preError(Doctrine_Event $event)
    { 
zYne's avatar
zYne committed
355
        foreach ($this->_listeners as $listener) {
zYne's avatar
zYne committed
356 357 358 359 360
            $listener->preError($event);
        }
    }
    public function postError(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
361
        foreach ($this->_listeners as $listener) {
zYne's avatar
zYne committed
362 363 364 365
            $listener->postError($event);
        }
    }

366
    public function preFetch(Doctrine_Event $event)
367
    { 
zYne's avatar
zYne committed
368
        foreach ($this->_listeners as $listener) {
369
            $listener->preFetch($event);
370 371
        }
    }
372 373
    public function postFetch(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
374
        foreach ($this->_listeners as $listener) {
375
            $listener->postFetch($event);
376 377 378
        }
    }

379
    public function preFetchAll(Doctrine_Event $event)
380
    { 
zYne's avatar
zYne committed
381
        foreach ($this->_listeners as $listener) {
382
            $listener->preFetchAll($event);
383 384 385
        }
    }

386 387
    public function postFetchAll(Doctrine_Event $event)
    {
zYne's avatar
zYne committed
388
        foreach ($this->_listeners as $listener) {
389
            $listener->postFetchAll($event);
390 391 392
        }
    }

zYne's avatar
zYne committed
393
    public function preStmtExecute(Doctrine_Event $event)
394
    {
zYne's avatar
zYne committed
395
        foreach ($this->_listeners as $listener) {
zYne's avatar
zYne committed
396
            $listener->preStmtExecute($event);
397 398 399
        }
    }

zYne's avatar
zYne committed
400
    public function postStmtExecute(Doctrine_Event $event)
401
    {
zYne's avatar
zYne committed
402
        foreach ($this->_listeners as $listener) {
zYne's avatar
zYne committed
403
            $listener->postStmtExecute($event);
404 405
        }
    }
zYne's avatar
zYne committed
406
}