From 7924d311d0262cee9b0eb0391e687a2e5332f05c Mon Sep 17 00:00:00 2001 From: Heikki Hokkanen Date: Sat, 10 Jan 2009 11:49:34 +0200 Subject: [PATCH] Started working on a plugin framework. Added a base class VGPlugin that all plugins should inherit. --- inc/plugins.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/hello/main.php | 20 ++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 inc/plugins.php create mode 100644 plugins/hello/main.php diff --git a/inc/plugins.php b/inc/plugins.php new file mode 100644 index 0000000..69f259b --- /dev/null +++ b/inc/plugins.php @@ -0,0 +1,55 @@ + plugin object +$plugin_hooks = array(); + +function call_hooks($type) { + global $plugin_hooks; + foreach ($plugin_hooks as $type => $classes) { + foreach ($classes as $class) { + $class->hook($type); + } + } +} + +/** + * Base class plugins should extend. This defines the public, hopefully + * somewhat static API plugins should be able to rely on. + */ +class VGPlugin +{ + function __construct() { + + } + + /** + * Called when a registered action is triggered. + */ + function action($action) {} + + /** + * Called when a registered hook is triggered. + */ + function hook($type) {} + + /** + * Can be used to output xhtml. + */ + function output($xhtml) { + echo($xhtml); + } + + /** + * Registers the given action for this plugin. + */ + function register_action($action) { + global $plugin_actions; + $plugin_actions[$action] = $this; + } + + function register_hook($type) { + global $plugin_hooks; + $plugin_hooks[$type][] = $this; + } +} + diff --git a/plugins/hello/main.php b/plugins/hello/main.php new file mode 100644 index 0000000..621c1f9 --- /dev/null +++ b/plugins/hello/main.php @@ -0,0 +1,20 @@ +register_action('hello'); + $this->register_hook('pagenav'); + } + + function action($action) { + $this->output("Hello world!"); + } + + function hook($type) { + if ($type == 'pagenav') { + $this->output(" | 'hello')) ."\">Hello "); + } + } +} + -- 2.11.4.GIT