root/trunk/system/libraries/Event_Observer.php
| Revision 3700, 1.4 kB (checked in by Shadowhand, 12 days ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | <?php |
| 2 | /** |
| 3 | * Kohana event observer. Uses the SPL observer pattern. |
| 4 | * |
| 5 | * $Id$ |
| 6 | * |
| 7 | * @package Core |
| 8 | * @author Kohana Team |
| 9 | * @copyright (c) 2007-2008 Kohana Team |
| 10 | * @license http://kohanaphp.com/license.html |
| 11 | */ |
| 12 | abstract class Event_Observer implements SplObserver { |
| 13 | |
| 14 | // Calling object |
| 15 | protected $caller; |
| 16 | |
| 17 | /** |
| 18 | * Initializes a new observer and attaches the subject as the caller. |
| 19 | * |
| 20 | * @param object Event_Subject |
| 21 | * @return void |
| 22 | */ |
| 23 | public function __construct(SplSubject $caller) |
| 24 | { |
| 25 | // Update the caller |
| 26 | $this->update($caller); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Updates the observer subject with a new caller. |
| 31 | * |
| 32 | * @chainable |
| 33 | * @param object Event_Subject |
| 34 | * @return object |
| 35 | */ |
| 36 | public function update(SplSubject $caller) |
| 37 | { |
| 38 | if ( ! ($caller instanceof Event_Subject)) |
| 39 | throw new Kohana_Exception('event.invalid_subject', get_class($caller), get_class($this)); |
| 40 | |
| 41 | // Update the caller |
| 42 | $this->caller = $caller; |
| 43 | |
| 44 | return $this; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Detaches this observer from the subject. |
| 49 | * |
| 50 | * @chainable |
| 51 | * @return object |
| 52 | */ |
| 53 | public function remove() |
| 54 | { |
| 55 | // Detach this observer from the caller |
| 56 | $this->caller->detach($this); |
| 57 | |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Notify the observer of a new message. This function must be defined in |
| 63 | * all observers and must take exactly one parameter of any type. |
| 64 | * |
| 65 | * @param mixed message string, object, or array |
| 66 | * @return void |
| 67 | */ |
| 68 | abstract public function notify($message); |
| 69 | |
| 70 | } // End Event Observer |
Note: See TracBrowser
for help on using the browser.
