views:

43

answers:

2

I am building a web app for keywords research. Mainly what it does is it takes a keyword and it uses it to make curl requests and it parses numbers which then are stored in a mysql table. The curl request would be for example for retrieving yahoo number of results, number of diggs for that keyword, etc.

So I was thinking to code it in some way to use plugins (one for yahoo, other for digg, etc).

Is there any php framework that will help me in doing this?

+1  A: 

Frameworks don't handle things like that. You need to code it in plug-in way.

Following scenario will allow you to code in plug-in fashion:

Lets say we have directory called plugins in the root of the site and a table in the database called plugins with following structure (id, name, enabled, path)

Now you need to create an interface for your plug-ins. This way all plug-ins will have same basic structure.

/**
 * FILE: /plugins/PluginInterface.php
 *
 * Sample Interface
 */
interface iPlugin{

    /**
     * Tests if plug-in can be executed
     */
    function test();

    /**
     * Prepared plug-in for execution
     */
    function prepare();

    /**
     * Executes plug-in logic and returns count of somethings
     */
    function execute();
}


/**
 * FILE: /plugins/PluginExample.php
 *
 * Sample Plug-in
 */
class PluginExample implements iPlugin{

    public function execute() {

    }

    public function prepare() {

    }

    public function test() {

    }
}

Now you need to insert a record for the PluginExample in the database.

INSERT INTO plugins (id, name, enabled, path) VALUES (1, 'Example', 1, 'PluginExample.php')

And lastly you need somekind of controller that loads all the enabled plugins from the database (get's path) and the creates objects and executes them. Like so

function loadAndExecutePlugins() {
    $query = "select * from plugins where enabled = 1";
    $plugins = 'array of objects from query 1';

    if ($plugins) {
        foreach ($plugins as $plugin) {
            //
            $class = $plug->path;
            include_once "/plugins/$class";

            //  Class is using interface, so you know what methods to call
            $plug = new $class();
            if ($plug->test()) {
                $plug->execute();
            }
        }
    }
}
Alex
Thanks, i have found this answer useful too http://stackoverflow.com/questions/42/best-way-to-allow-plugins-for-a-php-application, but your code is easier to understand for me.
jarkam
A: 

PHP Fat-Free Framework already has a Yahoo plugin that you can use. Although a Digg plugin isn't there, it should be easy to pattern it after the Yahoo plugin. Take a look at the code.

stillstanding