/source/includes/classes/plugin.php
http://prosporous.googlecode.com/ · PHP · 75 lines · 45 code · 1 blank · 29 comment · 7 complexity · 55c84af0ee8c91e1fab0dc3395340de2 MD5 · raw file
- <?php
- /*
- * edite in NetBeans of IDE at version 7.0.1.
- */
- /**
- * ??????
- * ????????????
- *
- * @author Zuolong Wang
- */
- class Plugin {
- private static $_pluginDir = 'Plug-in';
- private static $_instance = null;
- private static $_plugins = array();
- /**
- *?????
- * 2011-11-22 ????? '.php'
- * @var static
- */
- private static $_ext = '.php';
- private function __construct() {
- //???
- defined('BIC_ROOT') || define('BIC_ROOT',substr(dirname(__FILE__),0,-16));
- define('DS',DIRECTORY_SEPARATOR);
- $this -> _pluginPath = BIC_ROOT . self :: $_pluginDir;
- }
- public static function init(){
- if (is_null(self :: $_instance)){
- self :: $_instance = new self();
- }
- return self :: $_instance;
- }
- public function add($hook,$pluginName){
- self :: $_plugins[$hook][] = $pluginName;
- }
- /**
- *????hook???????,???????
- * @param type $hook
- * @return type
- */
- public function trigger($hook){
- if (!isset(self :: $_plugins[$hook])) return;
- foreach (self :: $_plugins[$hook] as $plugin){
- //????
- if (is_dir($this -> _pluginPath . $plugin)){
- $plugin_file = $this -> _pluginPath . $plugin . DS . $plugin . self :: $_ext;
- }else{
- $plugin_file = $this -> _pluginPath . DS . $plugin . self :: $_ext;
- }
- //?????
- if (is_string($plugin) && preg_match("/^[\w\-\/]+/mi",$plugin) && is_file($plugin_file)){
- require $plugin_file;
- /**
- * ???trigger()??????????????doAction()???????
- */
- $params = array_slice(func_get_args(),1);
- call_user_func_array(array(new $plugin(),'doAction'),$params);
- }else{
- self :: log('???????????????? ?'.$plugin_file);
- }
- }
- }
- /**
- *??????
- * @param type $msg
- */
- private static function log($msg){
- echo $msg;
- }
- }
- interface IPlugin{
- function doAction();
- }
- ?>