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 (http://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
25defined('XOOPS_ROOT_PATH') || exit('Restricted access');
26
27XoopsLoad::load('XoopsLists');
28XoopsLoad::load('XoopsCache');
29
30/**
31 * Class for handling events
32 *
33 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
34 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
35 * @package             kernel
36 * @subpackage          class
37 * @author              trabis <lusopoemas@gmail.com>
38 */
39class 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                            $this->_preloads[$i]['module'] = $module;
93                            $this->_preloads[$i]['file']   = $file;
94                            ++$i;
95                        }
96                    }
97                }
98            }
99        }
100    }
101
102    /**
103     * Get available events and set them to go!
104     *
105     * @return void
106     */
107    public function setEvents()
108    {
109        foreach ($this->_preloads as $preload) {
110            include_once XOOPS_ROOT_PATH . '/modules/' . $preload['module'] . '/preloads/' . $preload['file'] . '.php';
111            $class_name = ucfirst($preload['module']) . ucfirst($preload['file']) . 'Preload';
112            if (!class_exists($class_name)) {
113                continue;
114            }
115            $class_methods = get_class_methods($class_name);
116            foreach ($class_methods as $method) {
117                if (strpos($method, 'event') === 0) {
118                    $event_name                   = strtolower(str_replace('event', '', $method));
119                    $event                        = array('class_name' => $class_name, 'method' => $method);
120                    $this->_events[$event_name][] = $event;
121                }
122            }
123        }
124    }
125
126    /**
127     * Triggers a specific event
128     *
129     * @param string $event_name Name of the event to trigger
130     * @param array  $args       Method arguments
131     *
132     * @return void
133     */
134    public function triggerEvent($event_name, $args = array())
135    {
136        $event_name = strtolower(str_replace('.', '', $event_name));
137        if (isset($this->_events[$event_name])) {
138            foreach ($this->_events[$event_name] as $event) {
139                call_user_func(array($event['class_name'], $event['method']), $args);
140            }
141        }
142    }
143}
144
145/**
146 * XoopsPreloadItem
147 *
148 * Class which is extended by any preload item.
149 *
150 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
151 * @license             GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
152 * @package             kernel
153 * @subpackage          class
154 * @author              trabis <lusopoemas@gmail.com>
155 */
156class XoopsPreloadItem
157{
158    /**
159     * XoopsPreloadItem constructor.
160     */
161    public function __construct()
162    {
163    }
164}
165