summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonardo Sapiras2011-12-05 20:44:01 +0000
committerJehan-Guillaume (ioguix) de Rorthais2012-08-22 10:23:50 +0000
commit3e282e6b49c9fb06d15a74bd83a5df2d5d63b3de (patch)
treedca53abbde753ad6168e08a39810a9397e379ae8
parenta524be779bcb7881dada321e4f1e8c1088ba1cc2 (diff)
Add skeleton of the plugin architecture
By Leonardo Sapiras, reviewed, integrated, commited by ioguix.
-rw-r--r--classes/Plugin.php36
-rw-r--r--classes/PluginManager.php114
-rw-r--r--conf/config.inc.php-dist12
-rw-r--r--lang/english.php5
-rw-r--r--libraries/lib.inc.php4
-rw-r--r--plugin.php5
6 files changed, 175 insertions, 1 deletions
diff --git a/classes/Plugin.php b/classes/Plugin.php
new file mode 100644
index 00000000..e05ad270
--- /dev/null
+++ b/classes/Plugin.php
@@ -0,0 +1,36 @@
+<?php
+abstract class Plugin {
+
+ /**
+ * Constructor
+ * Register the plugin's functions in hooks of PPA.
+ * @param $language Current phpPgAdmin language.
+ */
+ function __construct($language) {
+ // Set the plugin's language
+ $plugin_directory = "plugins/". $this->get_name();
+ require_once("{$plugin_directory}/lang/recoded/english.php");
+ if (file_exists("{$plugin_directory}/lang/recoded/{$language}.php")) {
+ include_once("{$plugin_directory}/lang/recoded/{$language}.php");
+ }
+ $this->lang = $plugin_lang;
+
+ if (file_exists("{$plugin_directory}/conf/config.inc.php")) {
+ include_once("{$plugin_directory}/conf/config.inc.php");
+ $this->conf = $plugin_conf;
+ }
+ }
+
+ abstract function get_hooks();
+
+ abstract function get_actions();
+
+ /**
+ * Get the plugin name, that will be used as identification
+ * @return $name
+ */
+ function get_name() {
+ return $this->name;
+ }
+}
+?>
diff --git a/classes/PluginManager.php b/classes/PluginManager.php
new file mode 100644
index 00000000..c4279a02
--- /dev/null
+++ b/classes/PluginManager.php
@@ -0,0 +1,114 @@
+<?php
+
+/**
+ * A class that implements the plugin's system
+ */
+
+class PluginManager {
+
+ /**
+ * Attributes
+ */
+ private $plugins_list = array();
+ private $available_hooks = array(/* wip, will be added in next commits */);
+ private $actions = array();
+ private $hooks = array();
+
+ /**
+ * Register the plugins
+ * @param $language - Language that have been used.
+ */
+ function __construct($language) {
+ global $conf, $lang;
+
+ // Get the activated plugins
+ $plugins = $conf['plugins'];
+
+ foreach ($plugins as $activated_plugin) {
+ $plugin_file = './plugins/'.$activated_plugin.'/plugin.php';
+
+ // Verify is the activated plugin exists
+ if (file_exists($plugin_file)) {
+ include_once($plugin_file);
+ $plugin = new $activated_plugin($language);
+ $this->add_plugin($plugin);
+ } else {
+ printf($lang['strpluginnotfound']."\t\n", $activated_plugin);
+ exit;
+ }
+ }
+ }
+
+ /**
+ * Add a plugin in the list of plugins to manage
+ * @param $plugin - Instance from plugin
+ */
+ function add_plugin($plugin) {
+ global $lang;
+
+ //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)) {
+ printf($lang['strhooknotfound']."\t\n", $hook);
+ exit;
+ }
+ $this->hooks[$hook][$plugin_name] = $functions;
+ }
+
+ //Register the plugin's actions
+ $actions = $plugin->get_actions();
+ $this->actions[$plugin_name] = $actions;
+ }
+
+ /**
+ * Execute the plugins hook functions when needed.
+ * @param $hook - The place where the function will be called
+ * @param $function_args - An array reference with arguments to give to called function
+ */
+ function do_hook($hook, &$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(array($plugin, $function), $function_args);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Execute a plugin's action
+ * @param $plugin_name - The plugin name.
+ * @param $action - action that will be executed.
+ */
+ function do_action($plugin_name, $action) {
+ global $lang;
+
+ if (!isset($this->plugins_list[$plugin_name])) {
+ // Show an error and stop the application
+ printf($lang['strpluginnotfound']."\t\n", $name);
+ exit;
+ }
+ $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])) {
+ call_user_func(array($plugin, $action));
+ }
+ else {
+ // Show an error and stop the application
+ printf($lang['stractionnotfound']."\t\n", $action, $plugin_name);
+ exit;
+ }
+ }
+}
+?>
diff --git a/conf/config.inc.php-dist b/conf/config.inc.php-dist
index 2c70d315..ed553b1c 100644
--- a/conf/config.inc.php-dist
+++ b/conf/config.inc.php-dist
@@ -154,7 +154,17 @@
// Configuration for ajax scripts
// Time in seconds. If set to 0, refreshing data using ajax will be disabled (locks and activity pages)
$conf['ajax_refresh'] = 3;
-
+
+ /** Plugins management
+ * Add plugin names to the following array to activate them
+ * Example:
+ * $conf['plugins'] = array(
+ * 'Example',
+ * 'Slony'
+ * );
+ */
+ $conf['plugins'] = array();
+
/*****************************************
* Don't modify anything below this line *
*****************************************/
diff --git a/lang/english.php b/lang/english.php
index 2434b0b3..62f87371 100644
--- a/lang/english.php
+++ b/lang/english.php
@@ -909,4 +909,9 @@
$lang['strftstabdicts'] = 'Dictionaries';
$lang['strftstabparsers'] = 'Parsers';
$lang['strftscantparsercopy'] = 'Can\'t specify both parser and template during text search configuration creation.';
+
+ //Plugins
+ $lang['strpluginnotfound'] = 'Error: plugin \'%s\' not found. Check if this plugin exists in the plugins/ directory, or if this plugins has a plugin.php file. Plugin\'s names are case sensitive';
+ $lang['stractionnotfound'] = 'Error: action \'%s\' not found in the \'%s\' plugin, or it was not specified as an action.';
+ $lang['strhooknotfound'] = 'Error: hook \'%s\' is not avaliable.';
?>
diff --git a/libraries/lib.inc.php b/libraries/lib.inc.php
index 10772e99..05b41eea 100644
--- a/libraries/lib.inc.php
+++ b/libraries/lib.inc.php
@@ -226,4 +226,8 @@
return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
}
}
+
+ // Manage the plugins
+ require_once('./classes/PluginManager.php');
+ $plugin_manager = new PluginManager($_language);
?>
diff --git a/plugin.php b/plugin.php
new file mode 100644
index 00000000..0b3e5921
--- /dev/null
+++ b/plugin.php
@@ -0,0 +1,5 @@
+<?php
+require_once('./libraries/lib.inc.php');
+
+$plugin_manager->do_action($_REQUEST['plugin'], $_REQUEST['action']);
+?>