Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?PHP
2
/**
3
 * example that shows how to create event bubbling
4
 *
5
 * This allows you to create several levels of event handling and you
6
 * may post a notification to any of these levels.
7
 *
8
 * After a notification has been posted on a lower level, it will bubble
9
 * up through all other levels.
10
 *
11
 * @package    Event_Dispatcher
12
 * @subpackage Examples
13
 * @author     Stephan Schmidt <schst@php.net>
14
 */
15
 
16
/**
17
 * load Event_Dispatcher package
18
 */
19
require_once 'Event/Dispatcher.php';
20
 
21
/**
22
 * example sender class
23
 */
24
class sender
25
{
26
    var $_dispatcher = null;
27
 
28
    function sender(&$dispatcher)
29
    {
30
        $this->_dispatcher = &$dispatcher;
31
    }
32
 
33
    function foo($bubble = true)
34
    {
35
        $this->_dispatcher->post($this, 'onFoo', 'Some Info...', true, $bubble);
36
    }
37
}
38
 
39
/**
40
 * example observer
41
 */
42
function receiver1(&$notification)
43
{
44
    echo "receiver 1 received notification<br />\n";
45
}
46
 
47
/**
48
 * example observer
49
 */
50
function receiver2(&$notification)
51
{
52
    echo "receiver 2 received notification<br />\n";
53
}
54
 
55
/**
56
 * example observer
57
 */
58
function receiver3(&$notification)
59
{
60
    echo "receiver 3 received notification<br />\n";
61
}
62
 
63
// get the different dispatchers
64
$dispatcher1 = &Event_Dispatcher::getInstance();
65
$dispatcher2 = &Event_Dispatcher::getInstance('child');
66
$dispatcher3 = &Event_Dispatcher::getInstance('grandchild');
67
 
68
// create senders in two different levels
69
$sender1 = &new sender($dispatcher1);
70
$sender2 = &new sender($dispatcher2);
71
 
72
// build three levels
73
$dispatcher1->addNestedDispatcher($dispatcher2);
74
$dispatcher2->addNestedDispatcher($dispatcher3);
75
 
76
// add observers in level one and two
77
$dispatcher1->addObserver('receiver1', 'onFoo');
78
$dispatcher2->addObserver('receiver2', 'onFoo');
79
 
80
// this will bubble up from 1 to 3
81
echo 'sender1->foo()<br />';
82
$sender1->foo();
83
 
84
// this will not bubble up
85
echo '<br />';
86
echo 'sender1->foo(), but disable bubbling<br />';
87
$sender1->foo(false);
88
 
89
 
90
// this will bubble up from 2 to 3
91
echo '<br />';
92
echo 'sender2->foo()<br />';
93
$sender2->foo();
94
 
95
// This observer will receive the two pending notifications on level 3
96
echo '<br />';
97
echo 'dispatcher3->addObserver()<br />';
98
$dispatcher3->addObserver('receiver3', 'onFoo');
99
 
100
// remove one level
101
$success = $dispatcher1->removeNestedDispatcher($dispatcher2);
102
if ($success === true) {
103
    echo '<br />';
104
	echo 'removed nested dispatcher2 from dispatcher1<br />';
105
}
106
 
107
// this will stay in level 1
108
echo 'sender1->foo()<br />';
109
$sender1->foo();
110
 
111
// this will bubble up from 2-3
112
echo '<br />';
113
echo 'sender2->foo()<br />';
114
$sender2->foo();
115
?>