CodeIgniterの学習 22 - 開発用のプロファイラを拡張し、db_sessionの情報を表示する

今日はCodeIgniterのProfiler.phpを拡張してみる。

というのも、ワンタイムトークン(ワンタイムチケット)のライブラリの俺俺改造(というかほぼ作り直し)の途中で、
データベース利用のセッション(db_session)の情報をいちいち見るのが面倒になったから、ついでに作ってみた。


画面

こんな感じ、db_sessionの情報が表示される。


ソース

殆どが_compile_post()をコピペ改変しただけ、作成時間10分

$autoload['libraries'] = array('database','Db_session');
が有効であることが前提、その他の場合はエラーにはならないと思うけど知らん。


作法にならい、system/libraries/Profiler.php を継承している。

application/libraries/MY_Profiler.php

<?php  
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//db_session情報を表示するようにした。
//$autoload['libraries'] = array('database','Db_session')になっている事が前提
class MY_Profiler extends CI_Profiler {
    function MY_Profiler()
    {
        parent :: CI_Profiler();
    } 

    function _compile_session()
    {   
        $output = "";
        if(isset($this->CI->db) && isset($this->CI->db_session)){
            $data = $this->CI->db_session -> all_userdata();
            $output  = "\n\n";
            $output .= '<fieldset style="border:1px solid #ff6600;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
            $output .= "\n";
            $output .= '<legend style="color:#ff6600;">&nbsp;&nbsp;db_session&nbsp;&nbsp;</legend>';
            $output .= "\n";
                
            $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";

            if (count($data) == 0)
            {
                $output .= "<div style='color:#ff6600;font-weight:normal;padding:4px 0 4px 0'>NO DATA</div>";
            }else{
            
                foreach ($data as $key => $val)
                {
                
                    $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>".$key."&nbsp;&nbsp; </td><td width='50%' style='color:#ff6600;font-weight:normal;background-color:#ddd;'>";
                    if (is_array($val))
                    {
                        $output .= "<pre>" . htmlspecialchars(stripslashes(print_r($val, true))) . "</pre>";
                    }
                    else
                    {                        
                        #modified at 2010-04-07 for objects
                        #$output .= htmlspecialchars( stripslashes( $val ) );
                        if ( is_object( $val ) ) {
                          $output .= htmlspecialchars( stripslashes(print_r( $val,true )) );
                        }else{
                          $output .= htmlspecialchars( stripslashes( $val ) );
                        }
                        #modified end

                    }
                    $output .= "</td></tr>\n";
                }
            }
            $output .= "</table>\n";
            $output .= "</fieldset>";
        }
        return $output; 
    }
    
    function run()
    {       
        $output = '<br clear="all" />';
        $output .= "<div style='background-color:#fff;padding:10px;'>";
        
        $output .= $this->_compile_memory_usage();
        $output .= $this->_compile_benchmarks();    
        $output .= $this->_compile_uri_string();
        $output .= $this->_compile_get();
        $output .= $this->_compile_post();
        $output .= $this->_compile_session();
        $output .= $this->_compile_queries();
        $output .= '</div>';
        return $output;
    }

}// END MY_Profiler class

/* End of file MY_Profiler.php */
/* Location: ./application/libraries/MY_Profiler.php */
 ?>

使い方


なんてこと無いけど、
プロファイラについては、 http://d.hatena.ne.jp/dix3/20081005/1223161593
Db_sessionに付いては、http://d.hatena.ne.jp/dix3/20080921/1221946495
も参照のこと。





これで、db_session -> all_userdata()の情報が見られるようになりました。パチパチ。



ワンタイムトークンのライブラリの俺俺改造も一応出来たけれど、
一度に書くとネタに困るので明日ぐらいにする。


作業履歴のはずが、ネタを探して書くようになってきた。まーいいや。


(2010/4/7 部分修正)is_objectの時の分岐を追加。