change phar writable checker to FileSystem

This commit is contained in:
Jerry 2023-02-01 16:59:54 +08:00
parent 60382a98f6
commit 2d2660c8f3
5 changed files with 28 additions and 16 deletions

View File

@ -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}");

View File

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace ZM\Exception;
class FileSystemException extends ZMException
{
}

View File

@ -290,7 +290,7 @@ class PluginManager
$phar_name = $plugin->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 . ' 已存在,删除中...');

View File

@ -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);
}
}

View File

@ -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);
}
}
}