From c07e7f921cfd973e8b005f46ad1c3d04a1be1395 Mon Sep 17 00:00:00 2001 From: nmtho <144540995+toby7002@users.noreply.github.com> Date: Sat, 2 Dec 2023 09:40:17 +0700 Subject: [PATCH] Use await-generator for I/O functions --- .../commands/subcommands/DisableCommand.php | 10 +++- .../commands/subcommands/EnableCommand.php | 10 +++- .../commands/subcommands/ExtractCommand.php | 22 ++++++--- .../commands/subcommands/InstallCommand.php | 13 +++-- .../commands/subcommands/RemoveCommand.php | 20 ++++---- src/thebigcrafter/omp/helpers/PharHelper.php | 46 +++++++++++++----- .../omp/helpers/PluginHelper.php | 47 ++++++++++++++++++ .../omp/tasks/RemovePluginTask.php | 48 ------------------- 8 files changed, 134 insertions(+), 82 deletions(-) create mode 100644 src/thebigcrafter/omp/helpers/PluginHelper.php delete mode 100644 src/thebigcrafter/omp/tasks/RemovePluginTask.php diff --git a/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php b/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php index 538bf1b..9b2018d 100644 --- a/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php @@ -15,8 +15,10 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; +use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; @@ -46,7 +48,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v return; } - Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath)); + Await::f2c(function () use ($oldPluginFilePath, $newPluginFilePath, $sender) : Generator { + try { + yield from Filesystem::rename($oldPluginFilePath, $newPluginFilePath); + } catch (IOException $e) { + $sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()])); + } + }); $sender->sendMessage(Language::translate("commands.disable.successfully", ["name" => $name])); } diff --git a/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php b/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php index f18a343..f7be9a7 100644 --- a/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php @@ -15,8 +15,10 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; +use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; @@ -46,7 +48,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v return; } - Await::g2c(Filesystem::rename($oldPluginFilePath, $newPluginFilePath)); + Await::f2c(function () use ($oldPluginFilePath, $newPluginFilePath, $sender) : Generator { + try { + yield from Filesystem::rename($oldPluginFilePath, $newPluginFilePath); + } catch (IOException $e) { + $sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()])); + } + }); $sender->sendMessage(Language::translate("commands.enable.successfully", ["name" => $name])); } diff --git a/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php b/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php index d36c7e6..bb94c47 100644 --- a/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php @@ -15,14 +15,18 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; -use Phar; +use Generator; use pocketmine\command\CommandSender; +use SOFe\AwaitGenerator\Await; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Path; +use thebigcrafter\omp\helpers\PharHelper; use thebigcrafter\omp\Language; use thebigcrafter\omp\Utils; +use Throwable; -class ExtractCommand extends BaseSubCommand { +class ExtractCommand extends BaseSubCommand +{ protected function prepare() : void { $this->setPermission("oh-my-pmmp.extract"); @@ -39,14 +43,18 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $name = $args["name"]; $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); - if(!$fs->exists($pluginFilePath)) { + if (!$fs->exists($pluginFilePath)) { $sender->sendMessage(Language::translate("commands.extract.failed", ["name" => $name])); return; } - $phar = new Phar($pluginFilePath); - $phar->extractTo(Path::join(Utils::getPluginsFolder(), $name)); - - $sender->sendMessage(Language::translate("commands.extract.successfully", ["name" => $name])); + Await::f2c(function () use ($pluginFilePath, $name, $sender) : Generator { + try { + yield from PharHelper::extract($pluginFilePath, Path::join(Utils::getPluginsFolder(), $name)); + $sender->sendMessage(Language::translate("commands.extract.successfully", ["name" => $name])); + } catch (Throwable $e) { + $sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()])); + } + }); } } diff --git a/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php b/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php index 5c003dc..01b3847 100644 --- a/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php @@ -15,6 +15,8 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use Exception; +use Generator; use pocketmine\command\CommandSender; use pocketmine\utils\Internet; use pocketmine\utils\InternetRequestResult; @@ -71,8 +73,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $pharPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar"); - Await::g2c(PharHelper::writePhar($pharPath, $res->getBody())); - - $sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion])); + Await::f2c(function () use ($pharPath, $res, $sender, $name, $latestVersion) : Generator { + try { + yield from PharHelper::create($pharPath, $res->getBody()); + $sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion])); + } catch (Exception $e) { + $sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()])); + } + }); } } diff --git a/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php b/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php index 154c175..a23fc33 100644 --- a/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php @@ -16,9 +16,11 @@ use CortexPE\Commando\args\BooleanArgument; use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use Generator; use pocketmine\command\CommandSender; +use SOFe\AwaitGenerator\Await; +use thebigcrafter\omp\helpers\PluginHelper; use thebigcrafter\omp\Language; -use thebigcrafter\omp\tasks\RemovePluginTask; class RemoveCommand extends BaseSubCommand { @@ -38,13 +40,13 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $name = $args["name"]; $wipeData = isset($args["wipeData"]) ? (bool) $args["wipeData"] : false; - $exec = (new RemovePluginTask($name, $wipeData))->execute(); - - if (!$exec) { - $sender->sendMessage(Language::translate("commands.remove.failed", ["name" => $name])); - return; - } - $sender->sendMessage(Language::translate("commands.remove.successfully", ["name" => $name])); - return; + Await::f2c(function () use ($name, $wipeData, $sender) : Generator { + try { + yield from PluginHelper::remove($name, $wipeData); + $sender->sendMessage(Language::translate("commands.remove.successfully", ["name" => $name])); + } catch (\Throwable $th) { + $sender->sendMessage(Language::translate("commands.remove.failed", ["name" => $name])); + } + }); } } diff --git a/src/thebigcrafter/omp/helpers/PharHelper.php b/src/thebigcrafter/omp/helpers/PharHelper.php index 28a4313..6e7f700 100644 --- a/src/thebigcrafter/omp/helpers/PharHelper.php +++ b/src/thebigcrafter/omp/helpers/PharHelper.php @@ -13,24 +13,44 @@ namespace thebigcrafter\omp\helpers; -use Closure; -use Exception; -use Generator; +use Closure; +use Exception; +use Generator; +use Phar; use SOFe\AwaitGenerator\Await; +use Throwable; use function file_put_contents; -class PharHelper -{ - public static function writePhar(string $path, string $content) : Generator - { - return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path, $content) { +class PharHelper +{ + /** + * Create a Phar file + * Returns the number of bytes that were written to the file, or throw an Exception on failure. + */ + public static function create(string $path, string $content) : Generator + { + return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path, $content) { $exec = file_put_contents($path, $content); - if ($exec === false) { - $reject(new Exception("Cannot create Phar file")); + if ($exec === false) { + $reject(new Exception("Cannot create Phar file")); + return; } - $resolve($exec); - }); - } + $resolve($exec); + }); + } + + public static function extract(string $filePath, string $to) : Generator + { + return Await::promise(function (Closure $resolve, Closure $reject) use ($filePath, $to) { + $phar = new Phar($filePath); + try { + $phar->extractTo($to); + $resolve(true); + } catch (Throwable $e) { + $reject($e); + } + }); + } } diff --git a/src/thebigcrafter/omp/helpers/PluginHelper.php b/src/thebigcrafter/omp/helpers/PluginHelper.php new file mode 100644 index 0000000..6388b33 --- /dev/null +++ b/src/thebigcrafter/omp/helpers/PluginHelper.php @@ -0,0 +1,47 @@ + + * + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\helpers; + +use Closure; +use Exception; +use Generator; +use SOFe\AwaitGenerator\Await; +use Symfony\Component\Filesystem\Path; +use thebigcrafter\omp\OhMyPMMP; +use thebigcrafter\omp\Utils; +use thebigcrafter\omp\utils\Filesystem; + +class PluginHelper +{ + public static function remove(string $name, bool $wipeData) : Generator + { + return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($name, $wipeData) { + $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); + $pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); + + if (Filesystem::exists($pluginFilePath)) { + Await::g2c(Filesystem::remove($pluginFilePath)); + } elseif (Filesystem::exists($pluginFolderPath)) { + Await::g2c(Filesystem::remove($pluginFolderPath)); + } else { + $reject(new Exception("Plugin not found")); + } + if ($wipeData) { + $pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); + Await::g2c(Filesystem::remove($pluginDataFolder)); + } + $resolve(true); + }); + } +} diff --git a/src/thebigcrafter/omp/tasks/RemovePluginTask.php b/src/thebigcrafter/omp/tasks/RemovePluginTask.php deleted file mode 100644 index 1fd5f9f..0000000 --- a/src/thebigcrafter/omp/tasks/RemovePluginTask.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * This source file is subject to the GPL-3.0 license that is bundled - * with this source code in the file LICENSE. - */ - -declare(strict_types=1); - -namespace thebigcrafter\omp\tasks; - -use SOFe\AwaitGenerator\Await; -use Symfony\Component\Filesystem\Path; -use thebigcrafter\omp\OhMyPMMP; -use thebigcrafter\omp\Utils; -use thebigcrafter\omp\utils\Filesystem; - -class RemovePluginTask extends Task -{ - public function __construct(private readonly string $name, private readonly bool $wipeData) - { - } - public function execute() : bool - { - $name = $this->name; - $wipeData = $this->wipeData; - - $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); - $pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); - - if (Filesystem::exists($pluginFilePath)) { - Await::g2c(Filesystem::remove($pluginFilePath)); - } elseif (Filesystem::exists($pluginFolderPath)) { - Await::g2c(Filesystem::remove($pluginFolderPath)); - } else { - return false; - } - if ($wipeData) { - $pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); - Await::g2c(Filesystem::remove($pluginDataFolder)); - } - return true; - } -}