mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-20 09:30:49 +08:00
deleted event param use array instead of model
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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']));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -6136,7 +6136,7 @@ function build_search_box_category_table($mode, $checkboxValue, $categoryHrefPre
|
||||
//Category
|
||||
$html .= sprintf('<tr><td class="embedded" align="left">%s</td></tr>', nexus_trans('label.search_box.category'));
|
||||
/** @var \Illuminate\DataBase\Eloquent\Collection $categoryCollection */
|
||||
$categoryCollection = $searchBox->categories()->orderBy('sort_index', 'desc')->get();
|
||||
$categoryCollection = $searchBox->categories()->with('icon')->orderBy('sort_index', 'desc')->get();
|
||||
if (!empty($options['select_unselect'])) {
|
||||
$categoryCollection->push(new \App\Models\Category(['mode' => -1]));
|
||||
}
|
||||
|
||||
@@ -1340,17 +1340,25 @@ function fire_event(string $name, \Illuminate\Database\Eloquent\Model $model, ?\
|
||||
$prefix = "fire_event:";
|
||||
$idKey = $prefix . \Illuminate\Support\Str::random();
|
||||
$idKeyOld = "";
|
||||
\Nexus\Database\NexusDB::cache_put($idKey, serialize($model), 3600*24*30);
|
||||
\Nexus\Database\NexusDB::cache_put($idKey, serialize($model->toArray()), 3600*24*30);
|
||||
if ($oldModel) {
|
||||
$idKeyOld = $prefix . \Illuminate\Support\Str::random();
|
||||
\Nexus\Database\NexusDB::cache_put($idKeyOld, serialize($oldModel), 3600*24*30);
|
||||
\Nexus\Database\NexusDB::cache_put($idKeyOld, serialize($oldModel->toArray()), 3600*24*30);
|
||||
}
|
||||
executeCommand("event:fire --name=$name --idKey=$idKey --idKeyOld=$idKeyOld", "string", true, false);
|
||||
} else {
|
||||
$eventClass = \App\Enums\ModelEventEnum::$eventMaps[$name]['event'];
|
||||
$params = [$model];
|
||||
if ($oldModel) {
|
||||
$params[] = $oldModel;
|
||||
if (str_ends_with($name, '_deleted')) {
|
||||
//if deleted from database, can not pass model instance, use array
|
||||
$params = [$model->toArray()];
|
||||
if ($oldModel) {
|
||||
$params[] = $oldModel->toArray();
|
||||
}
|
||||
} else {
|
||||
$params = [$model];
|
||||
if ($oldModel) {
|
||||
$params[] = $oldModel;
|
||||
}
|
||||
}
|
||||
call_user_func_array([$eventClass, "dispatch"], $params);
|
||||
publish_model_event($name, $model->id);
|
||||
|
||||
Reference in New Issue
Block a user