Merge branch 'maint/7.0'
[ninja.git] / system / libraries / Event_Observer.php
blobe4b194fd55cad004850457633467cc147a7d13b2
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Kohana event observer. Uses the SPL observer pattern.
5 * $Id: Event_Observer.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Core
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 abstract class Event_Observer implements SplObserver {
14 // Calling object
15 protected $caller;
17 /**
18 * Initializes a new observer and attaches the subject as the caller.
20 * @param object Event_Subject
21 * @return void
23 public function __construct(SplSubject $caller)
25 // Update the caller
26 $this->update($caller);
29 /**
30 * Updates the observer subject with a new caller.
32 * @chainable
33 * @param object Event_Subject
34 * @return object
36 public function update(SplSubject $caller)
38 if ( ! ($caller instanceof Event_Subject))
39 throw new Kohana_Exception('event.invalid_subject', get_class($caller), get_class($this));
41 // Update the caller
42 $this->caller = $caller;
44 return $this;
47 /**
48 * Detaches this observer from the subject.
50 * @chainable
51 * @return object
53 public function remove()
55 // Detach this observer from the caller
56 $this->caller->detach($this);
58 return $this;
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.
65 * @param mixed message string, object, or array
66 * @return void
68 abstract public function notify($message);
70 } // End Event Observer