As part of my framework's development, I wanted to be able to make core modules fixed across many sites, but equally I wanted to be able to tweak and modify certain bits easily.
What I came up with, is a simple 1 include script plugin system that can be dropped into any existing PHP site and immediately enable plugin support anywhere the developer chooses. So called Hooks are points set within the code that can be attached to to run extra code.
pluginmanager.inc
<?
{
private static $pluginManager;
private $plugins;
private $hooks;
function __construct ()
{
$this->plugins = array ();
$this->hooks = array ();
{
if ($PlugFolder != '.' && $PlugFolder != '..')
{
{
{
if ($Plug != '.' && $Plug != '..' && strtolower (substr ($Plug, strlen ($Plug) - 4)) == '.php')
{
$Code = file_get_contents ($plugin_dir . $PlugFolder . '/' . $Plug);
$Plug = strtolower ($Plug);
$Plug = substr ($Plug , 0, strlen ($Plug) - 4);
}
}
}
}
}
{
if (is_null (self::$pluginManager))
{
self::$pluginManager = new pluginManager ($config);
}
return self::$pluginManager;
}
private function addHook ($hook, $plugin, $code)
{
if (!isset ($this->hooks[$hook][$plugin]))
}
{
$hook = strtolower ($hook);
$Code = '';
if (isset ($this->hooks[$hook]))
{
foreach ($this->hooks[$hook] as $plugin => $hook)
$Code .= $hook;
}
return $Code;
}
}
?>
With this saved in your site code folder, you can include it and start it up by doing the following:
index.php
<?
Thats it then, the plugin manager is running in your site. Using the power of Eval, you can put plugins anywhere within your site setting known 'hooks' for example
index.php
<?
include ('pluginmanager.inc');
pluginManager::getPluginManager ();
eval (PluginManager::getPluginManager ()->pluginHooks ('Start'));
This creates a hook called Start, and runs any plugins that are attached to it - which at this moment we don't have any of.
Lets Create a Plugin
Relative to the pluginmanager.inc file, create a folder plugins. Within this, we can now create plugins - a single plugin is a group of text files, called by the plugin hook that they are to hook onto and filled with the PHP code to be run.
So, for example, if we create a folder, 'test' and within this 2 text files, one called Start.php and one called End.php (to match the Hooks we created earlier). We'll now create a simple plugin to monitor the time taken to load the entire page.
Start.php
function getMicroTime ()
{
list ($usec, $sec) = explode (" ",microtime ());
return ((float)$usec + (float)$sec);
}
$plugin_loadtime_startTime = getMicroTime ();
End.php
print "Page Generated in " . round (getMicroTime () - $plugin_loadtime_startTime, 2) . " seconds. ";
This simple block of code should enable you to add a Plugin System to your own sites relatively easily.