1: <?php
2: /**
3: * Xoops Preload Classes
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @subpackage class
16: * @since 2.4.0
17: * @author trabis <lusopoemas@gmail.com>
18: * @deprecated To be deprecated in XOOPS 3
19: */
20:
21: /**
22: * XOOPS preload implemented in XOOPS is different from methods defined in this class, thus module developers are advised to be careful if you use current preload methods
23: */
24:
25: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
26:
27: XoopsLoad::load('XoopsLists');
28: XoopsLoad::load('XoopsCache');
29:
30: /**
31: * Class for handling events
32: *
33: * @copyright (c) 2000-2020 XOOPS Project (https://xoops.org)
34: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
35: * @package kernel
36: * @subpackage class
37: * @author trabis <lusopoemas@gmail.com>
38: */
39: class XoopsPreload
40: {
41: /**
42: * @var array $_preloads array containing information about the event observers
43: */
44: public $_preloads = array();
45:
46: /**
47: * @var array $_events array containing the events that are being observed
48: */
49: public $_events = array();
50:
51: /**
52: * Constructor
53: *
54: */
55: protected function __construct()
56: {
57: $this->setPreloads();
58: $this->setEvents();
59: }
60:
61: /**
62: * Allow one instance only!
63: *
64: * @return object
65: */
66: public static function getInstance()
67: {
68: static $instance = false;
69: if (!$instance) {
70: $instance = new XoopsPreload();
71: }
72:
73: return $instance;
74: }
75:
76: /**
77: * Get available preloads information and set them to go!
78: *
79: * @return void
80: */
81: public function setPreloads()
82: {
83: //$modules_list = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . "/modules/");
84: if ($modules_list = XoopsCache::read('system_modules_active')) {
85: $i = 0;
86: foreach ($modules_list as $module) {
87: if (is_dir($dir = XOOPS_ROOT_PATH . "/modules/{$module}/preloads/")) {
88: $file_list = XoopsLists::getFileListAsArray($dir);
89: foreach ($file_list as $file) {
90: if (preg_match('/(\.php)$/i', $file)) {
91: $file = substr($file, 0, -4);
92: if ('index' !== $file) {
93: $this->_preloads[$i]['module'] = $module;
94: $this->_preloads[$i]['file'] = $file;
95: ++$i;
96: }
97: }
98: }
99: }
100: }
101: }
102: }
103:
104: /**
105: * Get available events and set them to go!
106: *
107: * @return void
108: */
109: public function setEvents()
110: {
111: foreach ($this->_preloads as $preload) {
112: include_once XOOPS_ROOT_PATH . '/modules/' . $preload['module'] . '/preloads/' . $preload['file'] . '.php';
113: $class_name = ucfirst($preload['module']) . ucfirst($preload['file']) . 'Preload';
114: if (!class_exists($class_name)) {
115: continue;
116: }
117: $class_methods = get_class_methods($class_name);
118: foreach ($class_methods as $method) {
119: if (strpos($method, 'event') === 0) {
120: $event_name = strtolower(str_replace('event', '', $method));
121: $event = array('class_name' => $class_name, 'method' => $method);
122: $this->_events[$event_name][] = $event;
123: }
124: }
125: }
126: }
127:
128: /**
129: * Triggers a specific event
130: *
131: * @param string $event_name Name of the event to trigger
132: * @param array $args Method arguments
133: *
134: * @return void
135: */
136: public function triggerEvent($event_name, $args = array())
137: {
138: $event_name = strtolower(str_replace('.', '', $event_name));
139: if (isset($this->_events[$event_name])) {
140: foreach ($this->_events[$event_name] as $event) {
141: call_user_func(array($event['class_name'], $event['method']), $args);
142: }
143: }
144: }
145: }
146:
147: /**
148: * XoopsPreloadItem
149: *
150: * Class which is extended by any preload item.
151: *
152: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
153: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
154: * @package kernel
155: * @subpackage class
156: * @author trabis <lusopoemas@gmail.com>
157: */
158: class XoopsPreloadItem
159: {
160: /**
161: * XoopsPreloadItem constructor.
162: */
163: public function __construct()
164: {
165: }
166: }
167: