2021-11-02 16:01:24 +08:00
|
|
|
<?php
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2021-11-02 16:01:24 +08:00
|
|
|
namespace ZM;
|
|
|
|
|
|
|
|
|
|
use ZM\Command\RunServerCommand;
|
|
|
|
|
use ZM\Console\Console;
|
|
|
|
|
use ZM\Event\EventManager;
|
|
|
|
|
use ZM\Exception\InitException;
|
|
|
|
|
use ZM\Module\ModuleBase;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 2.6
|
|
|
|
|
*/
|
|
|
|
|
class ZMServer
|
|
|
|
|
{
|
2022-05-04 21:03:59 +08:00
|
|
|
/** @var string App名称 */
|
2021-11-02 16:01:24 +08:00
|
|
|
protected $app_name;
|
|
|
|
|
|
|
|
|
|
/** @var ModuleBase[] */
|
|
|
|
|
protected $modules = [];
|
|
|
|
|
|
2022-05-04 21:03:59 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $app_name App名称
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(string $app_name)
|
2022-03-15 18:05:33 +08:00
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
$this->app_name = $app_name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 21:03:59 +08:00
|
|
|
/**
|
|
|
|
|
* @param mixed $module_class
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
public function addModule($module_class)
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
$this->modules[] = $module_class;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 21:03:59 +08:00
|
|
|
/**
|
|
|
|
|
* @throws InitException
|
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
public function run()
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
Console::setLevel(4);
|
|
|
|
|
foreach ($this->modules as $module_class) {
|
|
|
|
|
foreach ($module_class->getEvents() as $event) {
|
|
|
|
|
EventManager::addEvent(get_class($event), $event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
echo "Running...\n";
|
2022-05-04 21:03:59 +08:00
|
|
|
if (defined('WORKING_DIR')) {
|
2022-03-15 18:05:33 +08:00
|
|
|
throw new InitException();
|
|
|
|
|
}
|
2021-11-02 16:01:24 +08:00
|
|
|
|
|
|
|
|
_zm_env_check();
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
define('WORKING_DIR', getcwd());
|
|
|
|
|
define('SOURCE_ROOT_DIR', WORKING_DIR);
|
|
|
|
|
define('LOAD_MODE', is_dir(SOURCE_ROOT_DIR . '/src/ZM') ? 0 : 1);
|
|
|
|
|
define('FRAMEWORK_ROOT_DIR', realpath(__DIR__ . '/../../'));
|
|
|
|
|
define('ZM_VERSION_ID', ConsoleApplication::VERSION_ID);
|
|
|
|
|
define('ZM_VERSION', ConsoleApplication::VERSION);
|
2021-11-02 16:01:24 +08:00
|
|
|
$options = array_map(function ($x) {
|
|
|
|
|
return $x->getDefault();
|
|
|
|
|
}, RunServerCommand::exportDefinition()->getOptions());
|
|
|
|
|
(new Framework($options, true))->start();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 21:03:59 +08:00
|
|
|
public function getAppName(): string
|
2022-03-15 18:05:33 +08:00
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
return $this->app_name;
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
}
|