yaf框架流程四

在前面的章节,在bootstrap里添加了一个benchmark插件,简单介绍下yaf的插件机制:http://yaf.laruence.com/manual/yaf.plugin.html

Yaf定义了6个Hook, 它们分别是:

表 7.1. Yaf的Hook

触发顺序名称触发时机说明
1 routerStartup 在路由之前触发 这个是7个事件中, 最早的一个. 但是一些全局自定的工作, 还是应该放在Bootstrap中去完成
2 routerShutdown 路由结束之后触发 此时路由一定正确完成, 否则这个事件不会触发
3 dispatchLoopStartup 分发循环开始之前被触发  
4 preDispatch 分发之前触发 如果在一个请求处理过程中, 发生了forward, 则这个事件会被触发多次
5 postDispatch 分发结束之后触发 此时动作已经执行结束, 视图也已经渲染完成. 和preDispatch类似, 此事件也可能触发多次
6 dispatchLoopShutdown 分发循环结束之后触发 此时表示所有的业务逻辑都已经运行完成, 但是响应还没有发送
class BenchmarkPlugin extends Yaf_Plugin_Abstract {
    public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
        Yaf_Registry::set('benchmark_start', microtime(true));
    }

    public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
    }

    public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

    }

    public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

    }

    public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {

    }

    public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
        $start = Yaf_Registry::get('benchmark_start');
        Yaf_Registry::del('benchmark_start');
        $time = microtime(true) - (float)$start;
        if ($time > 1)
        {
            Log::out('benchmark', 'I', $request->getRequestUri() . ':' . $time . ':' . (memory_get_usage(true) / 1024) . 'kb');
        }
    }
}

 Yaf里的单例有

Yaf_Application通过Yaf_Application::app()方式获取对象

Yaf_Loader通过Yaf_Loader::getInstance()方法获取对象,

参数有 string  $local_library_directory = NULL ,string  $global_library_directory = NULL

Yaf_Dispatcher通过Yaf_Dispatcher::getInstance()方法获取对象

Yaf_Registry严格来讲这个并不是一个单例,但是他只有静态方法,无法实例化

Yaf_Session通过Yaf_Session::getInstance()方法获取对象

 

PS:

Yaf_Application::app()->getDispatcher()与Yaf_Dispatcher::getInstance()保存的是一个实例

 

通过代码

 

echo Yaf_Dispatcher::getInstance()->getRouter()->getCurrentRoute();
var_dump($this->getRequest());

exit;
可以在控制器里了解使用的路由,以及请求参数,可以作为调试判断的依据

 

 

posted on 2014-06-04 16:52  kudosharry  阅读(1023)  评论(0编辑  收藏  举报

导航