@@ -35,13 +35,51 @@ class CakeEventManager {
35
35
*/
36
36
public static $ defaultPriority = 10 ;
37
37
38
+ /**
39
+ * The globally available instance, used for dispatching events attached from any scope
40
+ *
41
+ * @var CakeEventManager
42
+ */
43
+ protected static $ _generalManager = null ;
44
+
38
45
/**
39
46
* List of listener callbacks associated to
40
47
*
41
48
* @var object $Listeners
42
49
*/
43
50
protected $ _listeners = array ();
44
51
52
+ /**
53
+ * Internal flag to distinguish a common manager from the sigleton
54
+ *
55
+ * @var boolean
56
+ */
57
+ protected $ _isGlobal = false ;
58
+
59
+
60
+ /**
61
+ * Returns the globally available instance of a CakeEventManager
62
+ * this is used for dispatching events attached from outside the scope
63
+ * other managers were created. Usually for creating hook systems or inter-class
64
+ * communication
65
+ *
66
+ * If called with a first params, it will be set as the globally available instance
67
+ *
68
+ * @param CakeEventManager $manager
69
+ * @return CakeEventManager the global event manager
70
+ */
71
+ public static function instance ($ manager = null ) {
72
+ if ($ manager instanceof CakeEventManager) {
73
+ self ::$ _generalManager = $ manager ;
74
+ }
75
+ if (empty (self ::$ _generalManager )) {
76
+ self ::$ _generalManager = new CakeEventManager ;
77
+ }
78
+
79
+ self ::$ _generalManager ->_isGlobal = true ;
80
+ return self ::$ _generalManager ;
81
+ }
82
+
45
83
/**
46
84
* Adds a new listener to an event. Listeners
47
85
*
@@ -142,6 +180,11 @@ public function dispatch($event) {
142
180
if (is_string ($ event )) {
143
181
$ Event = new CakeEvent ($ event );
144
182
}
183
+
184
+ if (!$ this ->_isGlobal ) {
185
+ self ::instance ()->dispatch ($ event );
186
+ }
187
+
145
188
if (empty ($ this ->_listeners [$ event ->name ()])) {
146
189
return ;
147
190
}
0 commit comments