mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 17:15:36 +08:00
refactor all base things
This commit is contained in:
@@ -25,9 +25,11 @@ const ZM_PROCESS_WORKER = ONEBOT_PROCESS_WORKER;
|
||||
const ZM_PROCESS_USER = ONEBOT_PROCESS_USER;
|
||||
const ZM_PROCESS_TASKWORKER = ONEBOT_PROCESS_TASKWORKER;
|
||||
|
||||
const ZM_PARSE_BEFORE_DRIVER = 0;
|
||||
const ZM_PARSE_AFTER_DRIVER = 1;
|
||||
const ZM_PARSE_BEFORE_START = 2;
|
||||
/** 定义一些内部引用的错误ID */
|
||||
const ZM_ERR_NONE = 0; // 正常
|
||||
const ZM_ERR_METHOD_NOT_FOUND = 1; // 找不到方法
|
||||
const ZM_ERR_ROUTE_NOT_FOUND = 2; // 找不到路由
|
||||
const ZM_ERR_ROUTE_METHOD_NOT_ALLOWED = 3; // 路由方法不允许
|
||||
|
||||
/* 定义工作目录 */
|
||||
define('WORKING_DIR', getcwd());
|
||||
@@ -52,7 +54,6 @@ if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
|
||||
/* 对 global.php 在 Windows 下的兼容性考虑,因为 Windows 或者无 Swoole 环境时候无法运行 */
|
||||
!defined('SWOOLE_BASE') && define('SWOOLE_BASE', 1) && define('SWOOLE_PROCESS', 2);
|
||||
|
||||
!defined('SWOOLE_HOOK_ALL') && (
|
||||
define('SWOOLE_HOOK_TCP', 2)
|
||||
&& define('SWOOLE_HOOK_UDP', 4)
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/** 定义炸毛框架初始启动时间 */
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
/* 定义炸毛框架初始启动时间 */
|
||||
if (!defined('ZM_START_TIME')) {
|
||||
define('ZM_START_TIME', microtime(true));
|
||||
}
|
||||
|
||||
/* 定义使用炸毛框架应用的版本 */
|
||||
if (!defined('APP_VERSION')) {
|
||||
define('APP_VERSION', LOAD_MODE == 1 ? (json_decode(file_get_contents(SOURCE_ROOT_DIR . '/composer.json'), true)['version'] ?? 'unknown') : 'unknown');
|
||||
define('APP_VERSION', LOAD_MODE == 1 ? (ZMUtil::getComposerMetadata()['version'] ?? ZM_VERSION) : ZM_VERSION);
|
||||
}
|
||||
|
||||
@@ -2,8 +2,18 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use OneBot\V12\Object\MessageSegment;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ZM\Container\Container;
|
||||
use ZM\Container\ContainerInterface;
|
||||
use ZM\Context\Context;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
use ZM\Middleware\MiddlewareHandler;
|
||||
|
||||
// 防止重复引用引发报错
|
||||
if (function_exists('zm_internal_errcode')) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据具体操作系统替换目录分隔符
|
||||
@@ -61,12 +71,66 @@ function is_assoc_array(array $array): bool
|
||||
return !empty($array) && array_keys($array) !== range(0, count($array) - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return object
|
||||
*
|
||||
* TODO: 等待完善DI
|
||||
*/
|
||||
function resolve(string $class)
|
||||
function ctx(): Context
|
||||
{
|
||||
return new $class();
|
||||
return \container()->get('ctx');
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建消息段的助手函数
|
||||
*
|
||||
* @param string $type 类型
|
||||
* @param array $data 字段
|
||||
*/
|
||||
function segment(string $type, array $data = []): MessageSegment
|
||||
{
|
||||
return new MessageSegment($type, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 中间件操作类的助手函数
|
||||
*/
|
||||
function middleware(): MiddlewareHandler
|
||||
{
|
||||
return MiddlewareHandler::getInstance();
|
||||
}
|
||||
|
||||
// ////////////////// 容器部分 //////////////////////
|
||||
|
||||
/**
|
||||
* 获取容器(请求级)实例
|
||||
*/
|
||||
function container(): ContainerInterface
|
||||
{
|
||||
return Container::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析类实例(使用容器)
|
||||
*
|
||||
* @template T
|
||||
* @param class-string<T> $abstract
|
||||
* @return Closure|mixed|T
|
||||
* @noinspection PhpDocMissingThrowsInspection
|
||||
*/
|
||||
function resolve(string $abstract, array $parameters = [])
|
||||
{
|
||||
/* @noinspection PhpUnhandledExceptionInspection */
|
||||
return Container::getInstance()->make($abstract, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取容器实例
|
||||
*
|
||||
* @template T
|
||||
* @param null|class-string<T> $abstract
|
||||
* @return Closure|ContainerInterface|mixed|T
|
||||
*/
|
||||
function app(string $abstract = null, array $parameters = [])
|
||||
{
|
||||
if (is_null($abstract)) {
|
||||
return container();
|
||||
}
|
||||
|
||||
return resolve($abstract, $parameters);
|
||||
}
|
||||
|
||||
@@ -2,60 +2,43 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Koriym\Attributes\AttributeReader;
|
||||
use Koriym\Attributes\DualReader;
|
||||
use ZM\Annotation\Framework\OnSetup;
|
||||
use ZM\ConsoleApplication;
|
||||
use ZM\Exception\InitException;
|
||||
use ZM\Store\FileSystem;
|
||||
use ZM\Annotation\AnnotationParser;
|
||||
use ZM\Annotation\Framework\Setup;
|
||||
use ZM\Utils\ZMUtil;
|
||||
|
||||
function _zm_setup_loader()
|
||||
{
|
||||
try {
|
||||
try {
|
||||
new ConsoleApplication('zhamao');
|
||||
} catch (InitException $e) {
|
||||
}
|
||||
$base_path = SOURCE_ROOT_DIR;
|
||||
$scan_paths = [];
|
||||
$composer = json_decode(file_get_contents($base_path . '/composer.json'), true);
|
||||
$exclude_annotations = array_merge($composer['extra']['exclude_annotate'] ?? [], $composer['extra']['zm']['exclude-annotation-path'] ?? []);
|
||||
foreach (($composer['autoload']['psr-4'] ?? []) as $k => $v) {
|
||||
if (is_dir($base_path . '/' . $v) && !in_array($v, $exclude_annotations)) {
|
||||
$scan_paths[trim($k, '\\')] = $base_path . '/' . $v;
|
||||
global $_tmp_setup_list;
|
||||
$_tmp_setup_list = [];
|
||||
$parser = new AnnotationParser(false);
|
||||
$composer = ZMUtil::getComposerMetadata();
|
||||
// 合并 dev 和 非 dev 的 psr-4 加载目录
|
||||
$merge_psr4 = array_merge($composer['autoload']['psr-4'] ?? [], $composer['autoload-dev']['psr-4'] ?? []);
|
||||
// 排除 composer.json 中指定需要排除的目录
|
||||
$excludes = $composer['extra']['zm']['exclude-annotation-path'] ?? [];
|
||||
foreach ($merge_psr4 as $k => $v) {
|
||||
// 如果在排除表就排除,否则就解析注解
|
||||
if (is_dir(SOURCE_ROOT_DIR . '/' . $v) && !in_array($v, $excludes)) {
|
||||
// 添加解析路径,对应Base命名空间也贴出来
|
||||
$parser->addRegisterPath(SOURCE_ROOT_DIR . '/' . $v . '/', trim($k, '\\'));
|
||||
}
|
||||
}
|
||||
foreach (($composer['autoload-dev']['psr-4'] ?? []) as $k => $v) {
|
||||
if (is_dir($base_path . '/' . $v) && !in_array($v, $exclude_annotations)) {
|
||||
$scan_paths[trim($k, '\\')] = $base_path . '/' . $v;
|
||||
}
|
||||
}
|
||||
$all_event_class = [];
|
||||
foreach ($scan_paths as $namespace => $autoload_path) {
|
||||
$all_event_class = array_merge($all_event_class, FileSystem::getClassesPsr4($autoload_path, $namespace));
|
||||
}
|
||||
$parser->addSpecialParser(Setup::class, function (Setup $setup) {
|
||||
global $_tmp_setup_list;
|
||||
$_tmp_setup_list[] = [
|
||||
'class' => $setup->class,
|
||||
'method' => $setup->method,
|
||||
];
|
||||
return true;
|
||||
});
|
||||
|
||||
$reader = new DualReader(new AnnotationReader(), new AttributeReader());
|
||||
$event_list = [];
|
||||
$setup_list = [];
|
||||
foreach ($all_event_class as $v) {
|
||||
$reflection_class = new ReflectionClass($v);
|
||||
$methods = $reflection_class->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||
foreach ($methods as $vs) {
|
||||
$method_annotations = $reader->getMethodAnnotations($vs);
|
||||
if ($method_annotations != []) {
|
||||
$annotation = $method_annotations[0];
|
||||
if ($annotation instanceof OnSetup) {
|
||||
$setup_list[] = [
|
||||
'class' => $v,
|
||||
'method' => $vs->getName(),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return json_encode(['setup' => $setup_list, 'event' => $event_list]);
|
||||
// TODO: 然后加载插件目录下的插件
|
||||
|
||||
// 解析所有注册路径的文件,获取注解
|
||||
$parser->parseAll();
|
||||
|
||||
return json_encode(['setup' => $_tmp_setup_list]);
|
||||
} catch (Throwable $e) {
|
||||
$stderr = fopen('php://stderr', 'w');
|
||||
fwrite($stderr, zm_internal_errcode('E00031') . $e->getMessage() . ' in ' . $e->getFile() . ' at line ' . $e->getLine() . PHP_EOL);
|
||||
|
||||
Reference in New Issue
Block a user