From 2d2660c8f3ef0546ee648773e1617c05437dd98e Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 1 Feb 2023 16:59:54 +0800 Subject: [PATCH] change phar writable checker to FileSystem --- src/ZM/Command/BuildCommand.php | 4 +++- src/ZM/Exception/FileSystemException.php | 9 +++++++++ src/ZM/Plugin/PluginManager.php | 2 +- src/ZM/Store/FileSystem.php | 17 +++++++++++++++-- src/ZM/Store/PharHelper.php | 12 ------------ 5 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 src/ZM/Exception/FileSystemException.php diff --git a/src/ZM/Command/BuildCommand.php b/src/ZM/Command/BuildCommand.php index fc005014..90554552 100644 --- a/src/ZM/Command/BuildCommand.php +++ b/src/ZM/Command/BuildCommand.php @@ -6,6 +6,7 @@ namespace ZM\Command; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputOption; +use ZM\Exception\FileSystemException; use ZM\Store\FileSystem; use ZM\Store\PharHelper; @@ -27,6 +28,7 @@ class BuildCommand extends Command /** * @throws \PharException + * @throws FileSystemException */ protected function handle(): int { @@ -40,7 +42,7 @@ class BuildCommand extends Command } $target = $build_dir . '/' . $target; // 确认 Phar 文件可以写入 - PharHelper::ensurePharFileWritable($target); + FileSystem::ensureFileWritable($target); $this->comment("目标文件:{$target}"); diff --git a/src/ZM/Exception/FileSystemException.php b/src/ZM/Exception/FileSystemException.php new file mode 100644 index 00000000..6c920b2e --- /dev/null +++ b/src/ZM/Exception/FileSystemException.php @@ -0,0 +1,9 @@ +getName() . '_' . $plugin->getVersion() . '.phar'; $phar_name = zm_dir($build_dir . '/' . $phar_name); // 判断文件如果存在的话是否是可写的 - PharHelper::ensurePharFileWritable($phar_name); + FileSystem::ensureFileWritable($phar_name); // 文件存在先删除 if (file_exists($phar_name)) { $command_context?->info('Phar 文件 ' . $phar_name . ' 已存在,删除中...'); diff --git a/src/ZM/Store/FileSystem.php b/src/ZM/Store/FileSystem.php index 4efc2fea..aaa0ca1c 100644 --- a/src/ZM/Store/FileSystem.php +++ b/src/ZM/Store/FileSystem.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace ZM\Store; +use ZM\Exception\FileSystemException; use ZM\Utils\ZMUtil; class FileSystem @@ -83,12 +84,24 @@ class FileSystem /** * 创建目录(如果不存在) * - * @param string $path 目录路径 + * @param string $path 目录路径 + * @throws FileSystemException */ public static function createDir(string $path): void { if (!is_dir($path) && !mkdir($path, 0755, true) && !is_dir($path)) { - throw new \RuntimeException(sprintf('无法建立目录:%s', $path)); + throw new FileSystemException(sprintf('无法建立目录:%s', $path)); + } + } + + /** + * 调用该方法将确认传入的文件是否可写,如果不可写将抛出 FileSystemException 异常。 + * @throws FileSystemException + */ + public static function ensureFileWritable(string $phar_path): void + { + if (file_exists($phar_path) && !is_writable($phar_path)) { + throw new FileSystemException('目标文件不可写:' . $phar_path); } } diff --git a/src/ZM/Store/PharHelper.php b/src/ZM/Store/PharHelper.php index d8c3c54d..8e5d96aa 100644 --- a/src/ZM/Store/PharHelper.php +++ b/src/ZM/Store/PharHelper.php @@ -37,16 +37,4 @@ class PharHelper } } } - - /** - * 调用该方法将确认传入的 Phar 文件是否可写,如果不可写将抛出 \PharException 异常。 - * - * @throws \PharException - */ - public static function ensurePharFileWritable(string $phar_path): void - { - if (file_exists($phar_path) && !is_writable($phar_path)) { - throw new \PharException('目标文件不可写:' . $phar_path); - } - } }