deleted event param use array instead of model

This commit is contained in:
xiaomlove
2025-05-16 22:01:12 +07:00
parent 45c41047da
commit 1381390c8c
8 changed files with 47 additions and 31 deletions

View File

@@ -15,7 +15,7 @@ class FireEvent extends Command
*
* @var string
*/
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=""}';
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=}';
/**
* The console command description.
@@ -35,25 +35,26 @@ class FireEvent extends Command
$idKey = $this->option('idKey');
$idKeyOld = $this->option('idKeyOld');
$log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld";
$this->info("$log, begin ...");
if (isset(ModelEventEnum::$eventMaps[$name])) {
$eventName = ModelEventEnum::$eventMaps[$name]['event'];
$model = unserialize(NexusDB::cache_get($idKey));
if ($model instanceof Model) {
$params = [$model];
if ($idKeyOld) {
$modelOld = unserialize(NexusDB::cache_get($idKeyOld));
if ($modelOld instanceof Model) {
$params[] = $modelOld;
} else {
$log .= ", invalid idKeyOld";
}
}
$result = call_user_func_array([$eventName, "dispatch"], $params);
$log .= ", success call dispatch, result: " . var_export($result, true);
publish_model_event($name, $model->id);
} else {
$log .= ", invalid argument to call, it should be instance of: " . Model::class;
$modelClassName = ModelEventEnum::$eventMaps[$name]['model'];
$modelBasic = new $modelClassName();
$modelData = unserialize(NexusDB::cache_get($idKey));
$useArray = str_ends_with($name, '_deleted');
$model = call_user_func_array([$modelBasic, "newInstance"], [$modelData, true]);
//由于 id 不属于 fillable初始化新对象时是没有值的
$model->id = $modelData['id'];
$params = [$useArray ? $modelData: $model];
if ($idKeyOld) {
$modelOldData = unserialize(NexusDB::cache_get($idKeyOld));
$modelOld = call_user_func_array([$modelBasic, "newInstance"], [$modelOldData, true]);
$modelOld->id = $modelOldData['id'];
$params[] = $useArray ? $modelOldData: $modelOld;
}
$result = call_user_func_array([$eventName, "dispatch"], $params);
$log .= ", success call dispatch, result: " . var_export($result, true);
publish_model_event($name, $model->id);
} else {
$log .= ", no event match this name";
}

View File

@@ -15,16 +15,16 @@ class TorrentDeleted
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?Model $model = null;
public ?array $data = null;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Model $model)
public function __construct(array $data)
{
$this->model = $model;
$this->data = $data;
}
/**

View File

@@ -15,16 +15,16 @@ class UserDeleted
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?Model $model = null;
public ?array $data = null;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Model $model)
public function __construct(array $data)
{
$this->model = $model;
$this->data = $data;
}
/**

View File

@@ -28,7 +28,7 @@ class DeductUserBonusWhenTorrentDeleted implements ShouldQueue
/**
* Just a test
*/
$torrent = $event->model;
do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent->id, $torrent->pieces_hash));
$torrent = $event->data;
do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent['id'], $torrent['pieces_hash']));
}
}

View File

@@ -31,6 +31,10 @@ class Torrent extends NexusModel
'last_action' => 'datetime',
];
protected $hidden = [
'info_hash',
];
public static $commentFields = [
'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'promotion_time_type', 'promotion_until', 'pos_state',
'hr', 'picktype', 'picktime', 'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous',

View File

@@ -16,6 +16,9 @@ class TorrentExtra extends NexusModel
{
return Attribute::make(
get: function ($value) {
if (is_null($value)) {
return null;
}
$jsonDecoded = json_decode($value, true);
if (is_array($jsonDecoded)) {
return $jsonDecoded;