Passed
Push — develop ( d8a719...f4d730 )
by Felipe
04:10
created

PluginManager::doHook()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 5
nop 2
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.44
5
 */
6
7
namespace PHPPgAdmin;
8
9
/**
10
 * @file
11
 * A class that implements the plugin's system
12
 */
13
14
/**
15
 * A class that implements the plugin's system.
16
 *
17
 * @package PHPPgAdmin
18
 */
19
class PluginManager
20
{
21
    use \PHPPgAdmin\Traits\HelperTrait;
22
    /**
23
     * Attributes.
24
     */
25
    private $_plugins_list    = [];
26
    private $_available_hooks = [
27
        'head',
28
        'toplinks',
29
        'tabs',
30
        'trail',
31
        'navlinks',
32
        'actionbuttons',
33
        'tree',
34
        'logout',
35
    ];
36
    private $_actions = [];
37
    private $_hooks   = [];
38
39
    /**
40
     * Register the plugins.
41
     *
42
     * @param \Slim\Container $container
43
     *
44
     * @internal param $this ->language - Language that have been used
45
     *
46
     * @throws \Interop\Container\Exception\ContainerException
47
     * @throws \Slim\Exception\ContainerValueNotFoundException
48
     */
49
    public function __construct(\Slim\Container $container)
50
    {
51
        $this->language  = $container->has('language') ? $container->get('language') : 'english';
52
        $this->lang      = $container->get('lang');
53
        $this->conf      = $container->get('conf');
54
        $this->container = $container;
55
        if (!isset($this->conf['plugins'])) {
56
            return;
57
        }
58
59
        // Get the activated plugins
60
        $plugins = $this->conf['plugins'];
61
62
        foreach ($plugins as $activated_plugin) {
63
            $plugin_file = \BASE_PATH.'/src/plugins/'.$activated_plugin.'/plugin.php';
64
65
            // Verify is the activated plugin exists
66
            if (file_exists($plugin_file)) {
67
                include_once $plugin_file;
68
69
                try {
70
                    $plugin = new $activated_plugin($this->language);
71
                    $this->addPlugin($plugin);
72
                } catch (\Exception $e) {
73
                    continue;
74
                }
75
            } else {
76
                $this->halt(sprintf($this->lang['strpluginnotfound']."\t\n", $activated_plugin));
77
            }
78
        }
79
    }
80
81
    /**
82
     * Add a plugin in the list of plugins to manage.
83
     *
84
     * @param mixed $plugin - Instance from plugin
85
     */
86
    public function addPlugin($plugin)
87
    {
88
        //The $plugin_name is the identification of the plugin.
89
        //Example: PluginExample is the identification for PluginExample
90
        //It will be used to get a specific plugin from the _plugins_list.
91
        $plugin_name                       = $plugin->get_name();
92
        $this->_plugins_list[$plugin_name] = $plugin;
93
94
        //Register the plugin's functions
95
        $_hooks = $plugin->get__hooks();
96
        foreach ($_hooks as $hook => $functions) {
97
            if (!in_array($hook, $this->_available_hooks, true)) {
98
                $this->halt(sprintf($this->lang['strhooknotfound']."\t\n", $hook));
99
            }
100
            $this->_hooks[$hook][$plugin_name] = $functions;
101
        }
102
103
        //Register the plugin's _actions
104
        $_actions                     = $plugin->get__actions();
105
        $this->_actions[$plugin_name] = $_actions;
106
    }
107
108
    public function getPlugin($plugin)
109
    {
110
        if (isset($this->_plugins_list[$plugin])) {
111
            return $this->_plugins_list[$plugin];
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * Execute the plugins hook functions when needed.
119
     *
120
     * @param string $hook          - The place where the function will be called
121
     * @param array  $function_args - An array reference with arguments to give to called function
122
     */
123
    public function doHook($hook, &$function_args)
124
    {
125
        //$this->prtrace('_hooks', $this->_hooks, $function_args);
126
        if (isset($this->_hooks[$hook])) {
127
            foreach ($this->_hooks[$hook] as $plugin_name => $functions) {
128
                $plugin = $this->_plugins_list[$plugin_name];
129
                foreach ($functions as $function) {
130
                    if (method_exists($plugin, $function)) {
131
                        call_user_func([$plugin, $function], $function_args);
132
                    }
133
                }
134
            }
135
        }
136
    }
137
138
    /**
139
     * Execute a plugin's action.
140
     *
141
     * @param string $plugin_name - The plugin name
142
     * @param string $action      - action that will be executed
143
     */
144
    public function do_action($plugin_name, $action)
145
    {
146
        if (!isset($this->_plugins_list[$plugin_name])) {
147
            // Show an error and stop the application
148
            $this->halt(sprintf($this->lang['strpluginnotfound']."\t\n", $plugin_name));
149
        }
150
        $plugin = $this->_plugins_list[$plugin_name];
151
152
        // Check if the plugin's method exists and if this method is an declared action.
153
        if (method_exists($plugin, $action) and in_array($action, $this->_actions[$plugin_name], true)) {
154
            call_user_func([$plugin, $action]);
155
        } else {
156
            // Show an error and stop the application
157
            $this->halt(sprintf($this->lang['stractionnotfound']."\t\n", $action, $plugin_name));
158
        }
159
    }
160
}
161