<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace app\bootstrap;
use Webman\Bootstrap;
//use support\Db;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use support\think\Db;
class SqlDebug implements Bootstrap
{
    /**
     * 自定义输出格式，否则输出前面会带有当前文件，无用信息
     * @param $var
     * @return void
     */
    public static function dumpvar($var): void
    {
        $cloner = new VarCloner();
        $dumper = new CliDumper();
        $dumper->dump($cloner->cloneVar($var));
    }

    public static function start($worker)
    {
        // Is it console environment ?
        $is_console = !$worker;
        // if ($is_console) {
        //     return;
        // }
        if (!Config("app.debug")) return;
        
        Db::listen(function($sql, $runtime, $master) {
            if (!Config("app.debug")) return;
            if($sql!='select 1' && $sql){
                $sql= preg_replace('/db\.[db\.]+/', 'db.', $sql);
                \support\Log::alert('['.$runtime.']'.$sql);
            }
        });
    }
}