在Yaf中应用Zend_Log

Zend Framework是Zend 官方用PHP写的一套MVC框架,里面包含了诸多组件,可以单独使用,但是在PHP的MVC框架中的性能评测中总是处于下风。在之前的项目中有使用过Zend Framework,对于结构和代码的规范有比较好的帮助,类库也非常全面,所以想在其他的一些项目中也能够应用到Zend Framework的一些Library。Yaf国内牛人仿Zend Framework开发的一个PHP MVC框架的C扩展,性能较Zend Framework有巨大的提升,但是不如Zend Framework全面,更侧重于前端控制,大概后端用其方法来做更好。Yaf的文档少了些,而Zend Framework的应用则比较多,社区支持也更广泛。所以在新的项目采用了Yaf做为前端的分发管理器,结合Zend Framework的类库来进行开发。
本文是关于Zend_Log中添加Zend_Log_Writer_Firebug无效,不能输出到firebug的解决方法。其实这个问题刚好在其他项目中应用的时候也有碰到,之后在stackoverflow上找到了答案,原来是这个功能还依赖于Zend_Wildfire_Channel_HttpHeaders,又需要Zend_Controller_Request_Http和Zend_Controller_Response_Http。于是就仿着写了一个Yaf扩展,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
/**
 * Ap定义了如下的7个Hook,
 * 插件之间的执行顺序是先进先Call
 */
class WildfireChannelPlugin extends Yaf_Plugin_Abstract {
 
    public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
    }
 
    public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
    }
 
    public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) {
        $this->_request = new Zend_Controller_Request_Http();
        $this->_response = new Zend_Controller_Response_Http();
        $this->_channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
        $this->_channel->setRequest($this->_request);
        $this->_channel->setResponse($this->_response);
        ob_start();
    }
 
    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) {
        if(isset($this->_channel)){
            $this->_channel->flush();
            $this->_response->sendHeaders();
        }
    }
 
}

然后在Bootstrap.php中注册扩展,代码如下:

1
2
3
4
5
6
public function _initPlugin(Yaf_Dispatcher $dispatcher) {
    if(Yaf_Registry::get("config")->get("log")->get("firebug")){
        $WildfireChannel = new WildfireChannelPlugin();
        $dispatcher->registerPlugin($WildfireChannel);
    }
}

之后,Zend_Log_Writer_Firebug和firebug就可以正常工作了。如果要在其他项目中应用,只需要在开始输出前和输出结束后,分别调用上面连个方法就可以了,比如在启动文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Boot{
    protected $_request;
    protected $_response;
    protected $_channel;
    protected function _initWildfireChannel(){
        $this->_request = new Zend_Controller_Request_Http();
        $this->_response = new Zend_Controller_Response_Http();
        $this->_channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
        $this->_channel->setRequest($this->_request);
        $this->_channel->setResponse($this->_response);
        ob_start();
    }
    public function __destruct(){
        if(isset($this->_channel)){
            $this->_channel->flush();
            $this->_response->sendHeaders();
        }
    }
}

然后按需打开WildfireChannel就可以了。
参考链接:
Zend_Log_Writer_Firebug() not working
高性能PHP框架 Phalcon
Zend Framework

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据