审查视图

1  
邓超 authored
1 2 3 4
<?php

namespace Lib;
1  
邓超 authored
5 6 7 8 9 10
/**
 * @author:dc
 * @time 2023/2/13 15:07
 * Class App
 * @package Lib
 */
1  
邓超 authored
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
class App {

    /**
     * @var App
     */
    public static App $instance;

    /**
     * 当前日期
     * @var string
     */
    private string $date = '';

    /**
     * 当前时间
     * @var string
     */
    private string $dateTime = '';

    /**
     * @var Route
     */
    private Route $route;

    /**
     * 请求的参数
     * @var array
     */
    private array $request = [];
1  
邓超 authored
41 42 43 44 45

    /**
     * TODO:: 如果debug打开,错误时会返回错误消息到前端
     * @var bool
     */
1  
邓超 authored
46
    public bool $debug = false;
1  
邓超 authored
47
1  
邓超 authored
48 49
    /**
     * 输出到前端的数据
1  
邓超 authored
50 51
     * @var mixed
     */
1  
邓超 authored
52
    private mixed $data = [];
1  
邓超 authored
53 54 55 56 57 58 59 60 61

    /**
     * 表单文件
     * @var array
     */
    private array $uploadFile = [];

    /**
     * 错误
1  
邓超 authored
62 63
     * @var array
     */
1  
邓超 authored
64
    private array $error = [];
1  
邓超 authored
65
1  
邓超 authored
66 67 68
    /**
     * App constructor.
     */
1  
邓超 authored
69 70 71
    public function __construct()
    {
1  
邓超 authored
72 73
        register_shutdown_function("\\Lib\\App::end");
1  
邓超 authored
74 75 76 77 78 79 80 81
        $this->date = date('Y-m-d');
        $this->dateTime = date('Y-m-d H:i:s');


        // 路由
        $this->route = new Route();

        // 请求参数 TODO::不允许其他类型的请求参数
x  
邓超 authored
82
        $this->request = my_filter($_REQUEST,['trim']);
1  
邓超 authored
83
1  
邓超 authored
84 85 86 87
        // 调试
        if(defined('APP_DEBUG')){
            $this->debug = boolval(APP_DEBUG);
        }
1  
邓超 authored
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    }


    /**
     * @return App
     * @author:dc
     * @time 2023/2/13 10:37
     */
    public static function instance(){
        if(empty(static::$instance)){
            static::$instance = new App();
        }
        return static::$instance;
    }


    /**
     * 开始运行
     * @author:dc
     * @time 2023/2/13 11:15
     */
    public static function run() {
        $app = self::instance();
        try {
1  
邓超 authored
113 114 115
//            if ($_SERVER['REQUEST_METHOD'] != 'POST'){
//                $app->e('need_post_request');
//            }
1  
邓超 authored
116 117

            // 取到路由 控制器
1  
邓超 authored
118
            $route = $app->route->get(explode('?',$_SERVER['REQUEST_URI'])[0]);
1  
邓超 authored
119 120 121
            if(!$route){
                $app->e('route_not_found');
            }
1  
邓超 authored
122
1  
邓超 authored
123 124
            list($class,$action) = $route;
            $controller = new $class();
1  
邓超 authored
125 126 127

            $app->data = $controller->{$action}();
1  
邓超 authored
128 129 130 131 132
            // end 控制器


        }catch (\Throwable $exception){
1  
邓超 authored
133 134 135 136 137 138 139 140 141 142 143
            if($exception instanceof Err){

                if ($exception->getCode() !== 200){
                    $app->data = [
                        'error_message' => $exception->getMessage(),
                        'status'    =>  $exception->getCode() ? $exception->getCode() : 500,
                    ];
                }

            }else{
1  
邓超 authored
144 145 146 147 148 149
                $app->error = [
                    'message' => $exception->getMessage(),
                    'file' => $exception->getFile(),
                    'line' => $exception->getLine(),
                    'traceAsString' =>  $exception->getTraceAsString()
                ];
1  
邓超 authored
150 151 152

                // 非 Err 错误类型
                $app->data = [
1  
邓超 authored
153
                    'error_message' => $app->debug ? $exception->getMessage().PHP_EOL.$exception->getTraceAsString() : __('server_error'),
1  
邓超 authored
154 155
                    'status'    =>  $exception->getCode() ? $exception->getCode() : 500,
                ];
1  
邓超 authored
156
            }
1  
邓超 authored
157
1  
邓超 authored
158 159 160 161 162 163 164 165
        }

    }

    /**
     * 请求的参数
     * @param string $name
     * @param null $default
1  
邓超 authored
166
     * @param array|string|null $filter
1  
邓超 authored
167 168
     * @return array|false|float|int|mixed|null
     * @author:dc
1  
邓超 authored
169
     * @time 2023/2/17 9:37
1  
邓超 authored
170
     */
1  
邓超 authored
171 172 173 174 175 176 177
    public function request($name='*',$default=null,$filter = null){
        $data = [];
        if($name === '*'){
            $data = $this->request;
        } else if (is_string($name)){
            $data = $this->request[$name]??$default;
        }else if(is_array($name)){
1  
邓超 authored
178 179 180 181
            foreach ($this->request as $key=>$value){
                if(in_array($key,$name)){
                    $data[$key] = $value;
                }
1  
邓超 authored
182
            }
1  
邓超 authored
183 184 185 186 187
        }
        if($filter){
            $data = my_filter($data,$filter);
        }
        if($data !== []){
1  
邓超 authored
188
            return $data;
1  
邓超 authored
189
        }
1  
邓超 authored
190
        return null;
1  
邓超 authored
191 192
    }
x  
邓超 authored
193
    /**
x  
邓超 authored
194 195 196 197 198 199 200 201 202 203 204
     * 读取数组
     * @param string $name
     * @param array $default
     * @param null $filter
     * @return array|float|int|mixed|string[]
     * @author:dc
     * @time 2024/8/6 9:36
     */
    public function requestArr($name='*',array $default = [], $filter = null){
        $value = $this->request($name,$default, $filter);
        if(!is_array($value)){
x  
邓超 authored
205
            $value = explode(',',(string) $value);
x  
邓超 authored
206 207 208 209 210 211 212 213 214 215 216 217 218
        }
        foreach ($value as $k=>$v){
            if($v===''){
                unset($value[$k]);
            }
        }
        if(!$value){
            return $default;
        }
        return $value;
    }

    /**
x  
邓超 authored
219 220 221 222 223 224 225 226 227 228
     * 是否存在
     * @param string $name
     * @return bool
     * @author:dc
     * @time 2023/4/15 16:43
     */
    public function requestHas(string $name):bool {
        return isset($this->request[$name]);
    }
1  
邓超 authored
229 230

    /**
1  
邓超 authored
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
     * @param string $key
     * @return false|UploadFile[]
     * @author:dc
     * @time 2023/3/13 15:09
     */
    public function file($key='file'){
        if(!empty($this->uploadFile[$key])){
            return $this->uploadFile[$key];
        }
        if(empty($_FILES[$key])){
            return false;
        }

        if (is_array($_FILES[$key]['error'])){
            foreach ($_FILES[$key]['error'] as $k=>$e){
                // 成功的才处理
                if($e===0){
                    $this->uploadFile[$key][] = new UploadFile($_FILES[$key]['name'][$k],$_FILES[$key]['tmp_name'][$k]);
                }
            }
        }else if($_FILES[$key]['error']===0){
            $this->uploadFile[$key][] = new UploadFile($_FILES[$key]['name'],$_FILES[$key]['tmp_name']);
        }

        return $this->uploadFile[$key]??false;

    }




    /**
1  
邓超 authored
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
     * @return string
     * @author:dc
     * @time 2023/2/13 11:17
     */
    public function nowDate():string {
        return $this->date;
    }

