|
1
|
|
|
<?php namespace XoopsModules\Mymenus; |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
You may not change or alter any portion of this comment or credits |
|
5
|
|
|
of supporting developers from this source code or any supporting source code |
|
6
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
|
7
|
|
|
|
|
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
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @copyright XOOPS Project (https://xoops.org) |
|
15
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU Public License |
|
16
|
|
|
* @package Mymenus |
|
17
|
|
|
* @since 1.0 |
|
18
|
|
|
* @author trabis <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use XoopsModules\Mymenus; |
|
22
|
|
|
|
|
23
|
|
|
defined('XOOPS_ROOT_PATH') || die('Restricted access'); |
|
24
|
|
|
|
|
25
|
|
|
//require dirname(__DIR__) . '/include/common.php'; |
|
26
|
|
|
xoops_load('XoopsLists'); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class Plugin |
|
30
|
|
|
*/ |
|
31
|
|
|
class Plugin |
|
32
|
|
|
{ |
|
33
|
|
|
protected $registry; |
|
34
|
|
|
protected $plugins; |
|
35
|
|
|
protected $events; |
|
36
|
|
|
public $helper; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->plugins = []; |
|
44
|
|
|
$this->events = []; |
|
45
|
|
|
$this->registry = Mymenus\Registry::getInstance(); |
|
46
|
|
|
/** @var \XoopsModules\Mymenus\Helper $this->helper */ |
|
47
|
|
|
$this->helper = \XoopsModules\Mymenus\Helper::getInstance(); |
|
48
|
|
|
$this->setPlugins(); |
|
49
|
|
|
$this->setEvents(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \XoopsModules\Mymenus\Plugin |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function getInstance() |
|
56
|
|
|
{ |
|
57
|
|
|
static $instance; |
|
58
|
|
|
if (null === $instance) { |
|
59
|
|
|
$instance = new static(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $instance; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setPlugins() |
|
66
|
|
|
{ |
|
67
|
|
|
if (is_dir($dir = $GLOBALS['xoops']->path("modules/{$this->helper->getDirname()}/class/Plugins/"))) { |
|
68
|
|
|
$pluginsList = \XoopsLists::getDirListAsArray($dir); |
|
|
|
|
|
|
69
|
|
|
foreach ($pluginsList as $plugin) { |
|
70
|
|
|
// if (file_exists($GLOBALS['xoops']->path("modules/{$this->helper->getDirname()}/plugins/{$plugin}/{$plugin}.php"))) { |
|
71
|
|
|
$dirname = $this->helper->getDirname(); |
|
72
|
|
|
$className = "\XoopsModules\{$dirname}\Plugins\{$plugin}\PluginItem"; |
|
73
|
|
|
if (class_exists($className)) { |
|
74
|
|
|
$this->plugins[] = $plugin; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function setEvents() |
|
81
|
|
|
{ |
|
82
|
|
|
foreach ($this->plugins as $plugin) { |
|
83
|
|
|
// require $GLOBALS['xoops']->path("modules/{$this->helper->getDirname()}/plugins/{$plugin}/{$plugin}.php"); |
|
84
|
|
|
$dirname = $this->helper->getDirname(); |
|
85
|
|
|
$className = "\XoopsModules\{$dirname}\Plugins\{$plugin}\PluginItem"; |
|
86
|
|
|
if (!class_exists($className)) { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
$classMethods = get_class_methods($className); |
|
90
|
|
|
foreach ($classMethods as $method) { |
|
91
|
|
|
if (0 === strpos($method, 'event')) { |
|
92
|
|
|
$eventName = strtolower(str_replace('event', '', $method)); |
|
93
|
|
|
$event = ['className' => $className, 'method' => $method]; |
|
94
|
|
|
$this->events[$eventName][] = $event; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $eventName |
|
102
|
|
|
* @param array $args |
|
103
|
|
|
*/ |
|
104
|
|
|
public function triggerEvent($eventName, $args = []) |
|
105
|
|
|
{ |
|
106
|
|
|
$eventName = mb_strtolower(str_replace('.', '', $eventName)); |
|
107
|
|
|
if (isset($this->events[(string)$eventName])) { |
|
108
|
|
|
foreach ($this->events[(string)$eventName] as $event) { |
|
109
|
|
|
call_user_func([$event['className'], $event['method']], $args); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|