dBug版Debugフィルタ

作ってみた。さらにmaple.inc.phpデバッグモードを追加して起動できるようにしてみました。

http://dbug.ospinto.com/ からdBug.phpをダウンロードしてmaple/core/にコピーします。
(ライセンスが特にないので、Mapleに添付してもいいんじゃないかと。)

以下修正部分(後でパッチ送ります)

core/ConfigUtils.class.phpのexecute()に以下を追加

    function execute() {
        $curPath = "/";

        ConfigUtils::_getConfig($curPath);

        $container =& DIContainerFactory::getContainer();
        $actionChain =& $container->getComponent("ActionChain");
        $pathList = explode("_", $actionChain->getCurActionName());

        // 以下3行を追加
        if (DEBUG_MODE) {
            $this->_setConfig('Debug', array());
        }

        $count = 0;

        foreach ($pathList as $path) {
            $curPath .= "${path}/";
            $count++;
            ConfigUtils::_getConfig($curPath, ($count == count($pathList)));
        }
    }

maple.inc.phpに以下を追加

define('DEBUG_MODE',  1);

core/filterに Filter_Debug.class.phpを追加

<?php
//
// +--------------------------------------------------------------------+
// | PHP version 4                                                      |
// +--------------------------------------------------------------------+
// | Copyright (c) 2004-2005 The Maple Project                          |
// +--------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,    |
// | that is available at http://www.php.net/license/3_0.txt.           |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to        |
// | license@php.net so we can mail you a copy immediately.             |
// +--------------------------------------------------------------------+
// | Authors: Kazunobu Ichihashi <bobchin_ryu@bb.excite.co.jp>          |
// +--------------------------------------------------------------------+
//
// $Id: $
//

/**
 * デバッグ情報を表示するFilter
 *
 * @author  Kazunobu Ichihashi
 * @package maple.filter
 * @since   3.0.2
 **/
class Filter_Debug extends Filter {
    
    /** @var array 表示する内容  */
    var $debugs = array();

    /**
     * コンストラクター
     *
     * @access  public
     * @since   3.0.0
     */
    function Filter_Debug() {
        parent::Filter();
    }

    /**
     * デバッグ情報を表示する
     *
     * @access  public
     * @since   3.0.0
     **/
    function execute() {
        
        $log =& LogFactory::getLog();
        $log->trace("Filter_Debugの前処理が実行されました", "Filter_Debug#execute");
        $this->_preFilter();
        
        $container =& DIContainerFactory::getContainer();
        $filterChain =& $container->getComponent("FilterChain");
        $filterChain->execute();

        $log->trace("Filter_Debugの後処理が実行されました", "Filter_Debug#execute");
        $this->_postFilter();

    }
    
    /**
     * プリフィルター
     */
    function _preFilter()
    {
        // 何もしない
    }
    
    /**
     * ポストフィルター
     */
    function _postFilter()
    {
        $NO = 'なし';
        if (OUTPUT_CODE != INTERNAL_CODE) {
            $NO = mb_convert_encoding($NO, OUTPUT_CODE, INTERNAL_CODE);
        }
        $var = (0 < count($_POST))? $_POST: $NO;
        $this->addParam('リクエストパラメータ($_POST)', $var);
        
        $var = (0 < count($_GET))? $_GET: $NO;
        $this->addParam('リクエストパラメータ($_GET)', $var);

        $var = (0 < count($_FILES))? $_FILES: $NO;
        $this->addParam('リクエストパラメータ($_FILES)', $var);

        $var = (0 < count($_SESSION))? $_SESSION: $NO;
        $this->addParam('リクエストパラメータ($_SESSION)', $var);

        $container =& DIContainerFactory::getContainer();
        $DIDebug = array();
        foreach ($container->_components as $name => $obj) {
            // なぜかdBug自身を表示するとダメなので
            if ($name == "FilterChain") {
                $save = $obj->_list['Debug'];
                unset($obj->_list['Debug']);
                $obj->_list['Debug'] = $save;
            }
            $DIDebug[$name] = get_object_vars($obj);
        }
        $this->addParam("DIContainer", $DIDebug);
        
        $actionChain =& $container->getComponent("ActionChain");
        foreach ($actionChain->_list as $name => $action) {
            $this->addParam("アクション({$name})", get_object_vars($action));
            //$this->addParam("エラーリスト({$name})", get_object_vars($actionChain->_errorList[$name]));
        }

        $this->_printDebug();
    }

    /**
     * デバッグ情報を追加する
     * 
     * @param string $title タイトル
     * @param mixed $var デバッグ対象の変数
     */
    function addParam($title, $var)
    {
        if (OUTPUT_CODE != INTERNAL_CODE) {
            $title = mb_convert_encoding($title, OUTPUT_CODE, INTERNAL_CODE);
        }
        $this->debugs[$title] = $var;
    }
    
    /**
     * Javascript用のエスケープ処理
     * 
     * @param string $src javascriptソース
     */
    function _escapeJavascript($src)
    {
        return strtr($src, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
    }
    
    /**
     * デバッグを表示する
     */
    function _printDebug()
    {
        ob_start();
        require_once 'core/dBug.php';
        new dBug($this->debugs, 'array');
        $debug = ob_get_contents();
        ob_end_clean();

        // 表示するHTMLの作成
        $html = <<<HTML
<HTML>
<HEAD>
<TITLE>Maple Debug Console</TITLE>
</HEAD>
<BODY bgcolor=#ffffff>
<table border=0 width=100%><tr bgcolor=#cccccc><th colspan=2>Maple Debug Console</th></tr></table>
{$debug}
</BODY>
</HTML>
HTML;

        // Javascript出力用にエスケープ処理
        $html = $this->_escapeJavascript($html);

        // Javascript出力
        $JS = <<<JS
<SCRIPT language=javascript>
    var title = 'Console';
    _smarty_console = window.open("",title.value,"width=680,height=600,resizable,scrollbars=yes");
    _smarty_console.document.write("$html");
    _smarty_console.document.close();
</SCRIPT>
JS;
        print $JS;
    }
}

?>