PHP 打印输出调试类

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

 

posted @ 2020-04-17 02:15  心随所遇  阅读(971)  评论(0编辑  收藏  举报