Jump to content

how to create a plugin system


nuttycoder

Recommended Posts

Hi,

 

I'm after creating a CMS system that has support for plugins to make my code more modular I have looked all over Google and can't find any tutorials on the subject I assume I need to look at some port of hook mechanism but not to sure how to proceed.

 

Can anyone point me to any tutorials or resources on creating a custom plugin system? Or have any tips on how to get started..

 

Thanks

Link to comment
Share on other sites

This really depends on how You want to implement plugins/modules.

 

I have built two different type of module systems. They work pretty similar.

It's just one takes info from a db the other takes info from the script itself (allowing the owner to include the modules). These are filebased, and are included into the script

 

than ya have systems which keep the entire module in the db, I believe this will be eval'd later.

 

The module systems I have made, are just base engines (meaning they dont do nothing themselves except load in the various modules). The modules themselves dictate wut they display/do.

 

than you have a plugin system, which does involve setting up hooks and the like. where u have a standard api. which gives the base system more functionality.

 

 

Link to comment
Share on other sites

Well here's the thing.. In order to provide plugin support you need some sort of glue. That is, something that maps your plugins to your architecture. This is actually the hardest part of a plugin system.

 

I suggest you spend some time thinking about what you want your plugins to be able to do, and what they should not be able to do. Once you come up with this, we can start providing a bit more information.

Link to comment
Share on other sites

yeah some valid points.

 

I do need to research which way is better for me as I currently have a system with modules that are integrated into the system then to use a module you would put a tag in a page where you wanted the module to be similar to workdpress like if I wanted a blog I would put [blog] on a page.

 

You can see a demo of my system at http://www.surrectmedia.com/demo to get a better idea of what am using at the moment.

 

The problem with this system is it's slow and to add a new module there's lots of files to edit rather then being standalone like a module one would be.

Link to comment
Share on other sites

So it sounds like you just need a way to glue it together -- meaning, have 1 file which maps all of the actions/commands to the other files.

 

You can implement this as a proxy[1] (which I like)  .. or as a Flyweight[2], which could also be nice depending on your needs

 

1: http://en.wikipedia.org/wiki/Proxy_pattern

2: http://en.wikipedia.org/wiki/Flyweight_pattern

Link to comment
Share on other sites

Yes I do I would like to change the way the system works so I could create a standalone module have it in its own folder upload it and activate it like you do in wordpress making adding new modules easier.

 

at present for all modules I add all the needed files for the admin side to add/edit items and have the front end side in another folder update delete functions and javascript functions and then modily the htaccess file and index file so it's quite indepth.

Link to comment
Share on other sites

I've found an article on building a plugin architecture, from a pdf it's a little dated was released in 2006.

 

I have a really simple plugin that should replace the title on the main page ony I am getting this error:

 

<b>Warning</b>:  Missing argument 1 for date_append() in <b>E:\tutorials\xampp\htdocs\mod\plugins\datetitle.php</b> on line <b>4</b>

 

Here's the plugin file:

 

datetitle.php

<?php
add_filter('title', date_append);

function date_append($title)
{
return date('l, M j') . ': ' . $title;
}
?>

 

Here's the index file where the plugin should be run using apply_filters

 

<?php include_once('config.php'); ?>
<html>
<head>
<title><?php apply_filters('title', 'Main Page'); ?></title>
</head>
<body>
</body>
</html>

 

config file:

 

<?php
define(ABSPATH, dirname(__FILE__) . '/');

$plugins = array('datetitle');

include_once('plugins.php');

foreach ($plugins as $plugin) {
$plugin_file = ABSPATH . 'plugins/' . $plugin . '.php';
if ($plugin != '' && file_exists($plugin_file)) {
	include_once($plugin_file);
}
}

?>

 

and the pulgins.php file:

 

<?php

//------------------- Filters -------------------------------------

function add_filter($tag, $function_to_add, $priority = 10, $accept_args=1) {

global $filter_table;

if ( isset($filter_table[$tag][$priority]) ) {
	foreach($filter_table[$tag][$priority] as $filter) {
     	if ( $filter['function'] == $function_to_add ) {
     		return false;
		}
	}
}

$filter_table[$tag][$priority][] = 
	array('function'=>$function_to_add, 
			  'accept_args'=>$accept_args);

return true;

}

function remove_filter($tag,  $function_to_remove, $priority = 10) {

global $filter_table;
$toret = false;

if ( isset($filter_table[$tag][$priority]) ) {
	foreach($filter_table[$tag][$priority] as $filter) {
		if ( $filter['function'] != $function_to_remove ) {
			$new_function_list[] = $filter;
		}
		else {
			$toret = true;
		}
	}

	$filter_table[$tag][$priority] = $new_function_list;
}
return $toret;
}

//------------------- Actions -------------------------------------

function do_action($tag, $arg = '') {
global $filter_table;
$extra_args = array_slice(func_get_args(), 2);


$args = array_merge(array($arg), $extra_args);

if ( !isset($filter_table[$tag]) ) {
	return;
}
else {
	ksort($filter_table[$tag]);
}

foreach ($filter_table[$tag] as $priority => $functions) {
	if ( !is_null($functions) ) {
		foreach($functions as $function) {

			$func_name = $function['function'];
			$accept_args = $function['accepted_args'];

			if ( $accept_args == 1 ) {
				$the_args = array($arg);
			} elseif ( $accept_args > 1 ) {
				$the_args = array_slice($args, 0, $accept_args);
			} elseif ( $accept_args == 0 ) {
				$the_args = NULL;
			} else {
				$the_args = $args;
			}

			$str = call_user_func_array($func_name, $the_args);
		}
	}
}
}

//------------------- Apply Filters -------------------------------------

function apply_filters($tag, $string) {

global $filter_table;

$args = array_slice(func_get_args(), 2);

if ( !isset($filter_table[$tag]) ) {
	return $string;
}
else {
	ksort($filter_table[$tag]);
}

foreach ($filter_table[$tag] as $priority => $functions) {

	if ( !is_null($functions) ) {
		foreach($functions as $function) {

			$all_args = array_merge(array($string), $args);
			$func_name = $function['function'];
			$accept_args = $function['accepted_args'];

			if ( $accept_args == 1 )
				$the_args = array($string);
			elseif ( $accept_args > 1 )
				$the_args = array_slice($all_args,0, $accept_args);
			elseif ( $accept_args == 0 )
				$the_args = NULL;
			else
				$the_args = $all_args;

			$str = call_user_func_array($func_name, $the_args);

		}
	}
}
return $string;
}


?>

 

If anyone can spot what's going wrong that would be a huge help.

 

Thanks.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.