public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
Search Repo:
Add the add_temp_event_handler method to CmsEventOperations
Add associated tests
Change CmsEventOperations to be able to work as a singleton as well

Signed-off-by: Ted Kulp <ted@cmsmadesimple.org>


git-svn-id: http://svn.cmsmadesimple.org/svn/cmsmadesimple/trunk@4351 
3d254a34-79dc-0310-9e5f-be208747d8a0
tedkulp (author)
Fri Feb 15 21:53:39 -0800 2008
commit  5c119d9d9f6505727a9e640e11de6c29fcf92b92
tree    7f308ecb2c65ca7939476a44d4d4a2692b91c38e
parent  4c1f6bdb64ad87cf91aa6719b2e62184751f3b35
...
31
32
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
35
36
...
100
101
102
103
104
105
 
 
 
 
 
 
 
 
 
 
 
106
107
108
...
125
126
127
128
 
129
130
131
...
134
135
136
 
 
 
 
 
 
 
 
 
 
 
 
137
138
139
...
252
253
254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
255
256
257
...
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
...
129
130
131
 
 
 
132
133
134
135
136
137
138
139
140
141
142
143
144
145
...
162
163
164
 
165
166
167
168
...
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
0
@@ -31,6 +31,35 @@
0
 class CmsEventOperations extends CmsObject
0
 {
0
   private static $handlercache;
0
+ private static $instance = null;
0
+
0
+ //arrays to handle "on the fly" event handlers
0
+ //either before or after the actual calls to the stored handlers
0
+ //(defaults to after)
0
+ public static $before_temp_calls = null;
0
+ public static $after_temp_calls = null;
0
+
0
+ function __construct()
0
+ {
0
+ parent::__construct();
0
+ }
0
+
0
+ /**
0
+ * Get an instance of this object, though most people should be using
0
+ * the static methods instead. This is more for compatibility than
0
+ * anything else.
0
+ *
0
+ * @return CmsUserTagOperations The instance of the singleton object.
0
+ * @author Ted Kulp
0
+ **/
0
+ static public function get_instance()
0
+ {
0
+ if (self::$instance == NULL)
0
+ {
0
+ self::$instance = new CmsEventOperations();
0
+ }
0
+ return self::$instance;
0
+ }
0
 
0
   /**
0
    * Inform the system about a new event that can be generated
0
@@ -100,9 +129,17 @@ class CmsEventOperations extends CmsObject
0
    **/
0
   public static function send_event( $modulename, $eventname, $params = array() )
0
   {
0
- //var_dump("Sending $eventname on $modulename");
0
- global $gCms;
0
- //$usertagops =& $gCms->GetUserTagOperations();
0
+ //Send "before" temp handlers
0
+ if (self::$before_temp_calls != null)
0
+ {
0
+ if (isset(self::$before_temp_calls[$modulename][$eventname]))
0
+ {
0
+ foreach (self::$before_temp_calls[$modulename][$eventname] as &$one_method)
0
+ {
0
+ call_user_func_array($one_method, $params);
0
+ }
0
+ }
0
+ }
0
 
0
     $results = self::list_event_handlers($modulename, $eventname);
0
     
0
@@ -125,7 +162,7 @@ class CmsEventOperations extends CmsObject
0
           }
0
 
0
           // and call the module event handler.
0
- $obj =& CmsModule::get_module_instance($row['module_name']);
0
+ $obj = CmsModule::get_module_instance($row['module_name']);
0
           if( $obj )
0
           {
0
             debug_buffer('calling module ' . $row['module_name'] . ' from event ' . $eventname);
0
@@ -134,6 +171,18 @@ class CmsEventOperations extends CmsObject
0
         }
0
       }
0
     }
0
+
0
+ //Send "after" temp handlers
0
+ if (self::$after_temp_calls != null)
0
+ {
0
+ if (isset(self::$after_temp_calls[$modulename][$eventname]))
0
+ {
0
+ foreach (self::$after_temp_calls[$modulename][$eventname] as &$one_method)
0
+ {
0
+ call_user_func_array($one_method, $params);
0
+ }
0
+ }
0
+ }
0
   }
