zhamao-framework/src/ZM/script_setup_loader.php

84 lines
3.4 KiB
PHP
Raw Normal View History

2021-06-16 00:17:30 +08:00
<?php
declare(strict_types=1);
2021-06-16 00:17:30 +08:00
use Doctrine\Common\Annotations\AnnotationReader;
use ZM\Annotation\Swoole\OnSetup;
use ZM\Annotation\Swoole\SwooleHandler;
use ZM\ConsoleApplication;
2021-07-04 15:45:30 +08:00
use ZM\Exception\InitException;
2021-06-16 00:17:30 +08:00
use ZM\Utils\DataProvider;
use ZM\Utils\ZMUtil;
require_once((!is_dir(__DIR__ . '/../../vendor')) ? getcwd() : (__DIR__ . '/../..')) . '/vendor/autoload.php';
2021-06-16 00:17:30 +08:00
try {
2021-07-04 15:45:30 +08:00
try {
(new ConsoleApplication('zhamao'))->initEnv();
} catch (InitException $e) {
}
2021-06-16 00:17:30 +08:00
$base_path = DataProvider::getSourceRootDir();
$scan_paths = [];
$composer = json_decode(file_get_contents($base_path . '/composer.json'), true);
foreach (($composer['autoload']['psr-4'] ?? []) as $k => $v) {
if (is_dir($base_path . '/' . $v) && !in_array($v, $composer['extra']['exclude_annotate'] ?? [])) {
$scan_paths[trim($k, '\\')] = $base_path . '/' . $v;
2021-06-16 00:17:30 +08:00
}
}
foreach (($composer['autoload-dev']['psr-4'] ?? []) as $k => $v) {
if (is_dir($base_path . '/' . $v) && !in_array($v, $composer['extra']['exclude_annotate'] ?? [])) {
$scan_paths[trim($k, '\\')] = $base_path . '/' . $v;
2022-03-13 22:11:30 +08:00
}
}
2021-06-16 00:17:30 +08:00
$all_event_class = [];
foreach ($scan_paths as $namespace => $autoload_path) {
$all_event_class = array_merge($all_event_class, ZMUtil::getClassesPsr4($autoload_path, $namespace));
}
$reader = new AnnotationReader();
$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 SwooleHandler) {
$event_list[] = [
'class' => $v,
'method' => $vs->getName(),
'event' => $annotation->event,
2021-06-16 00:17:30 +08:00
];
} elseif ($annotation instanceof OnSetup) {
$setup_list[] = [
'class' => $v,
'method' => $vs->getName(),
2021-06-16 00:17:30 +08:00
];
}
}
}
}
echo json_encode(['setup' => $setup_list, 'event' => $event_list]);
2021-06-16 00:17:30 +08:00
} 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);
2021-06-16 00:17:30 +08:00
fclose($stderr);
exit(1);
}
2022-03-13 22:11:30 +08:00
/*
记迷惑,这里的代码是不是可以放到一个单独的文件里面,这样就不会出现每次都要重新加载的问题了?
然后这个文件就实现了,就是这个。
但是还有个什么问题呢?为了 reload 牺牲了太多太多,但是关键时刻好像又不是很能用到。
但又不能没有。
所以我很纠结很纠结。
如何让用户的代码能像 php-fpm 那样随时重置呢?
我不知道诶。
那这段代码干了个啥?
在最开始单独启动进程,加载一遍所有类,获取需要在启动前就执行的类,然后在启动的时候执行。
这样就可以不在爷进程里面加载所有类,在爹进程里面 Fork 的子进程再加载所有类,每次 reload 时可以重新加载了。
以上均为乱写的,请勿完全当真,本人对待框架代码还是比较认真的。
*/