Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

PluginManager::__construct()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 12
nop 1
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
1
<?php
2
/*
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
 * PHPPgAdmin v6.0.0-beta.30
4
 */
5
namespace PHPPgAdmin;
6
7
/**
8
 * A class that implements the plugin's system
9
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
10
class PluginManager
11
{
12
    use \PHPPgAdmin\HelperTrait;
13
    /**
14
     * Attributes
15
     */
16
    private $plugins_list    = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "plugins_list" must be prefixed with an underscore
Loading history...
17
    private $available_hooks = [
0 ignored issues
show
Coding Style introduced by
Private member variable "available_hooks" must be prefixed with an underscore
Loading history...
18
        'head',
19
        'toplinks',
20
        'tabs',
21
        'trail',
22
        'navlinks',
23
        'actionbuttons',
24
        'tree',
25
        'logout',
26
    ];
27
    private $actions = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "actions" must be prefixed with an underscore
Loading history...
28
    private $hooks   = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "hooks" must be prefixed with an underscore
Loading history...
29
30
    /**
31
     * Register the plugins
32
     *
33
     * @param \Slim\Container $container
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
34
     * @internal param $this ->language - Language that have been used.
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
35
     * @throws \Interop\Container\Exception\ContainerException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
36
     * @throws \Slim\Exception\ContainerValueNotFoundException
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
37
     */
38
    public function __construct(\Slim\Container $container)
39
    {
40
        $this->language = $container->has('language') ? $container->get('language') : 'english';
41
        $this->lang     = $container->get('lang');
42
        $this->conf     = $container->get('conf');
43
44
        if (!isset($this->conf['plugins'])) {
45
            return;
46
        }
47
48
        // Get the activated plugins
49
        $plugins = $this->conf['plugins'];
50
51
        foreach ($plugins as $activated_plugin) {
52
            $plugin_file = \BASE_PATH . '/src/plugins/' . $activated_plugin . '/plugin.php';
53
54
            // Verify is the activated plugin exists
55
            if (file_exists($plugin_file)) {
56
                include_once $plugin_file;
57
                try {
58
                    $plugin = new $activated_plugin($this->language);
59
                    $this->add_plugin($plugin);
60
                } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type PHPPgAdmin\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
61
                    continue;
62
                }
63
            } else {
64
                $this->halt(sprintf($this->lang['strpluginnotfound'] . "\t\n", $activated_plugin));
65
            }
66
        }
67
    }
68
69
    /**
70
     * Add a plugin in the list of plugins to manage
71
     *
72
     * @param $plugin - Instance from plugin
73
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
74
    public function add_plugin($plugin)
1 ignored issue
show
Coding Style introduced by
Public method name "PluginManager::add_plugin" is not in camel caps format
Loading history...
75
    {
76
77
        //The $plugin_name is the identification of the plugin.
78
        //Example: PluginExample is the identification for PluginExample
79
        //It will be used to get a specific plugin from the plugins_list.
80
        $plugin_name                      = $plugin->get_name();
81
        $this->plugins_list[$plugin_name] = $plugin;
82
83
        //Register the plugin's functions
84
        $hooks = $plugin->get_hooks();
85
        foreach ($hooks as $hook => $functions) {
86
            if (!in_array($hook, $this->available_hooks)) {
87
                $this->halt(sprintf($this->lang['strhooknotfound'] . "\t\n", $hook));
88
            }
89
            $this->hooks[$hook][$plugin_name] = $functions;
90
        }
91
92
        //Register the plugin's actions
93
        $actions                     = $plugin->get_actions();
94
        $this->actions[$plugin_name] = $actions;
95
    }
96
97
    public function getPlugin($plugin)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
98
    {
99
        if (isset($this->plugins_list[$plugin])) {
100
            return $this->plugins_list[$plugin];
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     * Execute the plugins hook functions when needed.
108
     *
109
     * @param $hook - The place where the function will be called
110
     * @param $function_args - An array reference with arguments to give to called function
111
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
112
    public function do_hook($hook, &$function_args)
1 ignored issue
show
Coding Style introduced by
Public method name "PluginManager::do_hook" is not in camel caps format
Loading history...
113
    {
114
        if (isset($this->hooks[$hook])) {
115
            foreach ($this->hooks[$hook] as $plugin_name => $functions) {
116
                $plugin = $this->plugins_list[$plugin_name];
117
                foreach ($functions as $function) {
118
                    if (method_exists($plugin, $function)) {
119
                        call_user_func([$plugin, $function], $function_args);
120
                    }
121
                }
122
            }
123
        }
124
    }
125
126
    /**
127
     * Execute a plugin's action
128
     *
129
     * @param $plugin_name - The plugin name.
130
     * @param $action - action that will be executed.
131
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
132
    public function do_action($plugin_name, $action)
1 ignored issue
show
Coding Style introduced by
Public method name "PluginManager::do_action" is not in camel caps format
Loading history...
133
    {
134
        if (!isset($this->plugins_list[$plugin_name])) {
135
            // Show an error and stop the application
136
            $this->halt(sprintf($this->lang['strpluginnotfound'] . "\t\n", $plugin_name));
137
        }
138
        $plugin = $this->plugins_list[$plugin_name];
139
140
        // Check if the plugin's method exists and if this method is an declared action.
141
        if (method_exists($plugin, $action) and in_array($action, $this->actions[$plugin_name])) {
142
            call_user_func([$plugin, $action]);
143
        } else {
144
            // Show an error and stop the application
145
            $this->halt(sprintf($this->lang['stractionnotfound'] . "\t\n", $action, $plugin_name));
146
        }
147
    }
148
}
149