0
   
0
   /**
0
@@ -252,6 +301,35 @@ class CmsEventOperations extends CmsObject
0
   {
0
     return self::add_event_handler( $modulename, $eventname, $tag_name, $module_handler, $removable );
0
   }
0
+
0
+ /**
0
+ * Add an event handler "on the fly" for a module event. This means that
0
+ * it only lasts for the duration of the request.
0
+ *
0
+ * @param string $module_name The name of the module sending the event
0
+ * @param string $event_name The name of the event
0
+ * @param array $method The callback method. This should be in the standard array($object, $method) structure
0
+ * @param boolean $before_calls Do we call this before or after the stored handlers? Defaults to false (after)
0
+ *
0
+ * @return void
0
+ */
0
+ public static function add_temp_event_handler($module_name, $event_name, $method, $before_calls = false)
0
+ {
0
+ if ($before_calls)
0
+ {
0
+ if (self::$before_temp_calls == null)
0
+ self::$before_temp_calls = array();
0
+
0
+ self::$before_temp_calls[$module_name][$event_name][] = $method;
0
+ }
0
+ else
0
+ {
0
+ if (self::$after_temp_calls == null)
0
+ self::$after_temp_calls = array();
0
+
0
+ self::$after_temp_calls[$module_name][$event_name][] = $method;
0
+ }
0
+ }
0
 
0
   /**
0
    * Remove an event handler for a particular event
...
20
21
22
 
 
23
24
25
...
67
68
69
 
 
 
 
 
70
71
72
...
174
175
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
178
179
...
20
21
22
23
24
25
26
27
...
69
70
71
72
73
74
75
76
77
78
79
...
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
0
@@ -20,6 +20,8 @@
0
 
0
 class TestCmsEventOperations extends UnitTestCase
0
 {
0
+ var $callback_test = false;
0
+
0
   function __construct()
0
   {
0
     parent::__construct();
0
@@ -67,6 +69,11 @@ class TestCmsEventOperations extends UnitTestCase
0
     return cms_orm('CmsEventHandler')->find_count_by_module_name_or_tag_name('TestModuleMethod', 'TestEventTag');
0
   }
0
   
0
+ function temp_callback(&$params)
0
+ {
0
+ $this->callback_test = $this->callback_test + 1;
0
+ }
0
+
0
   function test_create_event()
0
   {
0
     $this->create_event();
0
@@ -174,6 +181,21 @@ class TestCmsEventOperations extends UnitTestCase
0
     $this->create_event_handler_for_module();
0
     $this->assertTrue(CmsEventOperations::list_events() > 0);
0
   }
0
+
0
+ function test_add_temp_event_handler()
0
+ {
0
+ $this->callback_test = 1;
0
+ $this->create_event();
0
+ CmsEventOperations::add_temp_event_handler('TestModule', 'TestEvent1', array($this, 'temp_callback'));
0
+ CmsEventOperations::send_event('TestModule', 'TestEvent1', array('test stuff'));
0
+ $this->assertEqual($this->callback_test, 2);
0
+ CmsEventOperations::add_temp_event_handler('TestModule', 'TestEvent1', array($this, 'temp_callback'));
0
+ CmsEventOperations::send_event('TestModule', 'TestEvent1', array('test stuff'));
0
+ $this->assertEqual($this->callback_test, 4);
0
+ CmsEventOperations::add_temp_event_handler('TestModule', 'TestEvent1', array($this, 'temp_callback'), true);
0
+ CmsEventOperations::send_event('TestModule', 'TestEvent1', array('test stuff'));
0
+ $this->assertEqual($this->callback_test, 7);
0
+ }
0
 }
0
 
0
 # vim:ts=4 sw=4 noet

Comments

    No one has commented yet.