[CodeIgniter]CI_Profilerを拡張してCI_Session内容も出力してみた


Controllerなどで下記のように記述するとデバッグ用に変数の内容などを出力してくれます。

$this->output->enable_profiler(true);

デフォルトだと、下記の情報が表示されるようです。

  • URI STRING
  • CLASS/METHOD
  • MEMORY USAGE
  • BENCHMARKS
  • GET DATA
  • POST DATA
  • QUERIES

今回は省略しますが、プロファイラ用のホックなんかを使っておくと便利です。

追記:2010-07-20
大したプログラムではないですけど、残しておきます。

system/application/hooks/XC_ProfilerHook.php

< ?php
/**
 * プロファイラ用ホック
 * @author Tatsuya Fukata
 *
 */
class XC_ProfilerHook {
	/** CIインスタンス */
	private $CI;
 
	/**
	 * コンストラクタ
	 */
	public function __construct(){
		$this->CI =& get_instance();
	}
 
	public function enable_profiler() {
		$this->CI->output->enable_profiler($this->CI->config->item('hook_enable_profiler'));
	}
}
 
?>

で、設定ファイルに下記を追加します。

system/application/config/config.php

//プロファイラホックの有効化
$config['hook_enable_profiler'] = true;

さらに、hooks.phpに下記も追加します。

system/application/config/hooks.php

//プロファイラ用ホック
$hook['post_controller_constructor'][] = array(
	'class' => 'XC_ProfilerHook',
	'function' => 'enable_profiler',
	'filename' => 'XC_ProfilerHook.php',
	'filepath' => 'hooks',
);

これで、ローカルや、開発環境ではプロファイラを有効にしておいて、テスト環境や本番で一括でプロファイラの設定を切り替えることができます。

で、本題のPHP標準のSession機構を使用するCI_Session拡張のコードです。

前回の記事でCI_Sessionの拡張について書いたついでにCI_Sessionの持つ、ユーザデータの内容も出力するようにしてみました。

system/application/libraries/XC_Profiler.php

< ?php
/**
 * デフォルトのプロファイラ結果にCI_Sessionの持つ、ユーザデータを追加する
 * 
 * @author Tatsuya Fukata
 *
 */
class XC_Profiler extends CI_Profiler {
	public function __construct() {
		parent::__construct();
	}
 
	public function run() {
		$output = "<div id='codeigniter_profiler' style='clear:both;background-color:#fff;padding:10px;'>";
 
		// XXX スーパクラスのprivateメソッドのため、個別定義
		$output .= $this->_compile_uri_string();
		$output .= $this->_compile_controller_info();
		$output .= $this->_compile_memory_usage();
		$output .= $this->_compile_benchmarks();
		$output .= $this->_compile_get();
		$output .= $this->_compile_post();
		$output .= $this->_compile_queries();
 
		$output .= $this->_compile_session();
 
		$output .= '';
 
		return $output;
	}
 
	/**
	 * ユーザデータ情報を返す。
	 * @return String
	 */
	protected function _compile_session() {
		if (!isset($this->CI->session) || (!is_a($this->CI->session, 'CI_Session') && !is_subclass_of($this->CI->session, 'CI_Session'))) {
			return '';
		}
 
		$output  = "\n\n";
		$output .= '<fieldset style="border:1px solid #FF3535;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">';
		$output .= "\n";
		$output .= '<legend style="color:#FF3535;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_session').'&nbsp;&nbsp;</legend>';
		$output .= "\n";
		$output .= "\n\n<table cellpadding='4' cellspacing='1' border='0' width='100%'>\n";
 
		$session = $this->CI->session->all_userdata();
		if (empty($session) || count($session)==0) {
			$output .= "<tr><td width='100%' style='color:#FF3535;font-weight:normal;background-color:#eee;'>".$this->CI->lang->line('profiler_no_session')."</td></tr>\n";
		} else {
			foreach ($session as $key => $val) {
				$output .= "<tr><td width='100%' style='color:#FF3535;font-weight:normal;background-color:#eee;'>".$key.' => '.print_r($val, true)."</td></tr>\n";
			}
		}
 
		$output .= "</table>\n";
		$output .= "</fieldset>";
 
		return $output;
	}
}
?>

プロファイラ結果として使用する言語ファイルを作成します。
デフォルトのものは、system/language/english/profiler_lang.phpに定義されているので、独自で作成した言語ファイル内で読み込むようにします。

system/application/language/english/profiler_lang.php

< ?php
 
// 既存の言語ファイル
@include_once(BASEPATH.'language/english/profiler_lang.php');
 
// 以下、独自追加分
$lang['profiler_session'] = 'SESSION';
$lang['profiler_no_session'] = 'No SESSION data exists';
 
/* End of file profiler_lang.php */
/* Location: ./system/language/english/profiler_lang.php */
?>

関連記事