Passed
Push — cirework ( 7349fb...97b46e )
by Richard
04:26
created

Plugin::getPlugin()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 3
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 3.0175
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
namespace Xoops\Module;
13
14
/**
15
 * @copyright 2013-2015 XOOPS Project (http://xoops.org)
16
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @author    trabis <[email protected]>
18
 */
19
20
class Plugin
21
{
22
    /**
23
     * @var array
24
     */
25
    protected static $plugins = array();
26
27
    /**
28
     * @param string $dirname    module dirname
29
     * @param string $pluginName plugin name i.e. system, menus, etc.
30
     * @param bool   $force      get plugin even if module is inactive
31
     *
32
     * @return bool|object plugin, or false if plugin does not exist
33
     */
34 1
    public static function getPlugin($dirname, $pluginName = 'system', $force = false)
35
    {
36 1
        $inactiveModules = false;
37 1
        if ($force) {
38
            $inactiveModules = array($dirname);
39
        }
40 1
        $available = self::getPlugins($pluginName, $inactiveModules);
41 1
        if (!in_array($dirname, array_keys($available))) {
42 1
            return false;
43
        }
44 1
        return $available[$dirname];
45
    }
46
47
    /**
48
     * @param string $pluginName
49
     * @param array|bool $inactiveModules
50
     *
51
     * @return mixed
52
     */
53 2
    public static function getPlugins($pluginName = 'system', $inactiveModules = false)
54
    {
55 2
        if (!isset(static::$plugins[$pluginName])) {
56 1
            static::$plugins[$pluginName] = array();
57 1
            $xoops = \Xoops::getInstance();
58
59
            //Load interface for this plugin
60 1
            if (!\XoopsLoad::loadFile($xoops->path("modules/{$pluginName}/class/plugin/interface.php"))) {
61
                return static::$plugins[$pluginName];
62
            }
63
64 1
            $dirnames = $xoops->getActiveModules();
65
66 1
            \Xoops::getInstance()->events()->triggerEvent('core.module.plugin.get.plugins', array(&$dirnames, $pluginName));
67
68 1
            if (is_array($inactiveModules)) {
69
                $dirnames = array_merge($dirnames, $inactiveModules);
70
            }
71 1
            foreach ($dirnames as $dirname) {
72 1
                if (\XoopsLoad::loadFile($xoops->path("modules/{$dirname}/class/plugin/{$pluginName}.php"))) {
73 1
                    $className = '\\' . ucfirst($dirname) . ucfirst($pluginName) . 'Plugin';
74 1
                    $interface = '\\' . ucfirst($pluginName) . 'PluginInterface';
75 1
                    $class = new $className($dirname);
76 1
                    if ($class instanceof $interface) {
77 1
                        static::$plugins[$pluginName][$dirname] = $class;
78
                    }
79
                }
80
            }
81
        }
82 2
        return static::$plugins[$pluginName];
83
    }
84
85
    /**
86
     * Clear cache of plugins
87
     * return void
88
     */
89
    public static function resetPluginsCache()
90
    {
91
        static::$plugins = array();
92
    }
93
}
94