Swoole 實現路由功能的Http服務器

 1 <?php
 2 include_once 'error.php';//文件代碼在下方
 3 $http=new \swoole\http\server('0.0.0.0',9502);
 4 $http->set([
 5     'package_max_length'=>1024*1024*10,
 6     'upload_tmp_dir'=>__DIR__.'/upload'
 7 ]);
 8 //監聽http協議
 9 $http->on('request',function ($request,$response){
10     $response->header('Content-Type','text/html');
11     $response->header('Charset','utf-8');
12     $server=$request->server;
13     $path_info=$server['path_info'];
14     if($path_info=='/'){
15         $path_info='/';
16     }else{
17         $path_info=explode('/',$path_info);
18     }
19     if(!is_array($path_info)) {
20         $response->status(404);
21         $response->end('<meta charset="UTF-8">請求路徑無效');
22     }
23     //模塊
24     $model=(isset($path_info[1]) && !empty($path_info[1]))?$path_info[1]:'Swooleman';
25     //控制器
26     $controller=(isset($path_info[2]) && !empty($path_info[2]))?$path_info[2]:'error';
27     //方法
28     $method=(isset($path_info[3]) && !empty($path_info[3]))?$path_info[3]:'index';
29     //結合錯誤處理
30     $class_name="\\{$model}\\$controller";
31     if($class_name == '\favicon.ico\Index'||$class_name == '\favicon.ico\error' ){
32            return ;
33     }
34     //判斷控制器類是否存在
35    if(file_exists(__DIR__.'/../'.str_replace('\\', '/', $class_name).'.php')){
36        require_once __DIR__.'/../'.str_replace('\\', '/', $class_name).'.php';
37        $obj= new $class_name;
38        //判斷控制器方法是否存在
39        if(!method_exists($obj,$method)){
40            $response->status(404);
41            $response->end("<meta charset='UTF-8'>兄Dei,方法不存在,別瞎幾把訪問好嗎");
42        }else{
43            //若是存在此方法,返回結果,return 無效
44            $response->end($obj->$method());
45        }
46    }else{
47        $response->status(404);
48        $response->end("<meta charset='UTF-8'>兄Dei,類不存在,別瞎幾把訪問好嗎");
49    }
50 });
51 //啓動http服務器
52 $http->start();
 1 <?php
 2 
 3 register_shutdown_function('handleFatal');
 4 //進程關閉時觸發
 5 
 6 //發送錯誤信息到某個設備\發送郵件
 7 
 8 
 9 function handleFatal()
10 {
11     $error = error_get_last(); //獲取最後的錯誤
12     if (isset($error['type']))
13     {
14         switch ($error['type'])
15         {
16             case E_ERROR :
17             case E_PARSE :
18             case E_CORE_ERROR :
19             case E_COMPILE_ERROR :
20                 $message = $error['message'];
21                 $file = $error['file'];
22                 $line = $error['line'];
23                 $log = "$message ($file:$line)\nStack trace:\n";
24                 $trace = debug_backtrace();
25                 foreach ($trace as $i => $t)
26                 {
27                     if (!isset($t['file']))
28                     {
29                         $t['file'] = 'unknown';
30                     }
31                     if (!isset($t['line']))
32                     {
33                         $t['line'] = 0;
34                     }
35                     if (!isset($t['function']))
36                     {
37                         $t['function'] = 'unknown';
38                     }
39                     $log .= "#$i {$t['file']}({$t['line']}): ";
40                     if (isset($t['object']) and is_object($t['object']))
41                     {
42                         $log .= get_class($t['object']) . '->';
43                     }
44                     $log .= "{$t['function']}()\n";
45                 }
46                 if (isset($_SERVER['REQUEST_URI']))
47                 {
48                     $log .= '[QUERY] ' . $_SERVER['REQUEST_URI'];
49                 }
50                 file_put_contents(__DIR__.'/log.log',$log);//錯誤日誌的輸出
51                 return $log;
52                 //echo $log;
53             default:
54                 break;
55         }
56     }
57 }
相關文章
相關標籤/搜索