zhamao-framework/src/ZM/ZMApplication.php
2022-12-19 01:45:27 +08:00

51 lines
1.2 KiB
PHP

<?php
declare(strict_types=1);
namespace ZM;
use ZM\Command\Server\ServerStartCommand;
use ZM\Exception\SingletonViolationException;
use ZM\Plugin\PluginManager;
use ZM\Plugin\ZMPlugin;
class ZMApplication extends ZMPlugin
{
/** @var null|ZMApplication 存储单例类的变量 */
private static ?ZMApplication $obj;
/** @var array 存储要传入的args */
private array $args = [];
public function __construct(mixed $dir = null)
{
if (self::$obj !== null) {
throw new SingletonViolationException(self::class);
}
self::$obj = $this; // 用于标记已经初始化完成
parent::__construct($dir ?? (__DIR__ . '/../..'));
$this->args = ServerStartCommand::exportOptionArray();
}
public function withConfig(array $config): ZMApplication
{
// TODO: 完成patch config
return $this;
}
public function withArgs(array $args): ZMApplication
{
$this->args = array_replace_recursive($this->args, $args);
return $this;
}
/**
* @throws \Exception
*/
public function run()
{
PluginManager::addPlugin(['name' => 'native-app', 'object' => $this]);
(new Framework($this->args))->init()->start();
}
}