mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 16:45:35 +08:00
update to 3.0.0-alpha2 (build 610): refactor driver
This commit is contained in:
74
src/Globals/global_defines_app.php
Normal file
74
src/Globals/global_defines_app.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
此文件用于定义一些常量,是框架运行的前提常量,例如定义全局版本、全局方法等。
|
||||
*/
|
||||
|
||||
use ZM\Framework;
|
||||
|
||||
/** 全局版本ID */
|
||||
const ZM_VERSION_ID = Framework::VERSION_ID;
|
||||
|
||||
/** 全局版本名称 */
|
||||
const ZM_VERSION = Framework::VERSION;
|
||||
|
||||
/** 机器人用的,用于判断二元语意 */
|
||||
const TRUE_LIST = ['yes', 'y', 'true', 'on', '是', '对', true];
|
||||
const FALSE_LIST = ['no', 'n', 'false', 'off', '否', '错', false];
|
||||
|
||||
/** 定义多进程的全局变量 */
|
||||
const ZM_PROCESS_MASTER = ONEBOT_PROCESS_MASTER;
|
||||
const ZM_PROCESS_MANAGER = ONEBOT_PROCESS_MANAGER;
|
||||
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;
|
||||
|
||||
/* 定义工作目录 */
|
||||
define('WORKING_DIR', getcwd());
|
||||
|
||||
/* 定义源码根目录,如果是 Phar 打包框架运行的话,就是 Phar 文件本身 */
|
||||
define('SOURCE_ROOT_DIR', Phar::running() !== '' ? Phar::running() : WORKING_DIR);
|
||||
|
||||
/* 定义启动模式,这里指的是框架本身的源码目录是通过 composer 加入 vendor 加载的还是直接放到 src 目录加载的,前者为 1,后者为 0 */
|
||||
define('LOAD_MODE', is_dir(zm_dir(SOURCE_ROOT_DIR . '/src/ZM')) ? 0 : 1);
|
||||
|
||||
/* 定义框架本身所处的根目录,此处如果 LOAD_MODE 为 1 的话,框架自身的根目录在 vendor/zhamao/framework 子目录下 */
|
||||
if (Phar::running() !== '') {
|
||||
define('FRAMEWORK_ROOT_DIR', zm_dir(LOAD_MODE == 1 ? (SOURCE_ROOT_DIR . '/vendor/zhamao/framework') : SOURCE_ROOT_DIR));
|
||||
} else {
|
||||
define('FRAMEWORK_ROOT_DIR', realpath(zm_dir(__DIR__ . '/../../')));
|
||||
}
|
||||
|
||||
/* 定义用于存放框架运行状态的目录(Windows 不可用) */
|
||||
if (DIRECTORY_SEPARATOR !== '\\') {
|
||||
define('ZM_PID_DIR', '/tmp/.zm_' . sha1(FRAMEWORK_ROOT_DIR));
|
||||
}
|
||||
|
||||
/* 对 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)
|
||||
&& define('SWOOLE_HOOK_UNIX', 8)
|
||||
&& define('SWOOLE_HOOK_UDG', 16)
|
||||
&& define('SWOOLE_HOOK_SSL', 32)
|
||||
&& define('SWOOLE_HOOK_TLS', 64)
|
||||
&& define('SWOOLE_HOOK_STREAM_FUNCTION', 128)
|
||||
&& define('SWOOLE_HOOK_STREAM_SELECT', 128)
|
||||
&& define('SWOOLE_HOOK_FILE', 256)
|
||||
&& define('SWOOLE_HOOK_STDIO', 32768)
|
||||
&& define('SWOOLE_HOOK_SLEEP', 512)
|
||||
&& define('SWOOLE_HOOK_PROC', 1024)
|
||||
&& define('SWOOLE_HOOK_CURL', 2048)
|
||||
&& define('SWOOLE_HOOK_NATIVE_CURL', 4096)
|
||||
&& define('SWOOLE_HOOK_BLOCKING_FUNCTION', 8192)
|
||||
&& define('SWOOLE_HOOK_SOCKETS', 16384)
|
||||
&& define('SWOOLE_HOOK_ALL', 2147481599)
|
||||
);
|
||||
12
src/Globals/global_defines_framework.php
Normal file
12
src/Globals/global_defines_framework.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/** 定义炸毛框架初始启动时间 */
|
||||
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');
|
||||
}
|
||||
72
src/Globals/global_functions.php
Normal file
72
src/Globals/global_functions.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
|
||||
/**
|
||||
* 根据具体操作系统替换目录分隔符
|
||||
*
|
||||
* @param string $dir 目录
|
||||
*/
|
||||
function zm_dir(string $dir): string
|
||||
{
|
||||
if (strpos($dir, 'phar://') === 0) {
|
||||
return $dir;
|
||||
}
|
||||
return str_replace('/', DIRECTORY_SEPARATOR, $dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取内部错误码
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return object
|
||||
*
|
||||
* TODO: 等待完善DI
|
||||
*/
|
||||
function resolve(string $class)
|
||||
{
|
||||
return new $class();
|
||||
}
|
||||
71
src/Globals/script_setup_loader.php
Normal file
71
src/Globals/script_setup_loader.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
$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]);
|
||||
} 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);
|
||||
fclose($stderr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 在*nix等支持多进程环境的情况,可直接运行此文件,那么就执行
|
||||
if (debug_backtrace() === []) {
|
||||
require((!is_dir(__DIR__ . '/../../vendor')) ? getcwd() : (__DIR__ . '/../..')) . '/vendor/autoload.php';
|
||||
echo _zm_setup_loader();
|
||||
}
|
||||
Reference in New Issue
Block a user