From ebb724415d4ad4e13d7857a322645906f1b58382 Mon Sep 17 00:00:00 2001 From: sunxyw Date: Fri, 16 Dec 2022 17:58:52 +0800 Subject: [PATCH] split InitException to SingletonViolationException --- src/ZM/ConsoleApplication.php | 7 ++----- src/ZM/Exception/InitException.php | 5 ----- .../Exception/SingletonViolationException.php | 17 +++++++++++++++++ src/ZM/Framework.php | 5 ++--- src/ZM/InstantApplication.php | 10 +++------- 5 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 src/ZM/Exception/SingletonViolationException.php diff --git a/src/ZM/ConsoleApplication.php b/src/ZM/ConsoleApplication.php index c670fee2..7a0c5120 100644 --- a/src/ZM/ConsoleApplication.php +++ b/src/ZM/ConsoleApplication.php @@ -19,7 +19,7 @@ use ZM\Command\Server\ServerReloadCommand; use ZM\Command\Server\ServerStartCommand; use ZM\Command\Server\ServerStatusCommand; use ZM\Command\Server\ServerStopCommand; -use ZM\Exception\InitException; +use ZM\Exception\SingletonViolationException; /** * 命令行启动的入口文件,用于初始化环境变量,并启动命令行应用 @@ -30,13 +30,10 @@ final class ConsoleApplication extends Application { private static $obj; - /** - * @throws InitException - */ public function __construct(string $name = 'zhamao-framework') { if (self::$obj !== null) { - throw new InitException(zm_internal_errcode('E00069') . 'Initializing another Application is not allowed!'); + throw new SingletonViolationException(self::class); } // 初始化命令 diff --git a/src/ZM/Exception/InitException.php b/src/ZM/Exception/InitException.php index 538bf743..49ba9dce 100644 --- a/src/ZM/Exception/InitException.php +++ b/src/ZM/Exception/InitException.php @@ -6,9 +6,4 @@ namespace ZM\Exception; class InitException extends ZMException { - public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) - { - // TODO: change this to a better error message - parent::__construct($message, '', $code, $previous); - } } diff --git a/src/ZM/Exception/SingletonViolationException.php b/src/ZM/Exception/SingletonViolationException.php new file mode 100644 index 00000000..3e5ed60c --- /dev/null +++ b/src/ZM/Exception/SingletonViolationException.php @@ -0,0 +1,17 @@ + $argv 传入的参数(见 ServerStartCommand) - * @throws InitException * @throws \Exception */ public function __construct(array $argv = []) { // 单例化整个Framework类 if (self::$instance !== null) { - throw new InitException(zm_internal_errcode('E00069') . 'Initializing another Framework in one instance is not allowed!'); + throw new SingletonViolationException(self::class); } self::$instance = $this; diff --git a/src/ZM/InstantApplication.php b/src/ZM/InstantApplication.php index 95f79556..c6380078 100644 --- a/src/ZM/InstantApplication.php +++ b/src/ZM/InstantApplication.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace ZM; use ZM\Command\Server\ServerStartCommand; -use ZM\Exception\InitException; +use ZM\Exception\SingletonViolationException; use ZM\Plugin\InstantPlugin; class InstantApplication extends InstantPlugin @@ -16,14 +16,10 @@ class InstantApplication extends InstantPlugin /** @var array 存储要传入的args */ private array $args = []; - /** - * @param null|mixed $dir - * @throws InitException - */ - public function __construct($dir = null) + public function __construct(mixed $dir = null) { if (self::$obj !== null) { - throw new InitException(zm_internal_errcode('E00069') . 'Initializing another Application is not allowed!'); + throw new SingletonViolationException(self::class); } self::$obj = $this; // 用于标记已经初始化完成 parent::__construct($dir ?? (__DIR__ . '/../..'));