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

src/classes/PluginManager.php (1 issue)

1
<?php
2
/*
0 ignored issues
show
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
 */
10
class PluginManager
11
{
12
    use \PHPPgAdmin\HelperTrait;
13
    /**
14
     * Attributes
15
     */
16
    private $plugins_list    = [];
17
    private $available_hooks = [
18
        'head',
19
        'toplinks',
20
        'tabs',
21
        'trail',
22
        'navlinks',
23
        'actionbuttons',
24
        'tree',
25
        'logout',
26
    ];
27
    private $actions = [];
28
    private $hooks   = [];
29
30
    /**
31
     * Register the plugins
32
     *
33
     * @param \Slim\Container $container
34
     * @internal param $this ->language - Language that have been used.
35
     * @throws \Interop\Container\Exception\ContainerException
36
     * @throws \Slim\Exception\ContainerValueNotFoundException
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) {
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
     */
74
    public function add_plugin($plugin)
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)
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
     */
112
    public function do_hook($hook, &$function_args)
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
     */
132
    public function do_action($plugin_name, $action)
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