mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +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
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=""}';
|
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
@@ -35,25 +35,26 @@ class FireEvent extends Command
|
|||||||
$idKey = $this->option('idKey');
|
$idKey = $this->option('idKey');
|
||||||
$idKeyOld = $this->option('idKeyOld');
|
$idKeyOld = $this->option('idKeyOld');
|
||||||
$log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld";
|
$log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld";
|
||||||
|
$this->info("$log, begin ...");
|
||||||
if (isset(ModelEventEnum::$eventMaps[$name])) {
|
if (isset(ModelEventEnum::$eventMaps[$name])) {
|
||||||
$eventName = ModelEventEnum::$eventMaps[$name]['event'];
|
$eventName = ModelEventEnum::$eventMaps[$name]['event'];
|
||||||
$model = unserialize(NexusDB::cache_get($idKey));
|
$modelClassName = ModelEventEnum::$eventMaps[$name]['model'];
|
||||||
if ($model instanceof Model) {
|
$modelBasic = new $modelClassName();
|
||||||
$params = [$model];
|
$modelData = unserialize(NexusDB::cache_get($idKey));
|
||||||
if ($idKeyOld) {
|
$useArray = str_ends_with($name, '_deleted');
|
||||||
$modelOld = unserialize(NexusDB::cache_get($idKeyOld));
|
$model = call_user_func_array([$modelBasic, "newInstance"], [$modelData, true]);
|
||||||
if ($modelOld instanceof Model) {
|
//由于 id 不属于 fillable,初始化新对象时是没有值的
|
||||||
$params[] = $modelOld;
|
$model->id = $modelData['id'];
|
||||||
} else {
|
$params = [$useArray ? $modelData: $model];
|
||||||
$log .= ", invalid idKeyOld";
|
if ($idKeyOld) {
|
||||||
}
|
$modelOldData = unserialize(NexusDB::cache_get($idKeyOld));
|
||||||
}
|
$modelOld = call_user_func_array([$modelBasic, "newInstance"], [$modelOldData, true]);
|
||||||
$result = call_user_func_array([$eventName, "dispatch"], $params);
|
$modelOld->id = $modelOldData['id'];
|
||||||
$log .= ", success call dispatch, result: " . var_export($result, true);
|
$params[] = $useArray ? $modelOldData: $modelOld;
|
||||||
publish_model_event($name, $model->id);
|
|
||||||
} else {
|
|
||||||
$log .= ", invalid argument to call, it should be instance of: " . Model::class;
|
|
||||||
}
|
}
|
||||||
|
$result = call_user_func_array([$eventName, "dispatch"], $params);
|
||||||
|
$log .= ", success call dispatch, result: " . var_export($result, true);
|
||||||
|
publish_model_event($name, $model->id);
|
||||||
} else {
|
} else {
|
||||||
$log .= ", no event match this name";
|
$log .= ", no event match this name";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,16 +15,16 @@ class TorrentDeleted
|
|||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||||
|
|
||||||
public ?Model $model = null;
|
public ?array $data = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*
|
*
|
||||||
* @return void
|
* @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;
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||||
|
|
||||||
public ?Model $model = null;
|
public ?array $data = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*
|
*
|
||||||
* @return void
|
* @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
|
* Just a test
|
||||||
*/
|
*/
|
||||||
$torrent = $event->model;
|
$torrent = $event->data;
|
||||||
do_log(sprintf("torrent: %d is deleted, and it's pieces_hash is: %s", $torrent->id, $torrent->pieces_hash));
|
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',
|
'last_action' => 'datetime',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
protected $hidden = [
|
||||||
|
'info_hash',
|
||||||
|
];
|
||||||
|
|
||||||
public static $commentFields = [
|
public static $commentFields = [
|
||||||
'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'promotion_time_type', 'promotion_until', 'pos_state',
|
'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',
|
'hr', 'picktype', 'picktime', 'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size', 'cover', 'anonymous',
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ class TorrentExtra extends NexusModel
|
|||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: function ($value) {
|
get: function ($value) {
|
||||||
|
if (is_null($value)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
$jsonDecoded = json_decode($value, true);
|
$jsonDecoded = json_decode($value, true);
|
||||||
if (is_array($jsonDecoded)) {
|
if (is_array($jsonDecoded)) {
|
||||||
return $jsonDecoded;
|
return $jsonDecoded;
|
||||||
|
|||||||
@@ -6136,7 +6136,7 @@ function build_search_box_category_table($mode, $checkboxValue, $categoryHrefPre
|
|||||||
//Category
|
//Category
|
||||||
$html .= sprintf('<tr><td class="embedded" align="left">%s</td></tr>', nexus_trans('label.search_box.category'));
|
$html .= sprintf('<tr><td class="embedded" align="left">%s</td></tr>', nexus_trans('label.search_box.category'));
|
||||||
/** @var \Illuminate\DataBase\Eloquent\Collection $categoryCollection */
|
/** @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'])) {
|
if (!empty($options['select_unselect'])) {
|
||||||
$categoryCollection->push(new \App\Models\Category(['mode' => -1]));
|
$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:";
|
$prefix = "fire_event:";
|
||||||
$idKey = $prefix . \Illuminate\Support\Str::random();
|
$idKey = $prefix . \Illuminate\Support\Str::random();
|
||||||
$idKeyOld = "";
|
$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) {
|
if ($oldModel) {
|
||||||
$idKeyOld = $prefix . \Illuminate\Support\Str::random();
|
$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);
|
executeCommand("event:fire --name=$name --idKey=$idKey --idKeyOld=$idKeyOld", "string", true, false);
|
||||||
} else {
|
} else {
|
||||||
$eventClass = \App\Enums\ModelEventEnum::$eventMaps[$name]['event'];
|
$eventClass = \App\Enums\ModelEventEnum::$eventMaps[$name]['event'];
|
||||||
$params = [$model];
|
if (str_ends_with($name, '_deleted')) {
|
||||||
if ($oldModel) {
|
//if deleted from database, can not pass model instance, use array
|
||||||
$params[] = $oldModel;
|
$params = [$model->toArray()];
|
||||||
|
if ($oldModel) {
|
||||||
|
$params[] = $oldModel->toArray();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$params = [$model];
|
||||||
|
if ($oldModel) {
|
||||||
|
$params[] = $oldModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
call_user_func_array([$eventClass, "dispatch"], $params);
|
call_user_func_array([$eventClass, "dispatch"], $params);
|
||||||
publish_model_event($name, $model->id);
|
publish_model_event($name, $model->id);
|
||||||
|
|||||||
Reference in New Issue
Block a user