Plugin::setPlugins()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
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');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
/** @scrutinizer ignore-call */ 
27
xoops_load('XoopsLists');
Loading history...
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);
0 ignored issues
show
Bug introduced by
The type XoopsLists was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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