Completed
Push — master ( 0c2cb4...672bed )
by Richard
07:48
created

PluginsManager::updatePlugins()   D

Complexity

Conditions 9
Paths 45

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 45
nop 0
dl 0
loc 35
rs 4.909
c 0
b 0
f 0
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
13
/**
14
 * @copyright 2013-2015 XOOPS Project (http://xoops.org)
15
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @author    trabis <[email protected]>
17
 */
18
class PluginsManager
19
{
20
21
    /**
22
     * @return array returns an array of listeners in the form of listener=>caller
23
     */
24
    public static function getListeners()
25
    {
26
        $xoops = \Xoops::getInstance();
27
        $dirNames = $xoops->getActiveModules();
28
        $listeners = array();
29
30
        foreach ($dirNames as $listener) {
31
            foreach ($dirNames as $caller) {
32
                //Make sure to load the interface
33 View Code Duplication
                if (\XoopsLoad::loadFile($xoops->path("modules/{$caller}/class/plugin/interface.php"))) {
34
                    if (\XoopsLoad::loadFile($xoops->path("modules/{$listener}/class/plugin/{$caller}.php"))) {
35
                        $interfaceName = '\\' . ucfirst($caller) . "PluginInterface";
36
                        if ($ref = new ReflectionClass($interfaceName)) {
37
                            if ($ref->implementsInterface($interfaceName)) {
38
                                $listeners[$listener][] = $caller;
39
                            }
40
                        }
41
                    }
42
                }
43
            }
44
45
        }
46
        return $listeners;
47
    }
48
49
    /**
50
     * Checks if new plugins are available and adds them to database
51
     *
52
     * @return bool
53
     */
54
    public static function updatePlugins()
55
    {
56
        $ret = true;
57
        $handler = Plugins::getInstance()->getHandlerPlugin();
0 ignored issues
show
Bug introduced by
The method getHandlerPlugin() does not exist on Xoops\Module\Helper\HelperAbstract. Did you maybe mean getHandler()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
58
        $xoops = \Xoops::getInstance();
59
60
        $listeners = $handler->getListeners();
61
        foreach ($listeners as $key => $name ) {
62
            if (!$xoops->isActiveModule($key)) {
63
                $handler->deleteLC($key);
64
            }
65
        }
66
        $callers = $handler->getCallers();
67
        foreach ($callers as $key => $name) {
68
            if (!$xoops->isActiveModule($key)) {
69
                $handler->deleteLC($key);
70
            }
71
        }
72
73
        //Gets Listeners from file
74
        $plugins = self::getListeners();
75
        foreach ($plugins as $listener => $callers) {
76
            foreach ($callers as $caller) {
77
                if (!$object = $handler->getLC($listener, $caller)) {
78
                    if (!$handler->addNew($listener, $caller)) {
79
                        $ret = false;
80
                    };
81
                }
82
            }
83
        }
84
85
86
87
        return $ret;
88
    }
89
}
90