Merge branch 'maint/7.0'
[ninja.git] / system / libraries / Event_Subject.php
blobf571d17737344ab7fe9bb0761801c84e09125e0a
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Kohana event subject. Uses the SPL observer pattern.
5 * $Id: Event_Subject.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_Subject implements SplSubject {
14 // Attached subject listeners
15 protected $listeners = array();
17 /**
18 * Attach an observer to the object.
20 * @chainable
21 * @param object Event_Observer
22 * @return object
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));
29 // Add a new listener
30 $this->listeners[spl_object_hash($obj)] = $obj;
32 return $this;
35 /**
36 * Detach an observer from the object.
38 * @chainable
39 * @param object Event_Observer
40 * @return object
42 public function detach(SplObserver $obj)
44 // Remove the listener
45 unset($this->listeners[spl_object_hash($obj)]);
47 return $this;
50 /**
51 * Notify all attached observers of a new message.
53 * @chainable
54 * @param mixed message string, object, or array
55 * @return object
57 public function notify($message)
59 foreach ($this->listeners as $obj)
61 $obj->notify($message);
64 return $this;
67 } // End Event Subject