Development Plugins for a PHP Script

Discussion in 'Software' started by OneSeventeen, 28 Jan 2008.

  1. OneSeventeen

    OneSeventeen Oooh Shiny!

    Joined:
    3 Apr 2002
    Posts:
    3,454
    Likes Received:
    2
    I'm writing a framework-ish PHP script to help manage all my applications, and I want the core app to be flexible via the use of plugins.

    The best thing I could come up with in order to make the system as easy to use for developers as possible is to pull all the plugin files in and grab their mappings via a simple array.

    This script pulls the files in:
    PHP:
        function loadPlugins() {
            if(
    $pluginDir opendir(SHOEHORN_PLUGIN_FOLDER)) {
                
    $plugins = array();
                while(
    false !== ($file readdir($pluginDir))) {
                    if(!
    is_dir($file)) {
                        if(
    substr($file, -11)==".plugin.php") {
                            
    $plugins[] = $file;
                            
    $functionMaps = array();
                            require_once(
    SHOEHORN_PLUGIN_FOLDER "/" $file);
                            if(
    is_array($functionMaps) && count($functionMaps)>0) {
                                foreach(
    $functionMaps as $k=>$v) {
                                    
    $this->functionMaps[$k][] = $v;
                                }
                            }
                        }
                    }
                }
            }
        }
    Then this function is there to let the plugins "hook" into other functions:
    PHP:
        function hook($func$loc$args) {
            if(isset(
    $this->functionMaps[$func "_" $loc]) && 
                
    is_array($this->functionMaps[$func "_" $loc])) {
                foreach(
    $this->functionMaps[$func "_" $loc] as $newFunc) {
                    
    $newData $newFunc($args);
                    if(
    $newData == false) {
                        return(array(
    "status"=>HOOK_FAILED));
                    } else if(
    is_array($newData)) {
                        if(isset(
    $newData["continue__"])) {
                                
    $cont $newData["continue__"];
                                unset(
    $newData["continue__"]);
                        }
                        
    $args array_merge($args$newData);
                        if(
    $cont == false) {
                            return(array(
    "status"=>HOOK_SUCCESS_STOP
                            
    "args"=>$args));
                        }
                    }
                }
                return(array(
    "status"=>HOOK_SUCCESS"args"=>$args));
            } else {
                return(array(
    "status"=>HOOK_NONE));
            }
        }
    Finally, here's how a function would allow itself to be hooked both before and after code execution:
    PHP:
        function foo($foovar) {
            
    $hookPre = ($this->hook("foo""pre", array("foovar"=>$foovar)));
            if(
    $hookPre["status"] >= HOOK_SUCCESS && 
                
    is_array($hookPre["args"])) {
                
    extract($hookPre["args"], EXTR_OVERWRITE);
            }
            if(
    $hookPre["status"] == HOOK_SUCCESS_STOP) {
                return(
    $foovar);
            }
            
            
    $foovar strtoupper($foovar);
            
            
    $hookPost = ($this->hook("foo""post", array("foovar"=>$foovar)));
            if(
    $hookPost["status"] >= HOOK_SUCCESS && is_array($hookPost["args"])) {
                
    extract($hookPost["args"], EXTR_OVERWRITE);
            }
            return(
    $foovar);
        }
    And here's a sample plugin: (lowercase.plugin.php)
    PHP:
    $functionMaps = array("foo_post"=>"lowercase_it");

    function 
    lowercase_it($args) {
        if(isset(
    $args["foovar"])) {
            
    $args["foovar"] = strtolower($args["foovar"]);
        }
        
        
    $args["continue__"] = true;
        
        return(
    $args);
    }
    Any tips on making this better?

    The purpose is to be able to simply throw a plugin file into a folder and have it work. Plugins shouldn't really modify the output of a page so much, because this isn't a content management system. The only time plugins will really be used will probably be for authentication or other core functionality.
     
  2. Glider

    Glider /dev/null

    Joined:
    2 Aug 2005
    Posts:
    4,173
    Likes Received:
    21
    I would just include an entire directory and dump the plugins in there. But that might not do what you want it to do?
     

Share This Page