I learned something new today

Plugin system for Pritlog

A plugin system enables external users or members of the community to add functionality to the software without even touching a single line from the main software program.

Pritlog needed a plugin system and that too a very efficient and lightweight one. I found the plugin system called "Simple Hooks Plugin" from Phpclass website. The link is http://phpclasses.betablue.net/browse/package/4497.html.

I will mention some details of how it is implemented.

Firstly, the main program needs to initialize the plugins. It can be done like this:

//include Simple Hooks Plugin Class
$SHP = new SHP();

//set hook to which plugin developers can assign functions $SHP->developer_set_hook('hook-test');


Second, the main program needs to define what is called hooks. These are basically points in the main program where the developer wants external plugins to attach and execute. This code can look like this:
<html>
<head>
<title>Testing the plugins</title>
</head>
<body>
<?php
//Initialize the plugin
include "SHP.class.php";
$SHP = new SHP();
$SHP->developer_set_hook('hook-test');
echo "<h1>Simple Hooks Plugin</h1>";
$SHP->execute_hooks('hook-test');
?>
</body>
</html>

In the above piece of code, the developer has provided a hook called hook-test where external plugins can attach and execute. Now let us look at a simple plugin.

//set plugin id as file name of plugin
$plugin_id = basename(__FILE__);

//some plugin data
$data['name'] = "Name of the plugin";
$data['author'] = "Authors Name";
$data['desc']   = "Brief description of the plugin";
$data['url'] = "Plugin/Author website";

//register the plugin
register_plugin($plugin_id, $data);

//plugin function
function test_function() {
echo 'echoed from plugin';
}

//add hook, where to execute a function
add_hook($plugin_id, 'hook-test','test_function'); 
echo "Plugin 1 LOADED!";   //code to execute when loading plugin

When the main program runs, this plugin is picked up and the final output from the program would be:

Plugin 1 LOADED!
<h1>Simple Hooks Plugin</h1>
echoed from plugin

See how the code from the plugin got executed in the main program even without touching a single line of code from the main program.

In Pritlog, I modified the plugin system so that the user can enable or disable a plugin at will.

This was a very interesting and exciting experiment for me.



Author: Prit -  Date: 29 Apr 2009 13:18
Tags: pritlog,web -  Visits: 215 -  No Comments




Comments:

No comments posted yet!


Add Comment

Comment Form

 (Required)

 (Optional, will not be published)

 (Optional, format: http(s)://website.com)



 (ly51juvl)