XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
events.php
Go to the documentation of this file.
1 <?php
2 // $Id: events.php 1049 2012-09-11 19:40:00Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 if (!defined('XOOPS_ROOT_PATH')) die("Sorry, there are not <a href='../'>nothing here</a>");
12 
13 //include_once 'langs/english.php';
14 
15 include_once RMCPATH.'/class/textcleaner.php';
16 
17 class RMEvents
18 {
19 
20  private $_events = array();
21  private $_preloads = array();
22 
23  public function __construct(){
24  $db = XoopsDatabaseFactory::getDatabaseConnection();
25 
26  $result = $db->query("SELECT dirname FROM ".$db->prefix("modules")." WHERE isactive='1'");
27 
28  $i = 0;
29  while (list($module) = $db->fetchRow($result)) {
30  if (is_dir($dir = XOOPS_ROOT_PATH . "/modules/{$module}/events/")) {
31  $file_list = XoopsLists::getFileListAsArray($dir);
32  foreach ($file_list as $file) {
33  if (preg_match('/(\.php)$/i', $file)) {
34  $file = substr($file, 0, -4);
35  $this->_preloads[$i]['module'] = $module;
36  $this->_preloads[$i]['file'] = $file;
37  $i++;
38  }
39  }
40  }
41  }
42 
43 
44  // Set Events
45  foreach ($this->_preloads as $preload) {
46  include_once XOOPS_ROOT_PATH . '/modules/' . $preload['module'] . '/events/' . $preload['file']. '.php';
47  $class_name = ucfirst($preload['module']) . ucfirst($preload['file']) . 'Preload' ;
48  if (!class_exists($class_name)) {
49  continue;
50  }
51  $class_methods = get_class_methods($class_name);
52  foreach ($class_methods as $method) {
53  if (strpos($method, 'event') === 0) {
54  $event_name = strtolower(str_replace('event', '', $method));
55  $event= array('class_name' => $class_name, 'method' => $method);
56  $this->_events[$event_name][] = $event;
57  }
58  }
59  }
60 
61  }
62 
66  public function get(){
67  static $instance;
68 
69  if (isset($instance))
70  return $instance;
71 
72  $instance = new RMEvents();
73  return $instance;
74  }
75 
76  public function load_extra_preloads($dir, $name){
77  $dir = rtrim($dir, '/');
78  $extra = array();
79  if (is_dir($dir.'/events')){
80  $file_list = XoopsLists::getFileListAsArray($dir.'/events');
81  foreach ($file_list as $file) {
82  if (preg_match('/(\.php)$/i', $file)) {
83  $file = substr($file, 0, -4);
84  $extra[] = $file;
85  }
86  }
87  }
88 
89  foreach ($extra as $preload) {
90  include_once $dir . '/events/' . $preload. '.php';
91  $class_name = ucfirst($name) . ucfirst($preload) . 'Preload' ;
92  if (!class_exists($class_name)) {
93  continue;
94  }
95  $class_methods = get_class_methods($class_name);
96  foreach ($class_methods as $method) {
97  if (strpos($method, 'event') === 0) {
98  $event_name = strtolower(str_replace('event', '', $method));
99  $event= array('class_name' => $class_name, 'method' => $method);
100  $this->_events[$event_name][] = $event;
101  }
102  }
103  }
104 
105  }
106 
110  public function run_event($event_name, $value=null)
111  {
112  $pre = $event_name;
113  $event_name = strtolower(str_replace('.', '', $event_name));
114  $args = func_get_args();
115  if (!isset($this->_events[$event_name])) return $value;
116 
117  $xoopsLogger = XoopsLogger::getInstance();
118 
119  foreach ($this->_events[$event_name] as $event) {
120  $args[1] =& $value;
121  $xoopsLogger->addExtra($pre, $event['class_name'].'::'.$event['method']);
122  $value = call_user_func_array(array($event['class_name'], $event['method']), array_slice($args, 1));
123  }
124  return $value;
125  }
126 
127 }