Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PluginManager.php « classes « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2afb9242e1a4f39d96ea2c3b4349307ec1a955dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php

/**
 * PHPPgAdmin v6.0.0-RC7
 */

namespace PHPPgAdmin;

/**
 * @file
 * A class that implements the plugin's system
 */

/**
 * A class that implements the plugin's system.
 *
 * @package PHPPgAdmin
 */
class PluginManager
{
    use \PHPPgAdmin\Traits\HelperTrait;
    /**
     * Attributes.
     */
    private $_plugins_list    = [];
    private $_available_hooks = [
        'head',
        'toplinks',
        'tabs',
        'trail',
        'navlinks',
        'actionbuttons',
        'tree',
        'logout',
    ];
    private $_actions = [];
    private $_hooks   = [];

    /**
     * Register the plugins.
     *
     * @internal param $this ->language - Language that have been used
     *
     * @throws \Interop\Container\Exception\ContainerException
     * @throws \Slim\Exception\ContainerValueNotFoundException
     */
    public function __construct(\Slim\Container $container)
    {
        $this->language  = $container->has('language') ? $container->get('language') : 'english';
        $this->lang      = $container->get('lang');
        $this->conf      = $container->get('conf');
        $this->container = $container;
        if (!isset($this->conf['plugins'])) {
            return;
        }

        // Get the activated plugins
        $plugins = $this->conf['plugins'];

        foreach ($plugins as $activated_plugin) {
            $plugin_file = \BASE_PATH.'/src/plugins/'.$activated_plugin.'/plugin.php';

            // Verify is the activated plugin exists
            if (file_exists($plugin_file)) {
                include_once $plugin_file;

                try {
                    $plugin = new $activated_plugin($this->language);
                    $this->addPlugin($plugin);
                } catch (\Exception $e) {
                    continue;
                }
            } else {
                $this->halt(sprintf($this->lang['strpluginnotfound']."\t\n", $activated_plugin));
            }
        }
    }

    /**
     * Add a plugin in the list of plugins to manage.
     *
     * @param mixed $plugin - Instance from plugin
     */
    public function addPlugin($plugin)
    {
        //The $plugin_name is the identification of the plugin.
        //Example: PluginExample is the identification for PluginExample
        //It will be used to get a specific plugin from the _plugins_list.
        $plugin_name                       = $plugin->get_name();
        $this->_plugins_list[$plugin_name] = $plugin;

        //Register the plugin's functions
        $_hooks = $plugin->get__hooks();
        foreach ($_hooks as $hook => $functions) {
            if (!in_array($hook, $this->_available_hooks, true)) {
                $this->halt(sprintf($this->lang['strhooknotfound']."\t\n", $hook));
            }
            $this->_hooks[$hook][$plugin_name] = $functions;
        }

        //Register the plugin's _actions
        $_actions                     = $plugin->get__actions();
        $this->_actions[$plugin_name] = $_actions;
    }

    public function getPlugin($plugin)
    {
        if (isset($this->_plugins_list[$plugin])) {
            return $this->_plugins_list[$plugin];
        }

        return null;
    }

    /**
     * Execute the plugins hook functions when needed.
     *
     * @param string $hook          - The place where the function will be called
     * @param array  $function_args - An array reference with arguments to give to called function
     */
    public function doHook($hook, &$function_args)
    {
        //$this->prtrace('_hooks', $this->_hooks, $function_args);
        if (isset($this->_hooks[$hook])) {
            foreach ($this->_hooks[$hook] as $plugin_name => $functions) {
                $plugin = $this->_plugins_list[$plugin_name];
                foreach ($functions as $function) {
                    if (method_exists($plugin, $function)) {
                        call_user_func([$plugin, $function], $function_args);
                    }
                }
            }
        }
    }

    /**
     * Execute a plugin's action.
     *
     * @param string $plugin_name - The plugin name
     * @param string $action      - action that will be executed
     */
    public function do_action($plugin_name, $action)
    {
        if (!isset($this->_plugins_list[$plugin_name])) {
            // Show an error and stop the application
            $this->halt(sprintf($this->lang['strpluginnotfound']."\t\n", $plugin_name));
        }
        $plugin = $this->_plugins_list[$plugin_name];

        // Check if the plugin's method exists and if this method is an declared action.
        if (method_exists($plugin, $action) and in_array($action, $this->_actions[$plugin_name], true)) {
            call_user_func([$plugin, $action]);
        } else {
            // Show an error and stop the application
            $this->halt(sprintf($this->lang['stractionnotfound']."\t\n", $action, $plugin_name));
        }
    }
}