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