<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //添加自定义辅助函数
        require_once __DIR__.'/../helpers.php';

    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //把SQL输出到日志中
        if ($this->app->environment('local')) { // 仅在本地环境启用
            DB::listen(function ($query) {
                // 格式化 SQL 语句
                $sql = $query->sql;
                foreach ($query->bindings as $binding) {
                    $sql = preg_replace('/\?/', "'" . addslashes($binding) . "'", $sql, 1);
                }
                // 输出到控制台
                //dump($sql . ' [' . $query->time . 'ms]');
                //输出到log
                if ($query->time > 1000) {
                    Log::error($sql . ' [' . $query->time . 'ms]');
                } else {
                    Log::info($sql . ' [' . $query->time . 'ms]');
                }
            });

        }
        //


    }
}
