<?php


namespace App;

use App\Services\LogService;
use Illuminate\Database\Eloquent\Model;

class ModelExtended extends Model
{
    protected $isNotLogging=[
        Log::class
    ];
    private $isShouldLog=null;
    protected function log($input=''):bool{
        if($this->isShouldLog===null)
            $this->isShouldLog
                =array_search(get_class($this),$this->isNotLogging)===false;
        if(!$this->isShouldLog)return false;
        $traces=json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,5));
        preg_match('/\"function\"\:\".*?"function\"\:\"(.*?)\"\,\"class\"\:\".*?\"/', $traces, $result);
        $methodName = $result[1]??'';
        LogService::log(get_class($this),$methodName,
            '对象：'.$this->toJson()
            .'参数：'.json_encode([$input])
            .'调用堆栈：'.$traces
        );

        return $this->isShouldLog;
    }

    function save(array $options = [])
    {
        $this->log($options);
        return parent::save($options); // TODO: Change the autogenerated stub
    }
    function update(array $attributes = [], array $options = [])
    {
        $this->log([$attributes, $options]);
        return parent::update($attributes, $options); // TODO: Change the autogenerated stub
    }
    public function delete()
    {
        $this->log();
        return parent::delete(); // TODO: Change the autogenerated stub
    }
}
