標籤:thinkphp5
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <[email protected]>// +----------------------------------------------------------------------namespace think;use think\Config;// 配置use think\exception\ClassNotFoundException;// 異常 類尋找異常use think\Log;// 日誌use think\Request;// 請求use think\Response;// 回複use think\response\Redirect; // 跳轉class Debug{ // 區間時間資訊 protected static $info = [];// 區間 時間資訊 // 區間記憶體資訊 protected static $mem = [];// 區間 記憶體資訊 /** * 記錄時間(微秒)和記憶體使用量情況 * @param string $name 標記位置 * @param mixed $value 標記值 留空則取當前 time 表示僅記錄時間 否則同時記錄時間和記憶體 * @return mixed */ public static function remark($name, $value = ‘‘) {// 記錄 時間 微秒 和 記憶體使用量情況 // 記錄時間和記憶體使用量 self::$info[$name] = is_float($value) ? $value : microtime(true);// 記錄 時間 if (‘time‘ != $value) {// 如果沒有 value self::$mem[‘mem‘][$name] = is_float($value) ? $value : memory_get_usage();// 詳細標記 使用值 self::$mem[‘peak‘][$name] = memory_get_peak_usage();// 標記 鋒值 } } /** * 統計某個區間的時間(微秒)使用方式 * @param string $start 開始標籤 * @param string $end 結束標籤 * @param integer|string $dec 小數位 * @return integer */ public static function getRangeTime($start, $end, $dec = 6) { if (!isset(self::$info[$end])) {// 如果沒有 結束時間 self::$info[$end] = microtime(true); } return number_format((self::$info[$end] - self::$info[$start]), $dec);// 格式化 時間輸出 } /** * 統計從開始到統計時的時間(微秒)使用方式 * @param integer|string $dec 小數位 * @return integer */ public static function getUseTime($dec = 6) { return number_format((microtime(true) - THINK_START_TIME), $dec);// 時間統計 } /** * 擷取當前訪問的吞吐率情況 * @return string */ public static function getThroughputRate() { return number_format(1 / self::getUseTime(), 2) . ‘req/s‘;// 擷取單位時間內 請求此次數 } /** * 記錄區間的記憶體使用量情況 * @param string $start 開始標籤 * @param string $end 結束標籤 * @param integer|string $dec 小數位 * @return string */ public static function getRangeMem($start, $end, $dec = 2) { if (!isset(self::$mem[‘mem‘][$end])) { self::$mem[‘mem‘][$end] = memory_get_usage();// 擷取記憶體 結束情況 } $size = self::$mem[‘mem‘][$end] - self::$mem[‘mem‘][$start]; $a = [‘B‘, ‘KB‘, ‘MB‘, ‘GB‘, ‘TB‘]; $pos = 0; while ($size >= 1024) {// 計算單位 比較經典的用法 $size /= 1024; $pos++; } return round($size, $dec) . " " . $a[$pos];// 顯示 格式化 記憶體 資料 } /** * 統計從開始到統計時的記憶體使用量情況 * @param integer|string $dec 小數位 * @return string */ public static function getUseMem($dec = 2)// 顯示 記憶體 輸出 { $size = memory_get_usage() - THINK_START_MEM; $a = [‘B‘, ‘KB‘, ‘MB‘, ‘GB‘, ‘TB‘]; $pos = 0; while ($size >= 1024) { $size /= 1024; $pos++; } return round($size, $dec) . " " . $a[$pos]; } /** * 統計區間的記憶體峰值情況 * @param string $start 開始標籤 * @param string $end 結束標籤 * @param integer|string $dec 小數位 * @return mixed */ public static function getMemPeak($start, $end, $dec = 2)// 峰值消耗 { if (!isset(self::$mem[‘peak‘][$end])) { self::$mem[‘peak‘][$end] = memory_get_peak_usage(); } $size = self::$mem[‘peak‘][$end] - self::$mem[‘peak‘][$start]; $a = [‘B‘, ‘KB‘, ‘MB‘, ‘GB‘, ‘TB‘]; $pos = 0; while ($size >= 1024) { $size /= 1024; $pos++; } return round($size, $dec) . " " . $a[$pos]; } /** * 擷取檔案載入資訊 * @param bool $detail 是否顯示詳細 * @return integer|array */ public static function getFile($detail = false)// 擷取檔案載入資訊 { if ($detail) { $files = get_included_files();// 擷取包含檔案資訊 $info = []; foreach ($files as $key => $file) { $info[] = $file . ‘ ( ‘ . number_format(filesize($file) / 1024, 2) . ‘ KB )‘; } return $info;// 檔案大小 } return count(get_included_files()); } /** * 瀏覽器友好的變數輸出 * @param mixed $var 變數 * @param boolean $echo 是否輸出 預設為true 如果為false 則返回輸出字串 * @param string $label 標籤 預設為空白 * @param integer $flags htmlspecialchars flags * @return void|string */ public static function dump($var, $echo = true, $label = null, $flags = ENT_SUBSTITUTE) { $label = (null === $label) ? ‘‘ : rtrim($label) . ‘:‘;// 友好輸出 ob_start(); var_dump($var); $output = ob_get_clean(); $output = preg_replace(‘/\]\=\>\n(\s+)/m‘, ‘] => ‘, $output); if (IS_CLI) { $output = PHP_EOL . $label . $output . PHP_EOL; } else { if (!extension_loaded(‘xdebug‘)) { $output = htmlspecialchars($output, $flags); } $output = ‘<pre>‘ . $label . $output . ‘</pre>‘; } if ($echo) {// 顯示 是否 輸出 echo ($output); return null; } else { return $output; } } public static function inject(Response $response, &$content) { $config = Config::get(‘trace‘);// 擷取配置資訊 $type = isset($config[‘type‘]) ? $config[‘type‘] : ‘Html‘;// 設定 了設定檔 $request = Request::instance();// 請求 類型 $class = false !== strpos($type, ‘\\‘) ? $type : ‘\\think\\debug\\‘ . ucwords($type);// 類型位置 unset($config[‘type‘]);// 刪除配置資訊 if (class_exists($class)) { $trace = new $class($config); } else { throw new ClassNotFoundException(‘class not exists:‘ . $class, $class); } if ($response instanceof Redirect) { //TODO 記錄 } else { $output = $trace->output($response, Log::getLog()); if (is_string($output)) { // trace調試資訊注入 $pos = strripos($content, ‘</body>‘);// 注入 資訊 if (false !== $pos) {// 位置 不為 空 $content = substr($content, 0, $pos) . $output . substr($content, $pos);// 注入到指定位置 } else { $content = $content . $output;// 尾部追加輸出 } } } }}
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1877548
[李景山php]每天TP5-20170101|thinkphp5-Debug.php