mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
BulkCurlTask now accepts a closure callback for onCompletion
this requires much less boilerplate code than an anonymous class.
This commit is contained in:
parent
dcf53bd06d
commit
b3c6c11b20
@ -32,6 +32,7 @@ use pocketmine\scheduler\BulkCurlTask;
|
||||
use pocketmine\scheduler\BulkCurlTaskOperation;
|
||||
use pocketmine\timings\TimingsHandler;
|
||||
use pocketmine\utils\InternetException;
|
||||
use pocketmine\utils\InternetRequestResult;
|
||||
use function count;
|
||||
use function fclose;
|
||||
use function file_exists;
|
||||
@ -128,45 +129,28 @@ class TimingsCommand extends VanillaCommand{
|
||||
|
||||
$host = $sender->getServer()->getConfigGroup()->getProperty("timings.host", "timings.pmmp.io");
|
||||
|
||||
$sender->getServer()->getAsyncPool()->submitTask(new class($sender, $host, $agent, $data) extends BulkCurlTask{
|
||||
private const TLS_KEY_SENDER = "sender";
|
||||
|
||||
/** @var string */
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* @param string[] $data
|
||||
* @phpstan-param array<string, string> $data
|
||||
*/
|
||||
public function __construct(CommandSender $sender, string $host, string $agent, array $data){
|
||||
parent::__construct([
|
||||
new BulkCurlTaskOperation(
|
||||
"https://$host?upload=true",
|
||||
10,
|
||||
[],
|
||||
[
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"User-Agent: $agent",
|
||||
"Content-Type: application/x-www-form-urlencoded"
|
||||
],
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => http_build_query($data),
|
||||
CURLOPT_AUTOREFERER => false,
|
||||
CURLOPT_FOLLOWLOCATION => false
|
||||
]
|
||||
)
|
||||
]);
|
||||
$this->host = $host;
|
||||
$this->storeLocal(self::TLS_KEY_SENDER, $sender);
|
||||
}
|
||||
|
||||
public function onCompletion() : void{
|
||||
/** @var CommandSender $sender */
|
||||
$sender = $this->fetchLocal(self::TLS_KEY_SENDER);
|
||||
$sender->getServer()->getAsyncPool()->submitTask(new BulkCurlTask(
|
||||
[new BulkCurlTaskOperation(
|
||||
"https://$host?upload=true",
|
||||
10,
|
||||
[],
|
||||
[
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"User-Agent: $agent",
|
||||
"Content-Type: application/x-www-form-urlencoded"
|
||||
],
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => http_build_query($data),
|
||||
CURLOPT_AUTOREFERER => false,
|
||||
CURLOPT_FOLLOWLOCATION => false
|
||||
]
|
||||
)],
|
||||
function(array $results) use ($sender, $host) : void{
|
||||
/** @phpstan-var array<InternetRequestResult|InternetException> $results */
|
||||
if($sender instanceof Player and !$sender->isOnline()){ // TODO replace with a more generic API method for checking availability of CommandSender
|
||||
return;
|
||||
}
|
||||
$result = $this->getResult()[0];
|
||||
$result = $results[0];
|
||||
if($result instanceof InternetException){
|
||||
$sender->getServer()->getLogger()->logException($result);
|
||||
return;
|
||||
@ -174,12 +158,12 @@ class TimingsCommand extends VanillaCommand{
|
||||
$response = json_decode($result->getBody(), true);
|
||||
if(is_array($response) && isset($response["id"])){
|
||||
Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.timingsRead",
|
||||
["https://" . $this->host . "/?id=" . $response["id"]]));
|
||||
["https://" . $host . "/?id=" . $response["id"]]));
|
||||
}else{
|
||||
Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.pasteError"));
|
||||
}
|
||||
}
|
||||
});
|
||||
));
|
||||
}else{
|
||||
fclose($fileTimings);
|
||||
Command::broadcastCommandMessage($sender, new TranslationContainer("pocketmine.command.timings.timingsWrite", [$timings]));
|
||||
|
@ -25,6 +25,7 @@ namespace pocketmine\scheduler;
|
||||
|
||||
use pocketmine\utils\Internet;
|
||||
use pocketmine\utils\InternetException;
|
||||
use pocketmine\utils\InternetRequestResult;
|
||||
use function igbinary_serialize;
|
||||
use function igbinary_unserialize;
|
||||
|
||||
@ -34,6 +35,8 @@ use function igbinary_unserialize;
|
||||
* The result of this AsyncTask is an array of arrays (returned from {@link Internet::simpleCurl}) or InternetException objects.
|
||||
*/
|
||||
class BulkCurlTask extends AsyncTask{
|
||||
private const TLS_KEY_COMPLETION_CALLBACK = "completionCallback";
|
||||
|
||||
/** @var string */
|
||||
private $operations;
|
||||
|
||||
@ -44,11 +47,12 @@ class BulkCurlTask extends AsyncTask{
|
||||
* "timeout", "extraHeaders" and "extraOpts". Documentation of these options are same as those in
|
||||
* {@link Internet::simpleCurl}.
|
||||
*
|
||||
* @param BulkCurlTaskOperation[] $operations
|
||||
* @phpstan-param list<BulkCurlTaskOperation> $operations
|
||||
* @param BulkCurlTaskOperation[] $operations
|
||||
* @phpstan-param \Closure(list<InternetRequestResult|InternetException> $results) : void $onCompletion
|
||||
*/
|
||||
public function __construct(array $operations){
|
||||
public function __construct(array $operations, \Closure $onCompletion){
|
||||
$this->operations = igbinary_serialize($operations);
|
||||
$this->storeLocal(self::TLS_KEY_COMPLETION_CALLBACK, $onCompletion);
|
||||
}
|
||||
|
||||
public function onRun() : void{
|
||||
@ -67,4 +71,15 @@ class BulkCurlTask extends AsyncTask{
|
||||
}
|
||||
$this->setResult($results);
|
||||
}
|
||||
|
||||
public function onCompletion() : void{
|
||||
/**
|
||||
* @var \Closure
|
||||
* @phpstan-var \Closure(list<InternetRequestResult|InternetException>) : void
|
||||
*/
|
||||
$callback = $this->fetchLocal(self::TLS_KEY_COMPLETION_CALLBACK);
|
||||
/** @var InternetRequestResult[]|InternetException[] $results */
|
||||
$results = $this->getResult();
|
||||
$callback($results);
|
||||
}
|
||||
}
|
||||
|
@ -96,12 +96,7 @@ parameters:
|
||||
path: ../../../src/command/CommandReader.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#2 \\$host of class class@anonymous/src/command/defaults/TimingsCommand\\.php\\:131 constructor expects string, mixed given\\.$#"
|
||||
count: 1
|
||||
path: ../../../src/command/defaults/TimingsCommand.php
|
||||
|
||||
-
|
||||
message: "#^Cannot access offset 0 on mixed\\.$#"
|
||||
message: "#^Part \\$host \\(mixed\\) of encapsed string cannot be cast to string\\.$#"
|
||||
count: 1
|
||||
path: ../../../src/command/defaults/TimingsCommand.php
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user