mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 00:25:35 +08:00
refactor all base things
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user