Files
zhamao-framework/src/Globals/global_functions.php

213 lines
4.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2022-08-14 18:24:59 +08:00
use OneBot\Driver\Coroutine\Adaptive;
use OneBot\Driver\Coroutine\CoroutineInterface;
use OneBot\Driver\Process\ExecutionResult;
2022-08-13 17:00:29 +08:00
use OneBot\V12\Object\MessageSegment;
use Psr\Log\LoggerInterface;
2022-08-23 17:51:20 +08:00
use ZM\Config\ZMConfig;
2022-08-13 17:00:29 +08:00
use ZM\Container\Container;
use ZM\Container\ContainerInterface;
use ZM\Context\Context;
use ZM\Logger\ConsoleLogger;
2022-08-13 17:00:29 +08:00
use ZM\Middleware\MiddlewareHandler;
2022-08-21 16:08:20 +08:00
use ZM\Store\MySQL\MySQLException;
use ZM\Store\MySQL\MySQLWrapper;
2022-08-13 17:00:29 +08:00
// 防止重复引用引发报错
if (function_exists('zm_internal_errcode')) {
return;
}
/**
* 根据具体操作系统替换目录分隔符
*
* @param string $dir 目录
*/
function zm_dir(string $dir): string
{
if (strpos($dir, 'phar://') === 0) {
return $dir;
}
return str_replace('/', DIRECTORY_SEPARATOR, $dir);
}
2022-08-14 18:24:59 +08:00
/**
* 执行shell指令
*
* @param string $cmd 命令行
*/
function zm_exec(string $cmd): ExecutionResult
{
return Adaptive::exec($cmd);
}
/**
* sleep 指定时间单位为秒最小单位为1毫秒即0.001
*
* @param float|int $time
*/
function zm_sleep($time)
{
Adaptive::sleep($time);
}
/**
* 获取协程接口
*/
function coroutine(): ?CoroutineInterface
{
return Adaptive::getCoroutine();
}
/**
* 获取内部错误码
*
* @param int|string $code
*/
function zm_internal_errcode($code): string
{
return "[ErrCode:{$code}] ";
}
function zm_instance_id(): string
{
if (defined('ZM_INSTANCE_ID')) {
return ZM_INSTANCE_ID;
}
if (!defined('ZM_START_TIME')) {
define('ZM_START_TIME', microtime(true));
}
$instance_id = dechex(crc32(strval(ZM_START_TIME)));
define('ZM_INSTANCE_ID', $instance_id);
return ZM_INSTANCE_ID;
}
/**
* 助手方法,返回一个 Logger 实例
*/
function logger(): LoggerInterface
{
global $ob_logger;
if ($ob_logger === null) {
return new ConsoleLogger();
}
return $ob_logger;
}
/**
* 判断传入的数组是否为关联数组
*/
function is_assoc_array(array $array): bool
{
return !empty($array) && array_keys($array) !== range(0, count($array) - 1);
}
2022-08-13 17:00:29 +08:00
function ctx(): Context
{
2022-08-13 17:00:29 +08:00
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();
}
/**
* 解析类实例(使用容器)
*
2022-08-23 17:51:20 +08:00
* @template T
2022-08-23 18:02:00 +08:00
* @param class-string<T> $abstract
2022-08-13 17:00:29 +08:00
* @return Closure|mixed|T
* @noinspection PhpDocMissingThrowsInspection
*/
function resolve(string $abstract, array $parameters = [])
{
/* @noinspection PhpUnhandledExceptionInspection */
return Container::getInstance()->make($abstract, $parameters);
}
/**
* 获取容器实例
*
* @template T
2022-08-23 18:02:00 +08:00
* @param null|class-string<T> $abstract
2022-08-13 17:00:29 +08:00
* @return Closure|ContainerInterface|mixed|T
*/
function app(string $abstract = null, array $parameters = [])
{
if (is_null($abstract)) {
return container();
}
return resolve($abstract, $parameters);
}
2022-08-21 16:08:20 +08:00
/**
* 获取 MySQL 调用的类
*
* @throws MySQLException
*/
function mysql(string $name = '')
{
return new MySQLWrapper($name);
}
/**
* 获取构建 MySQL 的类
*
* @throws MySQLException
*/
function mysql_builder(string $name = '')
{
return (new MySQLWrapper($name))->createQueryBuilder();
}
2022-08-23 17:51:20 +08:00
/**
* 获取 / 设置配置项
*
* 传入键名和(或)默认值,获取配置项
* 传入数组,设置配置项
* 不传参数,返回配置容器
*
2022-08-23 18:02:00 +08:00
* @param null|array|string $key 键名
* @param mixed $default 默认值
2022-08-23 17:51:20 +08:00
* @return mixed|ZMConfig
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return resolve('config');
}
if (is_array($key)) {
return resolve('config')->set(...$key);
}
return resolve('config')->get($key, $default);
}