PHP 打印输出调试类

  1 <?php
  2 /**
  3 +------------------------------------------------------------+
  4 代码调试快捷函数
  5 @author: 相望成伤
  6 @qq: 617937424
  7 
  8 Debug::p(1,2,3)
  9 Debug::log(1, 2, 3)
 10 +------------------------------------------------------------+
 11 */
 12 
 13 class Debug {
 14 
 15     //日志文件
 16     public static $file = '';
 17 
 18     /**
 19      * 设置日志文件路径
 20      * @param string $file
 21      * @return mixed|string
 22      */
 23     public static function file($file=''){
 24 
 25         self::$file = empty($file)? sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'debug.txt' : str_replace('\', DIRECTORY_SEPARATOR, $file);
 26         return self::$file;
 27     }
 28 
 29     /**
 30      * 格式化数据
 31      * @param $data
 32      * @return string
 33      */
 34     public static function format($data){
 35         
 36         if(in_array($data, array('TRUE','true', 'false', 'FALSE', 'null', 'NULL'), true )) $data = "'$data'";
 37         if(is_bool($data)) $data = $data? 'true' : 'false';
 38         if(is_null($data)) $data = 'null';
 39         if($data === '') $data = "''";
 40 
 41         if($data instanceof 	hinkModel){ $data->__last_sql__ = $data->getLastSql(); $data = $data->getData();}
 42         if(is_array($data) && current($data) instanceof 	hinkModel){ $data = collection($data)->toArray();}
 43 
 44         if(is_string($data)) $data = self::unicode($data);
 45 
 46         $output = array();
 47 
 48         if(is_string($data) && function_exists($data)){
 49 
 50             $object = new ReflectionFunction($data);
 51             $output['========== FUNC =========='] = array('Function' => $data, 'Namespace' => $object->getNamespaceName(),  'File' => $object->getFilename());
 52         }
 53 
 54         if(is_object($data) || (is_string($data) && class_exists($data, false))){
 55 
 56             $message = '';
 57             if(is_object($data)){
 58 
 59                 if($data instanceof Exception){
 60 
 61                     $file = $data->getFile() . ' (' .$data->getLine() .')';
 62                     $message =  $data->getMessage() . ' (' .$data->getCode() .')';
 63                 }
 64 
 65                 $name = get_class($data);
 66                 $fields = get_object_vars($data);
 67 
 68             }else{
 69                 $name = $data;
 70                 $fields = get_class_vars($data);
 71             }
 72 
 73             $methods = get_class_methods($data);
 74 
 75             $object = new ReflectionClass($data);
 76             if(!isset($file)) $file = $object->getFilename();
 77 
 78             $output['========== CLASS =========='] = array('Class' => $name, 'Namespace' => $object->getNamespaceName(), 'Message' => $message, 'File' => $file, 'Attr' => $fields, 'Method' => $methods);
 79 
 80         }
 81 
 82         if(count($output) == 1)  $output = current($output);
 83         return empty($output)? $data : $output;
 84     }
 85 
 86     /**
 87      * 打印当前输入数据
 88      *
 89      */
 90     public static function input(){
 91         
 92         p("GET:", $_GET);
 93         p("POST:", $_POST);
 94         p("SERVER:", $_SERVER);
 95         p('php://input:', file_get_contents('php://input'));
 96     }
 97 
 98     /**
 99      * unicode 编码
100      * @param $string
101      * @return string|string[]|null
102      */
103     public static function unicode($string) {
104         
105         return preg_replace_callback('/\\u([0-9a-f]{4})/i', function($match){
106             return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
107         }, $string);
108         
109     }
110     
111     /**
112      * 打印数据
113      * $args 参数列表
114      */
115     public static function p($args=''){
116         
117         $args = func_get_args();
118         $count = func_num_args();
119         if($count == 0) $args = array();
120    
121         $format = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? false : true;
122         if($format == true) $format = PHP_SAPI == 'cli'? false : true;
123 
124         $output = ($format)? '<pre style="background:#f3f3f4;padding:5px;border:1px solid #aaa;">' : '';
125         foreach($args as $key => $data){
126             $data = self::format($data);
127             $output .= print_r($data, true);
128 
129             if($key < $count - 1) $output .= $format? '<hr/>' : "
--------------------------------------------------------
";
130         }
131 
132         $output .= $format? '</pre>' : "
";
133         echo $output;
134         
135     }
136 
137     /**
138      * 浏览器控制台打印数据
139      */
140     public static function console(){
141     
142         $output = '';
143         $args = func_get_args();
144         foreach($args as $key => $data) $output .= self::format($data);
145         
146         echo '<script>console.log("';
147         echo preg_replace('/
|
/', '', $output);
148         echo '")</script>';
149         
150     }
151     
152 
153 
154     /**
155      * 写入格式化的日志内容
156      */
157     public static function log($args=''){
158 
159         $args = func_get_args();
160         $count = func_num_args();
161 
162         if(empty(self::$file))self::file();
163         foreach($args as $key => $data){
164 
165             $data = self::format($data);
166             if(!is_string($data)){
167                 $data = var_export($data, true);
168                 $data = preg_replace(array('/(=>)(s+)
s+(array)/'), array('123'), $data);
169             }
170 
171             file_put_contents(self::$file, $data, FILE_APPEND | LOCK_EX);
172             if($key < $count - 1) file_put_contents(self::$file, "
----------------------------------------------------------------------------
", FILE_APPEND | LOCK_EX);
173     
174         }
175     
176         file_put_contents(self::$file, "
==================================================[time ".date('Y-m-d H:i:s')."]==================================================
", FILE_APPEND | LOCK_EX);
177     }
178 
179     /**
180      * 清空日志文件
181      */
182     public static function clear(){
183 
184         if(empty(self::$file)) self::file();
185         file_put_contents(self::$file, '',LOCK_EX);
186     }
187 
188     /**
189      * 写文件
190      */
191     public static function write($args=''){
192 
193         $args = func_get_args();
194         if(empty(self::$file)) self::file();
195 
196         foreach($args as $key => $data){
197             file_put_contents(self::$file, (is_string($data)? $data : var_export($data, true)), FILE_APPEND | LOCK_EX);
198         }
199 
200     }
201 
202     /**
203      * 读取文件内容
204      */
205     public static function read($file=''){
206 
207         if(empty($file)) $file = self::$file;
208         self::p('调试文件内容:', file_get_contents($file));
209     }
210 
211 
212 
213     /**
214      * 其他快捷方法
215      */
216     public static function stop(){ call_user_func_array(array('Debug', 'p'), func_get_args()); exit; }
217     public static function post($key = null){ isset($key)? self::p($_POST[$key]) : self::p($_POST); }
218     public static function get($key = null){ isset($key)? self::p($_GET[$key]) : self::p($_GET);  }
219     public static function sql(){ if(isset($GLOBALS['sql'])) self::p($GLOBALS['sql']); }
220 
221 }
222 
223 
224 if(!function_exists('p')){
225     function p(){ call_user_func_array(array('Debug', 'p'), func_get_args()); }
226 }
227 
228 
229 if(!function_exists('stop')){
230     function stop($args=''){ $args = empty($args)? ['=====> stop <=====']:  func_get_args(); call_user_func_array(array('Debug', 'stop'), $args); }
231 }
232 
233 if(!function_exists('logs')){
234     function logs(){ call_user_func_array(array('Debug', 'log'), func_get_args()); }
235 }
236 
237 if(!function_exists('write')){
238     function write(){ call_user_func_array(array('Debug', 'write'), func_get_args()); }
239 }
240 
241 if(!function_exists('read')){
242     function read(){ call_user_func_array(array('Debug', 'read'), array()); }
243 }
244 
245 if(!function_exists('get_debug_file')){
246     function get_debug_file(){ Debug::p('调试文件:' . Debug::$file); }
247 }
248 
249 
250 if(!function_exists('clear')){
251     function clear(){ Debug::clear(); }
252 }
原文地址:https://www.cnblogs.com/zbseoag/p/12717223.html