Files
zhamao-framework/src/ZM/ZMApplication.php

59 lines
1.5 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;
use ZM\Exception\SingletonViolationException;
2022-12-19 01:45:27 +08:00
use ZM\Plugin\PluginManager;
2023-01-12 09:42:16 +08:00
use ZM\Plugin\PluginMeta;
2022-12-18 00:15:19 +08:00
use ZM\Plugin\ZMPlugin;
2022-08-13 17:00:29 +08:00
2023-01-13 14:22:31 +08:00
/**
* 这是一个可以将框架以代码形式启动的一个类,且继承于插件,可以以插件的方式绑定事件回调等
*/
2022-12-18 00:15:19 +08:00
class ZMApplication extends ZMPlugin
2022-08-13 17:00:29 +08:00
{
2022-12-18 00:15:19 +08:00
/** @var null|ZMApplication 存储单例类的变量 */
private static ?ZMApplication $obj = null;
2022-08-21 20:15:55 +08:00
/** @var array 存储要传入的args */
private array $args = [];
2022-08-13 17:00:29 +08:00
/**
* @throws SingletonViolationException
*/
2023-01-12 09:42:16 +08:00
public function __construct()
2022-08-13 17:00:29 +08:00
{
if (self::$obj !== null) {
throw new SingletonViolationException(self::class);
2022-08-13 17:00:29 +08:00
}
self::$obj = $this; // 用于标记已经初始化完成
2022-08-21 20:15:55 +08:00
$this->args = ServerStartCommand::exportOptionArray();
}
2022-12-18 00:15:19 +08:00
public function withConfig(array $config): ZMApplication
2022-08-21 20:15:55 +08:00
{
// TODO: 完成patch config
return $this;
}
2022-12-18 00:15:19 +08:00
public function withArgs(array $args): ZMApplication
2022-08-21 20:15:55 +08:00
{
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()
{
2023-01-12 09:42:16 +08:00
$meta = new PluginMeta(['name' => 'native'], ZM_PLUGIN_TYPE_NATIVE);
$meta->bindEntity($this);
PluginManager::addPlugin($meta);
2022-08-21 20:15:55 +08:00
(new Framework($this->args))->init()->start();
2022-08-13 17:00:29 +08:00
}
}