zhamao-framework/src/ZM/InstantApplication.php

53 lines
1.3 KiB
PHP
Raw Normal View History

2022-08-13 17:00:29 +08:00
<?php
declare(strict_types=1);
namespace ZM;
2022-08-21 20:15:55 +08:00
use ZM\Command\Server\ServerStartCommand;
2022-08-13 17:00:29 +08:00
use ZM\Exception\InitException;
use ZM\Plugin\InstantPlugin;
class InstantApplication extends InstantPlugin
{
2022-08-21 20:15:55 +08:00
/** @var null|InstantApplication 存储单例类的变量 */
private static ?InstantApplication $obj;
/** @var array 存储要传入的args */
private array $args = [];
2022-08-13 17:00:29 +08:00
/**
* @param null|mixed $dir
* @throws InitException
*/
public function __construct($dir = null)
{
if (self::$obj !== null) {
throw new InitException(zm_internal_errcode('E00069') . 'Initializing another Application is not allowed!');
}
self::$obj = $this; // 用于标记已经初始化完成
2022-08-21 20:15:55 +08:00
parent::__construct($dir ?? (__DIR__ . '/../..'));
$this->args = ServerStartCommand::exportOptionArray();
}
public function withConfig(array $config): InstantApplication
{
// TODO: 完成patch config
return $this;
}
public function withArgs(array $args): InstantApplication
{
2022-08-23 18:02:00 +08:00
$this->args = array_replace_recursive($this->args, $args);
2022-08-21 20:15:55 +08:00
return $this;
2022-08-13 17:00:29 +08:00
}
/**
* @throws \Exception
2022-08-13 17:00:29 +08:00
*/
public function run()
{
2022-08-21 20:15:55 +08:00
(new Framework($this->args))->init()->start();
2022-08-13 17:00:29 +08:00
}
}