====== デバッグ出力 - php-debugbar ====== Version 1.16 ([[https://github.com/maximebf/php-debugbar/blob/master/LICENSE|MIT License]]) --- //[[http://www.y2sunlight.com|y2sunlight]] 2020-03-18// [[basic-library:top|定番ライブラリー に戻る]] 関連記事 * [[basic-library:project|プロジェクトの作成 - Apricot (α版)]] * [[basic-library:phpdotenv:4.1|環境変数 - phpdotenv]] * [[basic-library:monolog:2.0|ログ出力 - monolog]] * [[basic-library:idiorm:1.5|ORマッパー - Idiorm]] * [[basic-library:bladeone:3.37|テンプレートエンジン - BladeOne]] * [[basic-library:fast-route:1.3|リクエストルーター - FastRoute]] * [[basic-library:league-container:3.3|DIコンテナー - League/Container]] * [[basic-library:valitron:1.4|バリデーター - Valitron]] * [[basic-library:whoops:2.7|エラーハンドラー - Whoops]] * デバッグ出力 - php-debugbar リンク * https://github.com/maximebf/php-debugbar --- PHP Debug Barの本家 * http://phpdebugbar.com/docs/ --- PHP Debug Barの本家のドキュメント(英語) テストプログラムの所在 {Project Folder}\test\ ---- ===== php-debugbarについて ===== エラーログと共にデバッグログ(デバッグライト)はプログラム開発では無くてはならない存在です。[[https://www.php.net/manual/ja/function.var-dump.php|var_dump()]] は変数の中身を簡単に表示してくれる Cool な関数ですが、WEBプログラムでは適当な表示先が無いのでログファイルに書かなくてはなりません。そんな時に役立つのが php-debugbar です。 php-debugbarは、ブラウザにデバッグバーを表示し、PHPでデバッグ出力した情報を画面に綺麗に整形して表示してくれます。Whoops と同様に本当に pretty cool な存在です。whoops と php-debugbar はPHPデバッグ環境の最強バディと言ってもいいでしょう。 DebugBarには、ブリッジコレクターと呼ばれる他のパッケージとの統合方法が提供されており、ORMの[[https://www.doctrine-project.org/|Doctrine]]や[[http://propelorm.org/|Propel]]、ログ出力の[[https://github.com/Seldaek/monolog|Monolog]]、メール送信の[[https://swiftmailer.symfony.com/|Swift Mailer]]などがその例です。また、Laravel、Zend Framework、PhalconなどのPHPフレームワークとも統合されています。DebugBarもまた、PHPのデバッグツールとしての選定に関しては異論の余地がありません。 ===== インストール ===== composer require maximebf/debugbar Using version ^1.16 for maximebf/debugbar ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 3 installs, 0 updates, 0 removals - Installing symfony/polyfill-mbstring (v1.14.0): Downloading (100%) - Installing symfony/var-dumper (v5.0.5): Downloading (100%) - Installing maximebf/debugbar (v1.16.1): Downloading (100%) symfony/var-dumper suggests installing ext-intl (To show region name in time zone dump) symfony/var-dumper suggests installing symfony/console (To use the ServerDumpCommand and/or the bin/var-dump-server script) maximebf/debugbar suggests installing kriswallsmith/assetic (The best way to manage assets) maximebf/debugbar suggests installing predis/predis (Redis storage) Writing lock file Generating autoload files 1 package you are using is looking for funding. Use the `composer fund` command to find out more! >Note: >Eclipse起動中にパッケージを取得した場合は、プロジェクト・エクスプローラー内の[プロジェクト(apricote)]を右クリックして[リフレッシュ]を選択して下さい。また、新しく取得したパッケージのインテリセンスが有効にならない場合は、プロジェクトのビルトまたはクリーン&ビルドを行ってビルドリストの更新を行って下さい。 パッケージの取得が終わると ''composer.json'' の ''require'' に以下が追加されます。 { "require": { "maximebf/debugbar": "^1.16" } } \\ ===== テスト ===== パッケージのテストフォルダ(''test\'')に、テスト用のコード(''php-debugbar.php'')を作成し実行します。この例では、''DEBUG_MODE'' が ''true'' の場合のみ ''Debug Bar'' が有効になります。詳細はコード内のコメントを参照して下さい。 getJavascriptRenderer($base_url); // デバッグライトはメッセージコレクターに出力する // 使い方はvar_dump($var)と同じ $arr = ['0'=>'apple','1'=>'banana','2'=>'chery',]; $debugbar['messages']->debug($arr); // PSR-3 logger互換のログ出力 $debugbar['messages']->info("info"); $debugbar['messages']->notice("notice"); $debugbar['messages']->warning('warning'); $debugbar['messages']->error('error'); ?> renderHead() ?> ... render() ?> === 実行結果 === [{{basic-library:php-debugbar:debug-bar01.png?nolink}}] ■ 標準のデバッグレンダラーでは以下の表示ができます * php ---PHP バージョン番号 * memory --- メモリ使用量 * [[http://phpdebugbar.com/docs/base-collectors.html#timedata|time]] --- 総実行時間(または任意の測定結果) * [[http://phpdebugbar.com/docs/base-collectors.html#messages|messages]] --- デバッグ出力(debug/info/notice/warning/errorメソッドで出力した値) * [[http://phpdebugbar.com/docs/base-collectors.html#requestdata|request]] --- PHPのスーパーグローバル変数($_GET/$_POST/$_COOKIE/$_SERVER) * [[http://phpdebugbar.com/docs/base-collectors.html#exceptions|exceptions]] --- 例外表示 ■ デバッグ出力の種類毎にフィルタリングできます。 \\ ■ メッセージをクリックすると変数の展開できます。