-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncExecutor.php
28 lines (25 loc) · 939 Bytes
/
AsyncExecutor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace Jibix\AsyncMedoo;
use Closure;
use Jibix\AsyncMedoo\task\AsyncExecuteTask;
use pocketmine\Server;
use ReflectionFunction;
use function Jibix\AsyncMedoo\util\async;
/**
* Class AsyncExecutor
* @package Jibix\AsyncMedoo
* @author Jibix
* @date 26.05.2023 - 00:55
* @project AsyncMedoo
*/
final class AsyncExecutor{
public static function execute(Closure $task, ?Closure $onComplete = null, ?Closure $onError = null, ?MySQLCredentials $credentials = null): void{
$credentials ??= AsyncMedoo::getCredentials();
if (!Server::getInstance()->isRunning()) {
$result = (new ReflectionFunction($task))->getNumberOfParameters() == 0 ? ($task)() : ($task)($credentials->createConnection());
if ($onComplete !== null && !is_object($result)) ($onComplete)($result);
return;
}
async(new AsyncExecuteTask($credentials, $task, $onComplete, $onError));
}
}