ok been looking into creating some type of plugin system class. this is what i got so far.
its working as it should. it does need alot of improvements to it. such as added html/css
option to turn on and off at will, once a database/site is done itll need probably calls to mysql/mysqli etc.
like i said it can be much approved on this is just the begining.
im adding it here in hopes that some of you will find it interesting enough to maybe add/imput some of your
own idea to make it grow.
ok lets start.
plugins/plugin.php
plugins/sample_plugin/functions/plugin_funcs.php
plugins/sample_plugin/sample_plugin.php
index.php (or whatever you choose)
Full Script:
its working as it should. it does need alot of improvements to it. such as added html/css
option to turn on and off at will, once a database/site is done itll need probably calls to mysql/mysqli etc.
like i said it can be much approved on this is just the begining.
im adding it here in hopes that some of you will find it interesting enough to maybe add/imput some of your
own idea to make it grow.
ok lets start.
plugins/plugin.php
PHP Code:
<?php
class plugin {
private static $listeners = array(), $num_args, $args, $hook_name;
protected function __construct() {} /** Thou shalt not construct that which is unconstructable! */
protected function __clone() {} /** Me not like clones! Me smash clones! */
public function __wakeup() { throw new Exception("Cannot unserialize singleton"); }
/** Create an entry point for plugins */
public static function hook() {
self::$num_args = func_num_args(); /** Get the number of arguments passed to the function */
self::$args = func_get_args(); /** Get an array comprising the function's argument list */
if(!(self::$num_args >= 2)) { trigger_error("hook() function requires 2 or more arguments", E_USER_ERROR); }
self::$hook_name = array_shift(self::$args); /** Hook name should always be first argument */
if(!isset(self::$listeners[self::$hook_name])) { return; } /** No plugins have registered this hook */
foreach(self::$listeners[self::$hook_name] as $func) { self::$args = $func(self::$args); }
return self::$args;
}
/** Attach a function to a hook */
public static function add_listener() {
self::$num_args = func_num_args(); /** Get the number of arguments passed to the function */
self::$args = func_get_args(); /** Get an array comprising the function's argument list */
if(!(self::$num_args == 2)) { trigger_error("add_listener() function requires 2 arguments", E_USER_ERROR); }
self::$listeners[self::$args[0]][] = self::$args[1];
}
/** Display a plugin app */
public static function display() {
self::$num_args = func_num_args(); /** Get the number of arguments passed to the function */
self::$args = func_get_args(); /** Get an array comprising the function's argument list */
if(!(self::$num_args == 1)) { trigger_error("display() function requires 1 argument", E_USER_ERROR); }
$plugin = self::$args[0];
return $plugin();
}
}
?>
plugins/sample_plugin/functions/plugin_funcs.php
PHP Code:
<?php
/** Sample Plugin Func 1 **/
/** we create a listener for our plugins custom functions so our class
can successfully know what functions should be used with the plugin */
plugin::add_listener('change_numbs', 'change_numbs'); /** plugin::add_listener(hook_name, func_name) */
/** custom function to be used within the plugin */
function change_numbs($args){
/** Arguments are passed back as an array to our custom function */
$args[0] = $args[0] + 10;
$args[1] = $args[1] + 16;
return array($args[0], $args[1]);
}
/** Sample Plugin Func 2 **/
/** we create a listener for our plugins custom functions so our class
can successfully know what functions should be used with the plugin */
plugin::add_listener('str_rep', 'string_replace'); /** plugin::add_listener(hook_name, func_name) */
/** custom function to be used within the plugin */
function string_replace($args){
/** Arguments are passed back as an array to our custom function */
return str_replace('sample', 'PLUGIN', $args[0]);
}
/** Sample Plugin Func 3 **/
/** we create a listener for our plugins custom functions so our class
can successfully know what functions should be used with the plugin */
plugin::add_listener('echo_description', 'echo_func'); /** plugin::add_listener(hook_name, func_name) */
/** custom function to be used within the plugin */
function echo_func(){
echo "<strong>the sample plugin function change_numbs()<br/>will change original numbers, str_rep() will<br/>replace 'sample' with 'PLUGIN' this is basically just an<br/>example plugin to show how connecting functions<br/> and setting up plugin structure is accomplished</strong>";
}
?>
plugins/sample_plugin/sample_plugin.php
PHP Code:
<?php
/** Sample Plugin **/
/** we wrapped the main plugin within a function so our class
can find it and display correctly, we also name the plugin's main
function the same as the plugin folder so the user can successfully
know the name of the plugin without digging into the code */
function sample_plugin(){
$a = 4;
$b = 9;
/** we hook our custom function to our plugin and send the custom
function some arguments if that function specifically needs them */
list($a, $b) = plugin::hook('change_numbs', $a, $b); /** plugin::hook(func_name, arguments) */
$str = 'This is my sample application<br/>'."\r\n";
$str .= $a.' + '.$b.' = '.($a+$b).'<br/>'."\r\n";
$str .= $a.' * '.$b.' = '.($a*$b)."\r\n";
/** we hook our custom function to our plugin and send the custom
function some arguments if that function specifically needs them */
$str = plugin::hook('str_rep', $str); /** plugin::hook(func_name, arguments) */
echo $str, "<br/><br/>\r\n";
/** we hook our custom function to our plugin and give it a null argument
since the custom function we are calling doesnt require any arguments */
plugin::hook('echo_description', null); /** plugin::hook(func_name, arguments) */
}
?>
index.php (or whatever you choose)
PHP Code:
<?php
/** get all plugin files in order:
main class, plugins funcs, main plugin(func) */
if(file_exists('plugins/plugin.php')) { include('plugins/plugin.php'); } else { die('Cannot Find Plugin File'); }
foreach(glob('plugins/*/functions/*.php') as $plugin_func_file) { include($plugin_func_file); }
foreach(glob('plugins/*/*.php') as $plugin_file) { include($plugin_file); }
/** call to display plugin */
plugin::display('sample_plugin');
?>
Comment