1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * Kohana event subject. Uses the SPL observer pattern.
5 * $Id: Event_Subject.php 3917 2009-01-21 03:06:22Z zombor $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 abstract class Event_Subject
implements SplSubject
{
14 // Attached subject listeners
15 protected $listeners = array();
18 * Attach an observer to the object.
21 * @param object Event_Observer
24 public function attach(SplObserver
$obj)
26 if ( ! ($obj instanceof Event_Observer
))
27 throw new Kohana_Exception('eventable.invalid_observer', get_class($obj), get_class($this));
30 $this->listeners
[spl_object_hash($obj)] = $obj;
36 * Detach an observer from the object.
39 * @param object Event_Observer
42 public function detach(SplObserver
$obj)
44 // Remove the listener
45 unset($this->listeners
[spl_object_hash($obj)]);
51 * Notify all attached observers of a new message.
54 * @param mixed message string, object, or array
57 public function notify($message)
59 foreach ($this->listeners
as $obj)
61 $obj->notify($message);
67 } // End Event Subject