开发者

Is there any php framework to build a web app with plugins feature?

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开发者_Go百科 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?


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();
            }
        }
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