CodeIgniterの学習 47 - PEAR :: Var_Dumpを使ってプロファイラにvar_dump情報を整形して吐く俺俺ヘルパを作る

今日は昨日のエントリーの関連。

せっかくCodeIgniter上でPEARを簡単に使えるようにしたので、
開発が楽しくなるようにPEAR :: Var_Dumpを簡単に呼べるヘルパを作ってみる。


以下PEARと決別した人には全く不要なエントリー。
俺は現実派のヌルグラマなんで、他のデファクトの趨勢が決まるまではとりあえず使うのだ。


Var_Dumpが何かとか、設置方法とかはPEARを入れればすぐ分かるので省略する。
(http://pear.php.net/package/Var_Dump)


画面

使い方:
適当なコントローラ等で、

vd($this->db);

とかすると、
こんな感じにvar_dumpの情報がプロファイラ上に表示される。


画面:(長いので途中一部省略)


var_dump($this->db)と違い、
見た目がすっきり整って、プロファイラに収まってくれて良い感じ。

ちなみに vd($this)なんてやると、メモリオーバーするんで万能じゃないけどまあいいや。



ソース

昨日のエントリーのpearloader改を使用。いつもながら無保証。

呼びだし側は、適当な俺俺ヘルパ(cmn_helper.php)に設置しておく。

(08/12/18追記)
cmn_helper.phpにvd()を設置した場合は、
application/config/autoload.phpに、

$autoload['helper'] = array('cmn');

等としておくと、毎回ヘルパをロードせずに、
コア部分を除き、ほぼどこからでもvd()が呼べるようになる。
(追記終わり)


1)ヘルパ側
application/helpers/cmn_helper.php

<?php
 //上略
if ( ! function_exists( 'vd' ) ) {
  // var_dump短縮形
  function vd( $data )
  {
    $CI =& get_instance();
    //if(TRUE === $CI->config->item('my_debugger')){//開発時のみvdを有効にする
      $CI->load->library('pearloader');
      if(!isset($CI->objvd) || !is_object($CI->objvd)){
        $CI->objvd = $CI->pearloader->load('Var_Dump', array('display_mode' => 'HTML4_Table'));
      }
      if(!isset($CI->vddata) || !is_array($CI->vddata)){
        $CI->vddata = array();
      }
      $CI->vddata[] = $CI->objvd->toString($data);
    //}
  }
}
//下略
 ?>

objvdと、vddataは$CI->以下に勝手に作った。
名前が微妙なら好きに変えてください。



2)プロファイラ側

現状の俺のMY_Profilerはこんな感じ。

既にCodeIgniter 1.7.0に変更しているので、
1.6.3環境だと存在しないメソッドも run()の中で呼び出している事に注意。

(_compile_controller_info()とかが 1.7.0から追加されているっぽい。
あと_compile_queries()が色鮮やかになってたりする。)


ちなみにここでは

  • _compile_session() はセッションデータ
  • _compile_cookie() は(プロファイラが呼ばれた時点での)クッキーデータ
  • _compile_vd() は今回のヘルパで追加される PEAR :: Var_Dumpのデータ

の3つを追加している。


application/libraries/MY_Profiler.php

<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
// session情報を表示するようにした。
// $autoload['libraries'] = array('database','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 -> session ) ) {
      $data = $this -> CI -> session -> all_userdata();
      if ( count( $data ) == 0 ) {
        return "";
      }
      $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;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 _compile_cookie()
  {
    if ( count( $_COOKIE ) == 0 ) {
      return "";
    }

    $output = "\n\n";
    $output .= '<fieldset style="border:1px solid #8a2be2;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
    $output .= "\n";
    $output .= '<legend style="color:#8a2be2;">&nbsp;&nbsp;cookie_data(server)&nbsp;&nbsp;</legend>';
    $output .= "\n";

    if ( count( $_COOKIE ) == 0 ) {
      $output .= "<div style='color:#8a2be2;font-weight:normal;padding:4px 0 4px 0'>NO COOKIE</div>";
    }else {
      $output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";

      foreach ( $_COOKIE as $key => $val ) {
        if ( ! is_numeric( $key ) ) {
          $key = "'" . $key . "'";
        }
        $output .= "<tr><td width='50%' style='color:#000;background-color:#ddd;'>&#36;_COOKIE[" . $key . "]&nbsp;&nbsp; </td><td width='50%' style='color:#8a2be2;font-weight:normal;background-color:#ddd;'>";
        if ( is_array( $val ) ) {
          $output .= "<pre>" . htmlspecialchars( stripslashes( print_r( $val, true ) ) ) . "</pre>";
        }else {
          $output .= htmlspecialchars( stripslashes( $val ) );
        }
        $output .= "</td></tr>\n";
      }

      $output .= "</table>\n";
    }
    $output .= "</fieldset>";

    return $output;
  }

  //var_dumpデータ
  function _compile_vd()
  {
    $output = "\n\n";
    if(isset($this -> CI ->vddata) && is_array($this -> CI ->vddata)){
      $output .= '<fieldset style="border:1px solid #339900;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
      $output .= "\n";
      $output .= '<legend style="color:#339900;">&nbsp;&nbsp;var_dump&nbsp;&nbsp;</legend>';
      $output .= "\n";
      foreach($this -> CI ->vddata as $k => $v){
        $output .= $v ;
      }
      $output .= "</fieldset>";
    }
    return $output;
  }

  function run()
  {
    $output = '<br clear="all" />';
    $output .= "<div style='background-color:#fff;padding:3px 10px;'>";
    $output .= $this -> _compile_vd();
    $output .= $this -> _compile_uri_string();
    $output .= $this -> _compile_controller_info();
    $output .= $this -> _compile_memory_usage();
    $output .= $this -> _compile_benchmarks();
    $output .= $this -> _compile_session();
    $output .= $this -> _compile_get();
    $output .= $this -> _compile_post();
    $output .= $this -> _compile_cookie();
    $output .= $this -> _compile_queries();
    $output .= '</div>';
    return $output;
  }
}
// END CI_Profiler class


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

 ?>

(count( $data ) == 0 とかで途中で抜けているのに、下側でまたcountが出てくるのは、不要なプロファイル情報を非表示にするために後で追加して整理してないから。いわゆる手抜き)




これで作業効率2%アップ!したらいいな。

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