tags:

views:

53

answers:

3

I have been asked to do a project where the client can create PHP plugins and add them to the website… same concept as WordPress plugins.

What is the best way to make a software that supports that? I'm not asking for the solution, I'm asking for the concept or structure.

Thanks.

A: 

First of all, you have to determine how do you want your plugins integrated. If you want something like Wordpress plugins, which are just plain functions most of the times, then it's extremely easy. It's like adding new code to your application.

Well, basically, your application should be able to build a list of plugins available, and to determine which ones are enabled. The list can be built by browsing the files, a config file, or whatever. Enabled plugins can be stored in the database for example.

Then, you just do an interface where the user can enable each available plugin. When enabling it, the web application would write information about it in the plugins database, specifying that it's enabled. Then, the application calls a specific method of the plugin class (really, OOP is mandatory in correct plugin development), like Install(). To avoid exceptions when plugins have no such method, all plugin classes should inherit a base class written in the main web application, which does contain a definition for that method. That method has the sole purpose to perform the initialization of the plugin (create tables to the database, populate statistics, etc).

Also, at each request, your web application should include all the files of the plugins that are specified as enabled. Then use them however you want. Call their methods from templates for example. If you want to write plugins that are transparent to the template developer, then you could do just like in wordpress. There, the plugin at initialization (for example when it's file is included) writes specific data into arrays of actions. It can write what function should be called, with what parameters, and when to be called. You can even make something like event triggers in your main web application, to enable customization. Before drawing something, call a function called something like do_action("before_output_of_something"), and after output is ended, call the function do_action("after_output_of_something"). The string parameter would be a key in an array of triggers, that points to an array of actions (event handlers). To add event handlers call a function like this add_action("before_output_of_something", $object, "method_name", array("parameters")). do_action method passes the array of actions, and uses call_user_func() PHP function to call event handlers.

There is much more to this topic. All depends on what exactly you want to make. What I wrote here is just a lousy attempt to express some of the possibilities. What you should do is find a good book, maybe about just this topic.

Alexander
A: 

There are multiple options. Most common is the hooks concept with simple plugin functions, where you just have to include() a plugin from a central configuration script. Each plugin has to register itself, so usually there is a plugin registration method. But it can also just look like this:

$app_plugin["add_title"][] = "do_whatever";
function do_whatever($request, &$page) {...}

And in your main application you just define a couple of plugin calls, wherever you see the need for it. You would use a wrapper function to invoke all plugins, but basically you look for the hook name and invoke all registered plugins:

foreach ($app_plugin["add_title"] as $func) {
    $output .= $func($_REQUEST, $page);

Each named hook can have a different set of parameters. Typically you would want to get additional content, but sometimes plugins just modify existing variables, or influence the application flow.

The more hooks you have, the more diverse extensions are possible. And it's not necessary to use functions only. Of course this scheme also works with objects (they just need to be instantiated first.)

mario
+1  A: 

I would solve this by using the event-dispatcher pattern. You define certain points (events) in your main application. Plugins can register for this events and will be executed, when the related event is triggered.

faileN
For instance [Symfony Components Event Dispatcher](http://components.symfony-project.org/event-dispatcher/) or [PEAR Event Dispatcher](http://pear.php.net/package/Event_Dispatcher/redirected)
Gordon
Yeah, I think there lots of solutions for that. Since an Event-Dispatcher isn't a big deal, I would code it myself to fit my needs
faileN