FirePHPの代替

数週間後、PHPの学習を開始することを決めました。しばらくして、エコーによるスクリプトのデバッグが不便になりました。 firePHPのようなものの存在を思い出し、ドキュメントを読んで、ダウンロードして、インストールして、喜んでいました。 コンソールからスクリプトをデバッグするのはとてもクールです!

しかし、残念ながら、私が理解していなかった理由(「X-Wf-」ヘッダーが届きますが、「Firebugコンソールにメッセージがありません」*)のため、うまくいきませんでした。 私はインターネットを見ました-失敗しました、私は自分で問題を解決しなければなりませんでした。



ソリューションは、javascriptコードの生成による、ファイアウォールコンソールへのPHPインターフェイスです。



実際のソース:

class debug {



private $code = "<script type="text/javascript" id="debug">(function(){",

$timers = array (),

$counter = 0,

$vars = array();



function debug() {

$ this ->allow = true ;

}



/* */

public function end() {

if ($ this ->allow){

if ( sizeof ($ this ->vars) > 0){

$dump = 'function dump(a,b){var c="";if(!b)b=0;var d="";for(var j=0;j++<=b;)d+=" ";if(typeof(a)==\'object\'){for(var e in a){var f=a[e];if(typeof(f)==\'object\'){c+=d+"\'"+e+"\' ...\n";c+=dump(f,b+1)}else{c+=d+"\'"+e+"\' => \""+f+"\"\n"}}}return c}' ;

$ this ->code .= $dump;

}

echo $ this ->code. "})();</script>" ;

} else return null ;

}



/*

*

*/

public function group($name) {

$ this ->code .= "console.group('" .$name. "');" ;

return $ this ;

}



public function groupEnd() {

$ this ->code .= "console.groupEnd();" ;

return $ this ;

}



/*

*

*/

public function time($name) {

$mtime = microtime();

$mtime = explode( ' ' , $mtime);

$mtime = $mtime[1]+$mtime[0];

$ this ->timers[$name] = $mtime;

return $ this ;

}



/*

*

*/

public function timeEnd($name) {

$timeStart = $ this ->timers[$name];

if ($timeStart) {

$mtime = microtime();

$mtime = explode( " " , $mtime);

$mtime = $mtime[1]+$mtime[0];

$endtime = $mtime;

$totaltime = $endtime-$timeStart;

$ this ->info( "$name: $totaltime seconds" );

$ this ->timers[$name] = null ;

}

return $ this ;

}



/*

*

*/

private function consoleType($msg, $mode) {

if (is_string($msg))

$msg = "'$msg'" ;



if (is_array($msg)){

$name = "o" .($ this ->counter++);

$ this ->code .= $ this ->js_hash($msg,$name);

$ this ->code .= "console." .$mode. "(dump(" .$name. "));" ;

}



if (!$name)

$ this ->code .= "console." .$mode. "(" .$msg. ");" ;

}



//

public function log($msg) {

$ this ->consoleType($msg, "log" );

return $ this ;

}

//

public function error($msg) {

$ this ->consoleType($msg, "error" );

return $ this ;

}

//

public function warn($msg) {

$ this ->consoleType($msg, "warn" );

return $ this ;

}

//

public function info($msg) {

$ this ->consoleType($msg, "info" );

return $ this ;

}



/*

* JS PHP

*/

private function js_hash($arr, $name, & $code = '' ) {

if (!$ this ->vars[$ this ->counter]) {

$code .= "var " ;

$ this ->vars[$ this ->counter] = true ;

}



$code .= $name. "={};" ;



foreach ($arr as $key=>$value) {



$outKey = (is_int($key))? '[' .$key. ']' : ".$key" ;



if (is_array($value)) {

$ this ->js_hash($value, $name.$outKey, $code);

continue ;

}



$code .= $name.$outKey. "=" ;



if (is_string($value)) {

$code .= "'" .$value. "';" ;

} else if ($value === false ) {

$code .= "false;" ;

} else if ($value === NULL) {

$code .= "null;" ;

} else if ($value === true ) {

$code .= "true;" ;

} else {

$code .= $value. ";" ;

}

}

return $code;

}

}










使用例:



$d = new debug();



//

$d->time( 'Timer' );



// :)

$d->log( "WELCOME!" );



//

$arr = array();

for ($i = 0; $i < 6; $i++){

$arr[] = "line #" . $i;

}



//

$d->log($arr);



$name = "Evgeny" ;

$age = 18;



//

$myHobbies = array (

"films" ,

"music" => array( "full on" , "psy" , "dark" ),

"sports" => array(

"extreme" => array( "climbing" , "snowboard" ),

"diving" ,

"karting"

),

array(1,2,3,5, "sometxt" )

);



// About

$d->group( "About" );

//

$d->log( "Hello, my name is $name" );

$d->log( "and i am $age" );

$d->groupEnd();



/* :

* $d->group("About")

* ->log("Hello, my name is $name")

* ->log("and i am $age")

* ->groupEnd();

*/



//

$d->group( "My hobbies" );

//

$d->info( "list of my hobbies" );

//

$d->log($myHobbies);

//

$d->groupEnd();



// info

$d->timeEnd( 'Timer' );

//

$d->end();

// $d->timeEnd('Timer')->end();



/*

* end(),

* ,

* .

*/










end()メソッドを呼び出した後、次のjavascriptコードがhtmlドキュメントに追加されます。

( function (){console.log( 'WELCOME!' ); var o0={};o0[0]= 'line #0' ;o0[1]= 'line #1' ;o0[2]= 'line #2' ;o0[3]= 'line #3' ;o0[4]= 'line #4' ;o0[5]= 'line #5' ;console.log(dump(o0));console.group( 'About' );console.log( 'Hello, my name is Evgeny' );console.log( 'and i am 18' );console.groupEnd();console.group( 'My hobbies' );console.info( 'list of my hobbies' ); var o1={};o1[0]= 'films' ;o1.music={};o1.music[0]= 'full on' ;o1.music[1]= 'psy' ;o1.music[2]= 'dark' ;o1.sports={};o1.sports.extreme={};o1.sports.extreme[0]= 'climbing' ;o1.sports.extreme[1]= 'snowboard' ;o1.sports[0]= 'diving' ;o1.sports[1]= 'karting' ;o1[1]={};o1[1][0]=1;o1[1][1]=2;o1[1][2]=3;o1[1][3]=5;o1[1][4]= 'sometxt' ;console.log(dump(o1));console.groupEnd();console.info( 'Timer: 0.000530004501343 seconds' ); function dump(a,b){ var c= "" ; if (!b)b=0; var d= "" ; for ( var j=0;j++<=b;)d+= " " ; if ( typeof (a)== 'object' ){ for ( var e in a){ var f=a[e]; if ( typeof (f)== 'object' ){c+=d+ "'" +e+ "' ...\n" ;c+=dump(f,b+1)} else {c+=d+ "'" +e+ "' => \"" +f+ "\"\n" }}} return c}})();









そして、花火コンソールでは、

画像



脅威

コメント、スクリプトの提案-コメント内



ZZY

これらすべてが機能するには、 firefoxfirebugが必要です




All Articles