Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
require_once dirname(__FILE__).'/sfEvent.php';
4
 
5
/*
6
 * This file is part of the symfony package.
7
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
 
13
/**
14
 * sfEventDispatcher implements a dispatcher object.
15
 *
16
 * @see http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/index.html Apple's Cocoa framework
17
 *
18
 * @package    symfony
19
 * @subpackage event_dispatcher
20
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21
 * @version    SVN: $Id: sfEventDispatcher.class.php 10631 2008-08-03 16:50:47Z fabien $
22
 */
23
class sfEventDispatcher
24
{
25
  protected
26
    $listeners = array();
27
 
28
  /**
29
   * Connects a listener to a given event name.
30
   *
31
   * @param string  $name      An event name
32
   * @param mixed   $listener  A PHP callable
33
   */
34
  public function connect($name, $listener)
35
  {
36
    if (!isset($this->listeners[$name]))
37
    {
38
      $this->listeners[$name] = array();
39
    }
40
 
41
    $this->listeners[$name][] = $listener;
42
  }
43
 
44
  /**
45
   * Disconnects a listener for a given event name.
46
   *
47
   * @param string   $name      An event name
48
   * @param mixed    $listener  A PHP callable
49
   *
50
   * @return mixed false if listener does not exist, null otherwise
51
   */
52
  public function disconnect($name, $listener)
53
  {
54
    if (!isset($this->listeners[$name]))
55
    {
56
      return false;
57
    }
58
 
59
    foreach ($this->listeners[$name] as $i => $callable)
60
    {
61
      if ($listener === $callable)
62
      {
63
        unset($this->listeners[$name][$i]);
64
      }
65
    }
66
  }
67
 
68
  /**
69
   * Notifies all listeners of a given event.
70
   *
71
   * @param sfEvent $event A sfEvent instance
72
   *
73
   * @return sfEvent The sfEvent instance
74
   */
75
  public function notify(sfEvent $event)
76
  {
77
    foreach ($this->getListeners($event->getName()) as $listener)
78
    {
79
      call_user_func($listener, $event);
80
    }
81
 
82
    return $event;
83
  }
84
 
85
  /**
86
   * Notifies all listeners of a given event until one returns a non null value.
87
   *
88
   * @param  sfEvent $event A sfEvent instance
89
   *
90
   * @return sfEvent The sfEvent instance
91
   */
92
  public function notifyUntil(sfEvent $event)
93
  {
94
    foreach ($this->getListeners($event->getName()) as $listener)
95
    {
96
      if (call_user_func($listener, $event))
97
      {
98
        $event->setProcessed(true);
99
        break;
100
      }
101
    }
102
 
103
    return $event;
104
  }
105
 
106
  /**
107
   * Filters a value by calling all listeners of a given event.
108
   *
109
   * @param  sfEvent  $event   A sfEvent instance
110
   * @param  mixed    $value   The value to be filtered
111
   *
112
   * @return sfEvent The sfEvent instance
113
   */
114
  public function filter(sfEvent $event, $value)
115
  {
116
    foreach ($this->getListeners($event->getName()) as $listener)
117
    {
118
      $value = call_user_func_array($listener, array($event, $value));
119
    }
120
 
121
    $event->setReturnValue($value);
122
 
123
    return $event;
124
  }
125
 
126
  /**
127
   * Returns true if the given event name has some listeners.
128
   *
129
   * @param  string   $name    The event name
130
   *
131
   * @return Boolean true if some listeners are connected, false otherwise
132
   */
133
  public function hasListeners($name)
134
  {
135
    if (!isset($this->listeners[$name]))
136
    {
137
      $this->listeners[$name] = array();
138
    }
139
 
140
    return (boolean) count($this->listeners[$name]);
141
  }
142
 
143
  /**
144
   * Returns all listeners associated with a given event name.
145
   *
146
   * @param  string   $name    The event name
147
   *
148
   * @return array  An array of listeners
149
   */
150
  public function getListeners($name)
151
  {
152
    if (!isset($this->listeners[$name]))
153
    {
154
      return array();
155
    }
156
 
157
    return $this->listeners[$name];
158
  }
159
}