BulkCurlTask now accepts a closure callback for onCompletion

this requires much less boilerplate code than an anonymous class.
This commit is contained in:
Dylan K. Taylor 2021-03-16 23:56:14 +00:00
parent dcf53bd06d
commit b3c6c11b20
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 41 additions and 47 deletions

View File

@ -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,19 +129,8 @@ 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(
/** @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", "https://$host?upload=true",
10, 10,
[], [],
@ -154,19 +144,13 @@ class TimingsCommand extends VanillaCommand{
CURLOPT_AUTOREFERER => false, CURLOPT_AUTOREFERER => false,
CURLOPT_FOLLOWLOCATION => false CURLOPT_FOLLOWLOCATION => false
] ]
) )],
]); function(array $results) use ($sender, $host) : void{
$this->host = $host; /** @phpstan-var array<InternetRequestResult|InternetException> $results */
$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]));

View File

@ -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;
@ -45,10 +48,11 @@ class BulkCurlTask extends AsyncTask{
* {@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);
}
} }

View File

@ -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