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-12-16 17:58:52 +08:00
|
|
|
use ZM\Exception\SingletonViolationException;
|
2022-12-19 01:45:27 +08:00
|
|
|
use ZM\Plugin\PluginManager;
|
2022-12-18 00:15:19 +08:00
|
|
|
use ZM\Plugin\ZMPlugin;
|
2022-08-13 17:00:29 +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;
|
2022-08-21 20:15:55 +08:00
|
|
|
|
|
|
|
|
/** @var array 存储要传入的args */
|
|
|
|
|
private array $args = [];
|
2022-08-13 17:00:29 +08:00
|
|
|
|
2022-12-16 17:58:52 +08:00
|
|
|
public function __construct(mixed $dir = null)
|
2022-08-13 17:00:29 +08:00
|
|
|
{
|
|
|
|
|
if (self::$obj !== null) {
|
2022-12-16 17:58:52 +08:00
|
|
|
throw new SingletonViolationException(self::class);
|
2022-08-13 17:00:29 +08:00
|
|
|
}
|
|
|
|
|
self::$obj = $this; // 用于标记已经初始化完成
|
2022-08-21 20:15:55 +08:00
|
|
|
parent::__construct($dir ?? (__DIR__ . '/../..'));
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-03 10:18:17 +08:00
|
|
|
* @throws \Exception
|
2022-08-13 17:00:29 +08:00
|
|
|
*/
|
|
|
|
|
public function run()
|
|
|
|
|
{
|
2022-12-19 01:45:27 +08:00
|
|
|
PluginManager::addPlugin(['name' => 'native-app', 'object' => $this]);
|
2022-08-21 20:15:55 +08:00
|
|
|
(new Framework($this->args))->init()->start();
|
2022-08-13 17:00:29 +08:00
|
|
|
}
|
|
|
|
|
}
|