tags:

views:

89

answers:

2

Hi all,

I have been writing some code that creates a generic blog.

Its features are simple, create a post, edit and delete the post and allow comments.

I am trying to write it so that it is very easy to write plugins for, however Im not sure on the best approach.
Some of my ideas:

  1. Have the plugin author write a short script called e.g "config" in which they have an array that has the application(e.g frontend, admin etc), the module(e.g blog, profile etc) and the action(s)(e.g create, edit etc) that thier plugin affects, then include the plugin files when the correct action is run.

    //example array in config.php:
    array(
      'application' => 'admin',
      'module'      => 'blog',
      'action'      => array('create','edit')
    );
    
  2. add strings into the views code such as "{form-extras}" and have the plugin author say which string there code will replace. Then use str_replace to replace the {xxx} with the plugin code.

    #example code in blog_form.php
    <input type="text" name="blog_title" />
    <input type="text" name="blog_text" />
    {form-extras}
    
    
    #example code in plugins config.php
    array(
      'replace' => array('form-extras')
    );
    

Both of these ideas are pretty rubbish and very limited in their use but I am struggling to come up with an better ideas.

I'm not sure how much info about my code people need but the basic dir structure is simple, below is an example:

apps //applications
  frontend  //app name
    modules
      blog
        views
          index.php  //list blogs
          new.php //create new blog post
        actions.class.php
  admin
    modules
      blog
        views
          index.php  //list blogs
          new.php //create new blog post
        actions.class.php
lib //library of classes like database class
plugins //where plugins will hopefully be installed
web //where the site runs e.g index.php, all the css and js

The Question

Does anyone know of any tutorials/articles on making code easy to write plugins for, or does anyone have any tested methods that I could apply?

Regards

Luke

+1  A: 

What you are looking for is an example and/or information about a plugin architecture. Using this term and Google will reveal many resources.

Examine how this is achieved in existing established applications such as WordPress, Drupal or Joomla.

Consider also this existing question: Plugin Architecture in PHP

Jon Cram
Hi Jon, I had a look at that other topic and also on google, I found a few forum posts(thanks for the correct name"plugin architecture") but no good articles for php, Im going to delv into drupal at some point to have a look how they do it, but you don't perchance know of a good article specific to php do you? Cheers Luke
Luke
+1  A: 

Before you start thinking about how to make the plugins system, you have to define what exactly a plugin is for you application, what the plugins can do and what data the plugins will be able to access (for example, posts table but not users table).

Then, take a look at Drupal, i guess it's module system based on hooks is really powerfull and 'simple' to use for developers.

Basically, the idea is that when a module or plugin is installed, everythings you do on your 'core' code, search if there is some module/plugins hooked to that action.

Example:

//your code
$modules_enabled = array('foo', 'bar');
//example action, lets say insert a new blog post. Obviously, nothings prevent you
//to do that in OOP style (i'd never really understood why drupal is almost all procedural).
function create_new_post($modules_enabled, $other_args){
    //looks if some modules need to be called before create the blog post
    foreach($modules_enables AS $module){
        if(function_exists("after_create_new_post_$module")){
            $func = "before_create_new_post_$module";
            $func($params);
        }
    }
    //do your stuff here
    //looks if some modules need to be called after the post is been created
    foreach($modules_enables AS $module){
        if(function_exists("after_create_new_post_$module")){
            $func = "after_create_new_post_$module";
            $func($params);
        }
    }
}

//the module file should look like $hooks_name . $module_name:
function after_create_new_post_foo($args){
    //do your job
}

This is a very very sintetic example (and doesnt work!), but should give you the idea.

The only problem here comes with the args that you pass to each hook_function, that have to be documented really good, however the documentation is important whatever path you'll choose.

Some reference: Drupal hooks, hook_insert

DaNieL
Thanks for the example code Daniel, would it be a viable idea to have some kind of call like you have in your foreach loops in the base actions class construct and destruct functions? That way they would always get called at the very start and end of any code in the class? Thanks for taking the time to write the above example with so much documentation, its very clear.
Luke
My code is just an example, is not the real way how drupla's hook works (you really have to give at look at them in drupal documentation).Some hooks just 'override' the base functions, others, works as a 'check', if the hook function return true the base function is executed, otherwise not. This is just a guideline, all depends on your specific needs.
DaNieL
Thanks Daniel, helpful references and comments, I have been digging through the Drupal code and its been very useful. Cheers
Luke