XOOPS  2.6.0
Events.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
12 namespace Xoops\Core;
13 
27 class Events
28 {
32  protected $preloadList = array();
33 
38  protected $eventListeners = array();
39 
43  protected $eventsEnabled = true;
44 
48  protected function __construct()
49  {
50  }
51 
57  public static function getInstance()
58  {
59  static $instance = false;
60 
61  if (!$instance) {
62  $instance = new \Xoops\Core\Events();
63  }
64 
65  return $instance;
66  }
67 
78  public function initializeListeners()
79  {
80  $this->eventsEnabled = false;
81  $this->setPreloads();
82  $this->setEvents();
83  $this->eventsEnabled = true;
84  }
85 
91  protected function setPreloads()
92  {
93  $cache = \Xoops::getInstance()->cache();
94  $key = 'system/modules/preloads';
95  if (!$this->preloadList = $cache->read($key)) {
96  // get active modules from the xoops instance
97  $modules_list = \Xoops::getInstance()->getActiveModules();
98  if (empty($modules_list)) {
99  // this should only happen if an exception was thrown in setActiveModules()
100  $modules_list = array ('system');
101  }
102  $this->preloadList =array();
103  $i = 0;
104  foreach ($modules_list as $module) {
105  if (is_dir($dir = \XoopsBaseConfig::get('root-path') . "/modules/{$module}/preloads/")) {
106  $file_list = \XoopsLists::getFileListAsArray($dir);
107  foreach ($file_list as $file) {
108  if (preg_match('/(\.php)$/i', $file)) {
109  $file = substr($file, 0, -4);
110  $this->preloadList[$i]['module'] = $module;
111  $this->preloadList[$i]['file'] = $file;
112  ++$i;
113  }
114  }
115  }
116  }
117  $cache->write($key, $this->preloadList);
118  }
119  }
120 
145  protected function setEvents()
146  {
147  foreach ($this->preloadList as $preload) {
148  include_once \XoopsBaseConfig::get('root-path') . '/modules/' . $preload['module'] . '/preloads/' . $preload['file']. '.php';
149  $class_name = ucfirst($preload['module'])
150  . ($preload['file'] == 'preload' ? '' : ucfirst($preload['file']) )
151  . 'Preload';
152  if (!class_exists($class_name)) {
153  continue;
154  }
155  $class_methods = get_class_methods($class_name);
156  foreach ($class_methods as $method) {
157  if (strpos($method, 'event') === 0) {
158  $eventName = strtolower(str_replace('event', '', $method));
159  $event = array($class_name, $method);
160  $this->eventListeners[$eventName][] = $event;
161  }
162  }
163  }
164  }
165 
174  public function triggerEvent($eventName, $args = array())
175  {
176  if ($this->eventsEnabled) {
177  $eventName = $this->toInternalEventName($eventName);
178  if (isset($this->eventListeners[$eventName])) {
179  foreach ($this->eventListeners[$eventName] as $event) {
180  if (is_callable($event)) {
181  call_user_func($event, $args);
182  }
183  }
184  }
185  }
186  }
187 
196  protected function toInternalEventName($eventName)
197  {
198  return strtolower(str_replace('.', '', $eventName));
199  }
200 
209  public function addListener($eventName, $callback)
210  {
211  $eventName = $this->toInternalEventName($eventName);
212  $this->eventListeners[$eventName][]=$callback;
213  }
214 
220  public function getEvents()
221  {
222  return $this->eventListeners;
223  }
224 
231  public function hasListeners($eventName)
232  {
233  $eventName = $this->toInternalEventName($eventName);
234  return array_key_exists($eventName, $this->eventListeners);
235  }
236 }
$i
Definition: dialog.php:68
static getInstance()
Definition: Events.php:57
static getInstance()
Definition: Xoops.php:160
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
addListener($eventName, $callback)
Definition: Events.php:209
toInternalEventName($eventName)
Definition: Events.php:196
$module
Definition: Xoops.php:38
static get($name)
hasListeners($eventName)
Definition: Events.php:231
$dir
Definition: browse.php:56
triggerEvent($eventName, $args=array())
Definition: Events.php:174