Completed
Push — master ( 089ad5...045fb9 )
by Richard
10:39
created

Plugin::resetPluginsCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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