2024-02-23 02:38:42 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2024-04-03 04:05:04 +08:00
|
|
|
use App\Events\NewsCreated;
|
2024-02-23 02:38:42 +08:00
|
|
|
use App\Events\TorrentCreated;
|
2024-04-25 02:15:56 +08:00
|
|
|
use App\Events\TorrentDeleted;
|
|
|
|
|
use App\Events\TorrentUpdated;
|
2024-03-16 15:26:12 +08:00
|
|
|
use App\Events\UserDestroyed;
|
|
|
|
|
use App\Events\UserDisabled;
|
|
|
|
|
use App\Events\UserEnabled;
|
2024-04-25 02:15:56 +08:00
|
|
|
use App\Models\News;
|
|
|
|
|
use App\Models\Torrent;
|
|
|
|
|
use App\Models\User;
|
2024-02-23 02:38:42 +08:00
|
|
|
use Illuminate\Console\Command;
|
2024-04-25 02:15:56 +08:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-04-26 03:21:35 +08:00
|
|
|
use Nexus\Database\NexusDB;
|
|
|
|
|
use Symfony\Component\Console\Command\Command as CommandAlias;
|
2024-02-23 02:38:42 +08:00
|
|
|
|
|
|
|
|
class FireEvent extends Command
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The name and signature of the console command.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2024-04-26 03:21:35 +08:00
|
|
|
protected $signature = 'event:fire {--name=} {--idKey=} {--idKeyOld=""}';
|
2024-02-23 02:38:42 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The console command description.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2024-04-26 03:21:35 +08:00
|
|
|
protected $description = 'Fire a event, options: --name, --idKey --idKeyOld';
|
2024-02-23 02:38:42 +08:00
|
|
|
|
|
|
|
|
protected array $eventMaps = [
|
2024-04-25 02:15:56 +08:00
|
|
|
"torrent_created" => ['event' => TorrentCreated::class, 'model' => Torrent::class],
|
|
|
|
|
"torrent_updated" => ['event' => TorrentUpdated::class, 'model' => Torrent::class],
|
|
|
|
|
"torrent_deleted" => ['event' => TorrentDeleted::class, 'model' => Torrent::class],
|
|
|
|
|
"user_destroyed" => ['event' => UserDestroyed::class, 'model' => User::class],
|
|
|
|
|
"user_disabled" => ['event' => UserDisabled::class, 'model' => User::class],
|
|
|
|
|
"user_enabled" => ['event' => UserEnabled::class, 'model' => User::class],
|
|
|
|
|
"news_created" => ['event' => NewsCreated::class, 'model' => News::class],
|
2024-02-23 02:38:42 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the console command.
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
|
|
|
|
$name = $this->option('name');
|
2024-04-26 03:21:35 +08:00
|
|
|
$idKey = $this->option('idKey');
|
|
|
|
|
$idKeyOld = $this->option('idKeyOld');
|
|
|
|
|
$log = "FireEvent, name: $name, idKey: $idKey, idKeyOld: $idKeyOld";
|
2024-02-23 02:38:42 +08:00
|
|
|
if (isset($this->eventMaps[$name])) {
|
2024-04-25 02:15:56 +08:00
|
|
|
$eventName = $this->eventMaps[$name]['event'];
|
2024-04-26 03:21:35 +08:00
|
|
|
$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);
|
|
|
|
|
} else {
|
|
|
|
|
$log .= ", invalid argument to call, it should be instance of: " . Model::class;
|
|
|
|
|
}
|
2024-02-23 02:38:42 +08:00
|
|
|
} else {
|
2024-04-26 03:21:35 +08:00
|
|
|
$log .= ", no event match this name";
|
2024-02-23 02:38:42 +08:00
|
|
|
}
|
2024-04-26 03:21:35 +08:00
|
|
|
$this->info($log);
|
|
|
|
|
do_log($log);
|
|
|
|
|
return CommandAlias::SUCCESS;
|
2024-02-23 02:38:42 +08:00
|
|
|
}
|
|
|
|
|
}
|