zhphpframework (十五) 程序分发,静态反射调用

来源:互联网 发布:java求两个时间差 秒 编辑:程序博客网 时间:2024/05/17 08:07
<?php/** * Created by JetBrains PhpStorm. * User: 张华 * Date: 14-3-8 * Time: 下午12:21 * QQ: 746502560@qq.com * To change this template use File | Settings | File Templates. */defined('IN_ZHPHP')?NULL:die('attempts to hack the system');final class dispatcher {    private function __construct() {}#防止被实例化    /*** * 动态程序分发* @param undefined $router* * @return*/    public static function dispatch(){    $router=engine::load('router');    $routerInfo=createModule_Control_Action(serialize($router));        $controllerfile=APP_PATH.$routerInfo['moduleDir'] .'controllers/'.$routerInfo['trueControllerFileName'].'.class.php';#构建控制器文件        ################## page static  start ######################################        if( is_file($controllerfile)){#判断是否存在该文件\               self::run($controllerfile,$routerInfo);          }else{#否则就运行框架的报错页面            die('<div style="border: 1px solid red; color: #990000;width: 500px; height: 300px;">                   你访问的文件不存在.            </div>');        }       unset($router,$controllerfile,$routerInfo);    }    /*** * * @param undefined $controllerfile* @param undefined $controller* @param undefined $action* @param undefined $actionName* @param undefined $params* * @return*/   public   static function run($controllerfile,$routerInfo){    ############################避免重复加载文件开始#################################    if( ! isset($GLOBALS['_LOADAPPFILENAME'])){ $GLOBALS['_LOADAPPFILENAME']=array();}    $sha1file=sha1_file($controllerfile);    if(in_array($sha1file,$GLOBALS['_LOADAPPFILENAME']) === false){        $GLOBALS['_LOADAPPFILENAME'][]=$sha1file;#避免重复加载将文件名名sha1();        include_once($controllerfile);#加载文件    }    ################################################################################    if(class_exists($routerInfo['trueControllerFileName'])){ #处理用户请求        $has_static= self::has_static_method($routerInfo['trueControllerFileName'],$routerInfo['trueActionName']);       if($has_static === '2' ){           header("HTTP/1.0 404 Not Found");#404错误        }elseif($has_static === '1'){           $controlObj=engine::load($routerInfo['trueControllerFileName']);#实例化控制器对象           $before='before_'.$routerInfo['actionName'];  #是否有在什么之前            $after='after_'.$routerInfo['actionName'];#在之后的操作            if(method_exists($controlObj,$before)){#存在在什么之前                call_user_func_array(array(&$controlObj,$before),$routerInfo['params']);#执行用户的请求            }            call_user_func_array(array(&$controlObj,$routerInfo['trueActionName']),$routerInfo['params']);#执行用户的请求            if(method_exists($controlObj, $after)){#存在在什么之后                call_user_func_array(array(&$controlObj,$after),$routerInfo['params']);#执行用户的请求            }        }     }else{          header("HTTP/1.0 404 Not Found");#404错误        }     unset($controllerfile,$routerInfo,$sha1file);    }    /**     * 判断是否是静态属性 使用反射     * @param $className     * @param $methodName     * @return bool     */    private static   function has_static_method($className,$methodName){      $ref=new ReflectionClass($className);       if($ref->hasMethod($methodName)){          if($ref->getMethod($methodName)->isStatic()){             return false;            }else{               return '1';            }       }else{           return '2';       }    }    }

0 0