    /**
     * @return string
     * @author:dc
     * @time 2023/2/13 11:18
     */
    public function nowDateTime():string{
        return $this->dateTime;
    }

    /**
     * 错误
1  
邓超 authored
282
     * @param string|array $message
1  
邓超 authored
283 284 285 286 287 288
     * @param int $status
     * @throws Err
     * @author:dc
     * @time 2023/2/13 10:57
     */
    public function e($message,$status=400){
1  
邓超 authored
289 290
        if(is_array($message)){
            $this->data['error_message'] = __($message[0]);
1  
邓超 authored
291 292 293 294 295 296
            if(is_array($message[1])){
                $this->data['error_message'] = sprintf($this->data['error_message'],...$message[1]);
            }else{
                unset($message[0]);
                $this->data['error_message'] = sprintf($this->data['error_message'],...$message);
            }
1  
邓超 authored
297 298 299
        }else{
            $this->data['error_message'] = __($message);
        }
1  
邓超 authored
300
        $this->data['status'] = $status;
1  
邓超 authored
301
        throw new Err($this->data['error_message'],$status);
1  
邓超 authored
302 303 304 305 306 307 308 309 310 311
    }

    /**
     * 成功消息
     * @param $data
     * @param string $message
     * @throws Err
     * @author:dc
     * @time 2023/2/13 11:03
     */
1  
邓超 authored
312
    public function _json($data){
1  
邓超 authored
313 314
        $this->data = $data;
        throw new Err('',200);
1  
邓超 authored
315 316 317
    }

1  
邓超 authored
318 319 320 321 322 323
    /**
     * @param $data
     * @param int $http_code
     * @author:dc
     * @time 2023/3/27 10:53
     */
1  
邓超 authored
324 325
    public static function echo($data, $http_code = 200){
1  
邓超 authored
326 327 328 329 330

        if(php_sapi_name()=='cli'){
            return $data;
        }
1  
邓超 authored
331 332 333
        http_response_code($http_code);

        if(is_array($data)){
1  
邓超 authored
334
            @header("Content-Type:application/json;Charset=UTF-8;");
1  
邓超 authored
335 336
            echo json_encode($data,JSON_UNESCAPED_UNICODE);
        }else{
1  
邓超 authored
337
            @header("Content-Type:text/html;Charset=UTF-8;");
1  
邓超 authored
338 339 340 341
            echo $data;
        }
    }
1  
邓超 authored
342 343 344 345
    public function getError(){
        return $this->error;
    }
1  
邓超 authored
346 347 348 349 350 351 352 353
    /**
     * 结束
     * @author:dc
     * @time 2023/2/13 10:54
     */
    public static function end()
    {
1  
邓超 authored
354
        $app = self::instance();
1  
邓超 authored
355
1  
邓超 authored
356
        // 记录日志
x  
邓超 authored
357 358
        $last_error = error_get_last();
        if($last_error){
1  
邓超 authored
359
            logs($last_error);
1  
邓超 authored
360
x  
邓超 authored
361 362 363
            if($last_error == E_ERROR){
                $data['error_message'] = $last_error['message'];
                $data['status'] = 502;
1  
邓超 authored
364
x  
邓超 authored
365 366 367
                if($app->debug){
                    $data['debug']  =   $last_error;
                }
1  
邓超 authored
368
x  
邓超 authored
369 370 371
                self::echo($data,502);

                return true;
1  
邓超 authored
372
            }
1  
邓超 authored
373
1  
邓超 authored
374
        }
1  
邓超 authored
375
x  
邓超 authored
376 377 378 379 380 381 382 383

        if($app->getError()){
            logs($app->getError());
        }

        self::echo($app->data);

1  
邓超 authored
384 385
        // 日志记录
        Log::getInstance()->write();
1  
邓超 authored
386 387 388

//        header("Content-Type:text/event-stream;Charset=UTF-8;");
1  
邓超 authored
389 390
//        ob_flush();
//        ob_clean();
1  
邓超 authored
391 392 393 394 395 396
    }




}