creating a plugin system class

Started on the old forum in February 2015 · view the original in the archive · old links redirect here

6 replies · 128 views · started 11 years ago · 1 min read

2 participants
Ghost Senior · 366 posts #1

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

<?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
/** 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
/** 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
/** 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');
?>

Full Script:

👍 1
Share
<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Attachments: plugins.zip
Ghost Senior · 366 posts #3

lol i think there is no hope for you 😉

index "calls" sample plugin to "display" sample plugin "hooks" (adds) custom functions related to "plugin" "add_listener" tells "hooks" these functions are to be associated with plugin (so listen for them)

this then in turn will show "3rd party" plugins (addons) in your site just by adding

plugin::display('sample_plugin');
<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ghost Senior · 366 posts #5

because it would be easier to implement other peoples mods of "your" script/site you could add extra functions to it so it could make calls to a "database" check if plugin is active. stick it in the database list of installed mods. you could basically make it so it does something like the plugins/mods do here at coding talk. the plugin could install itself with an install file (if making database tables etc) and all you would need to really do to get that mod working is add 1 line of code:

plugin::display('sample_plugin'); 

wherever you wanted it to be displayed on script.

then go to admin panel and turn it on/off etc. instead of messing about trying to get someone elses code to work. as long as they stick to the structure a plugin shoud be created under nothing can go wrong.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
kevk3v Senior · 332 posts #6

Lol... sorry man, I must be kind of dumb today, had too much beer. :/

Mobile chat, iphone chat, android chat, chat, rooms http://www.aiochat.com

Ghost Senior · 366 posts #7

lol no worries take alook again when less drunk 😁

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close