Files
nexusphp/app/Console/Commands/FireEvent.php

83 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
2024-04-03 04:05:04 +08:00
use App\Events\NewsCreated;
use App\Events\TorrentCreated;
2024-04-25 02:15:56 +08:00
use App\Events\TorrentDeleted;
use App\Events\TorrentUpdated;
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;
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;
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=""}';
/**
* The console command description.
*
* @var string
*/
2024-04-26 03:21:35 +08:00
protected $description = 'Fire a event, options: --name, --idKey --idKeyOld';
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],
];
/**
* 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";
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;
}
} else {
2024-04-26 03:21:35 +08:00
$log .= ", no event match this name";
}
2024-04-26 03:21:35 +08:00
$this->info($log);
do_log($log);
return CommandAlias::SUCCESS;
}
}