root/trunk/system/libraries/Event_Subject.php

Revision 2337, 1.3 kB (checked in by Shadowhand, 4 months ago)

Updated Calendar:

  • Added Easter as a supported holiday
  • Optimized Calendar_Event->notify() a bit

Also, minor tweaks to Event_Subject and Event_Observer

  • Property svn:eol-style set to LF
  • Property copyright set to Copyright (c) 2007-2008 Kohana Team
  • Property svn:keywords set to Id
Line 
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3  * Kohana event subject. 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_Subject implements SplSubject {
13
14     // Attached subject listeners
15     protected $listeners = array();
16
17     /**
18      * Attach an observer to the object.
19      *
20      * @chainable
21      * @param   object  Event_Observer
22      * @return  object
23      */
24     public function attach(SplObserver $obj)
25     {
26         if ( ! ($obj instanceof Event_Observer))
27             throw new Kohana_Exception('eventable.invalid_observer', get_class($obj), get_class($this));
28
29         // Add a new listener
30         $this->listeners[spl_object_hash($obj)] = $obj;
31
32         return $this;
33     }
34
35     /**
36      * Detach an observer from the object.
37      *
38      * @chainable
39      * @param   object  Event_Observer
40      * @return  object
41      */
42     public function detach(SplObserver $obj)
43     {
44         // Remove the listener
45         unset($this->listeners[spl_object_hash($obj)]);
46
47         return $this;
48     }
49
50     /**
51      * Notify all attached observers of a new message.
52      *
53      * @chainable
54      * @param   mixed   message string, object, or array
55      * @return  object
56      */
57     public function notify($message)
58     {
59         foreach ($this->listeners as $obj)
60         {
61             $obj->notify($message);
62         }
63
64         return $this;
65     }
66
67 } // End Event Subject
Note: See TracBrowser for help on using the browser.