split InitException to SingletonViolationException

This commit is contained in:
sunxyw 2022-12-16 17:58:52 +08:00
parent daa07dcb2b
commit ebb724415d
No known key found for this signature in database
GPG Key ID: F391C42B19AFFC98
5 changed files with 24 additions and 20 deletions

View File

@ -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);
}
// 初始化命令

View File

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

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace ZM\Exception;
class SingletonViolationException extends ZMException
{
public function __construct(string $singleton_class_name)
{
parent::__construct(
"{$singleton_class_name} 是单例模式,不允许初始化多个实例。",
"请检查代码,确保只初始化了一个 {$singleton_class_name} 实例。",
69
);
}
}

View File

@ -27,7 +27,7 @@ use ZM\Event\Listener\ManagerEventListener;
use ZM\Event\Listener\MasterEventListener;
use ZM\Event\Listener\WorkerEventListener;
use ZM\Event\Listener\WSEventListener;
use ZM\Exception\InitException;
use ZM\Exception\SingletonViolationException;
use ZM\Exception\ZMKnownException;
use ZM\Logger\TablePrinter;
use ZM\Process\ProcessStateManager;
@ -69,14 +69,13 @@ class Framework
* 框架初始化文件
*
* @param array<string, null|bool|string> $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;

View File

@ -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__ . '/../..'));