<?php

namespace App\Listeners;

use App\Events\ModelChangedEvent;
use App\Services\LogService;

class ModelChangedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
    }

    public function handle(ModelChangedEvent $event)
    {
        $traces=json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),6,config('exception.log.traceDepth.modelChange')));
        preg_match('/Model.php.*?function\"\:\"(.*?)\"/', $traces, $result);
        $methodName = $result[1]??'';
        LogService::log(
            get_class($event->model),
            $methodName,
            '对象：'.$event->model->toJson()
            .'调用堆栈h：'.$traces
        );
    }

}
