A boot/booted/unplug options

This commit is contained in:
a6a2f5842 2025-12-17 21:34:20 +01:00
parent 38e841c13a
commit 72c8bf4ac8
1 changed files with 43 additions and 5 deletions

View File

@ -32,6 +32,30 @@ class Controller
} }
} }
/**
* To be overridden by child classes if needed
* Called before need_auth check
*
* @return void
*/
public function boot(): void {}
/**
* To be overridden by child classes if needed
* Called after need_auth check
*
* @return void
*/
public function booted(): void {}
/**
* To be overridden by child classes if needed
* Called after main function execution (even if not found)
*
* @return void
*/
public function unboot(): void {}
public static function controll_message( public static function controll_message(
ConnectionInterface $connection, ConnectionInterface $connection,
PrivateChannel|Channel|PresenceChannel $channel, PrivateChannel|Channel|PresenceChannel $channel,
@ -60,10 +84,6 @@ class Controller
return self::send_error($connection, $message, 'Event could not be associated'); return self::send_error($connection, $message, 'Event could not be associated');
} }
if (! method_exists($controllerClass, $method)) {
return self::send_error($connection, $message, 'Event could not be handled');
}
$controller = new $controllerClass( $controller = new $controllerClass(
$connection, $connection,
$channel, $channel,
@ -71,16 +91,34 @@ class Controller
$channelManager $channelManager
); );
$controller->boot();
if (($controller->need_auth ?? true) && ! $connection->user) { if (($controller->need_auth ?? true) && ! $connection->user) {
return $controller->error('Unauthorized'); $e = $controller->error('Unauthorized');
$controller->unboot();
return $e;
} }
if (! method_exists($controllerClass, $method)) {
$e = self::send_error($connection, $message, 'Event could not be handled');
$controller->unboot();
return $e;
}
$controller->booted();
$payload = $controller->$method( $payload = $controller->$method(
$connection, $connection,
@$message['data'] ?? [], @$message['data'] ?? [],
$message['channel'] $message['channel']
); );
$controller->unboot();
if ($payload === false || $payload === true) { if ($payload === false || $payload === true) {
return null; return null;
} }