Files
nexusphp/nexus/Nexus.php

430 lines
13 KiB
PHP
Raw Normal View History

2022-03-20 22:14:00 +08:00
<?php
namespace Nexus;
2025-02-15 03:15:45 +08:00
use App\Http\Middleware\Locale;
2025-06-10 21:50:41 +07:00
use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Capsule\Manager;
use Illuminate\Redis\RedisManager;
2022-04-07 15:44:43 +08:00
use Illuminate\Support\Arr;
2025-04-19 02:06:51 +07:00
use Nexus\Translation\NexusTranslator;
2022-04-07 15:44:43 +08:00
2022-03-20 22:14:00 +08:00
final class Nexus
{
private string $requestId;
private int $logSequence = 0;
private float $startTimestamp;
private string $script;
private string $platform;
private static bool $booted = false;
private static ?Nexus $instance = null;
2022-04-03 16:03:47 +08:00
private static array $appendHeaders = [];
private static array $appendFooters = [];
2022-08-10 17:38:05 +08:00
private static array $translationNamespaces = [];
private static array $translations = [];
2025-04-19 02:06:51 +07:00
private static ?NexusTranslator $translator = null;
2025-06-10 21:50:41 +07:00
private static ?Manager $queueManager = null;
const QUEUE_CONNECTION_NAME = 'my_queue_connection';
2022-03-20 22:14:00 +08:00
const PLATFORM_USER = 'user';
const PLATFORM_ADMIN = 'admin';
const PLATFORM_TRACKER = 'tracker';
const PLATFORMS = [self::PLATFORM_USER, self::PLATFORM_ADMIN, self::PLATFORM_TRACKER];
private function __construct()
{
}
private function __clone()
{
}
public static function instance()
{
return self::$instance;
}
public function getRequestId(): string
{
return $this->requestId;
}
public function getStartTimestamp(): float
{
return $this->startTimestamp;
}
public function getPlatform(): string
{
return $this->platform;
}
public function getScript(): string
{
return $this->script;
}
public function getLogSequence(): int
{
return $this->logSequence;
}
public function isPlatformValid(): bool
{
return in_array($this->platform, self::PLATFORMS);
}
public function isPlatformAdmin(): bool
{
return $this->platform == self::PLATFORM_ADMIN;
}
public function isPlatformUser(): bool
{
return $this->platform == self::PLATFORM_USER;
}
public function isScriptAnnounce(): bool
{
return $this->script == 'announce';
}
2024-03-16 22:17:31 +08:00
public function incrementLogSequence(): void
2022-03-20 22:14:00 +08:00
{
$this->logSequence++;
}
2024-03-16 22:12:54 +08:00
private function getFirst(string $result): string
{
if (str_contains($result, ",")) {
return strstr($result, ",", true);
}
return $result;
}
2024-03-16 22:17:31 +08:00
public function getRequestSchema(): string
2022-04-07 00:54:05 +08:00
{
$schema = $this->retrieveFromServer(['HTTP_X_FORWARDED_PROTO', 'REQUEST_SCHEME', 'HTTP_SCHEME']);
if (empty($schema)) {
2022-04-07 15:44:43 +08:00
$https = $this->retrieveFromServer(['HTTPS']);
if ($https == 'on') {
2022-04-07 00:54:05 +08:00
$schema = 'https';
}
}
2024-03-16 22:12:54 +08:00
return $this->getFirst($schema);
2022-04-07 00:54:05 +08:00
}
2022-04-07 19:08:02 +08:00
public function getRequestHost(): string
{
2024-03-16 22:17:31 +08:00
$host = $this->retrieveFromServer(['HTTP_X_FORWARDED_HOST', 'HTTP_HOST', 'host'], true);
2024-03-16 22:12:54 +08:00
return $this->getFirst(strval($host));
2022-04-07 19:08:02 +08:00
}
2024-03-16 22:12:54 +08:00
public function getRequestIp(): string
2022-04-07 00:54:05 +08:00
{
2024-03-16 22:12:54 +08:00
$ip = $this->retrieveFromServer(['HTTP_CF_CONNECTING_IP', 'HTTP_X_FORWARDED_FOR', 'x-forwarded-for', 'HTTP_REMOTE_ADDR', 'REMOTE_ADDR'], true);
return $this->getFirst($ip);
2022-04-07 00:54:05 +08:00
}
2022-04-07 15:44:43 +08:00
private function retrieveFromServer(array $fields, bool $includeHeader = false)
2022-04-07 00:54:05 +08:00
{
if ($this->runningInOctane()) {
$servers = request()->server();
2022-04-07 15:44:43 +08:00
$headers = request()->header();
2022-04-07 00:54:05 +08:00
} else {
$servers = $_SERVER;
2022-04-07 15:44:43 +08:00
$headers = getallheaders();
2022-04-07 00:54:05 +08:00
}
foreach ($fields as $field) {
2022-04-07 15:44:43 +08:00
$result = $servers[$field] ?? null;
if ($result !== null && $result !== '') {
return $result;
}
if ($includeHeader) {
$result = $headers[$field] ?? null;
if (is_array($result)) {
$result = Arr::first($result);
}
if ($result !== null && $result !== '') {
return $result;
}
2022-04-07 00:54:05 +08:00
}
}
}
2022-03-20 22:14:00 +08:00
private function runningInOctane(): bool
{
if (defined('RUNNING_IN_OCTANE') && RUNNING_IN_OCTANE) {
return true;
}
return false;
}
private function generateRequestId(): string
{
$prefix = ($_SERVER['SCRIPT_FILENAME'] ?? '') . implode('', $_SERVER['argv'] ?? []);
$prefix = substr(md5($prefix), 0, 4);
// 4 + 23 = 27 characters, after replace '.', 26
$requestId = str_replace('.', '', uniqid($prefix, true));
$requestId .= bin2hex(random_bytes(3));
return $requestId;
}
public static function boot()
{
if (self::$booted) {
2022-04-06 21:32:57 +08:00
// file_put_contents('/tmp/reset.log', "booted\n",FILE_APPEND);
2022-03-20 22:14:00 +08:00
return;
}
2022-04-06 21:32:57 +08:00
// file_put_contents('/tmp/reset.log', "booting\n",FILE_APPEND);
2022-03-20 22:14:00 +08:00
$instance = new self();
$instance->setStartTimestamp();
$instance->setRequestId();
$instance->setScript();
$instance->setPlatform();
self::$instance = $instance;
self::$booted = true;
}
public static function flush()
{
self::$booted = false;
}
private function setRequestId()
{
2022-04-07 15:44:43 +08:00
$requestId = $this->retrieveFromServer(['HTTP_X_REQUEST_ID', 'REQUEST_ID', 'Request-Id', 'request-id'], true);
2022-03-20 22:14:00 +08:00
if (empty($requestId)) {
$requestId = $this->generateRequestId();
}
2022-04-07 15:44:43 +08:00
$this->requestId = (string)$requestId;
2022-03-20 22:14:00 +08:00
}
private function setScript()
{
2022-04-07 15:44:43 +08:00
$script = $this->retrieveFromServer(['SCRIPT_FILENAME', 'SCRIPT_NAME', 'Script', 'script'], true);
if (str_contains($script, '.')) {
$script = strstr(basename($script), '.', true);
2022-03-20 22:14:00 +08:00
}
2022-04-07 15:44:43 +08:00
$this->script = (string)$script;
2022-03-20 22:14:00 +08:00
}
private function setStartTimestamp()
{
$this->startTimestamp = microtime(true);
}
private function setPlatform()
{
2022-04-07 15:44:43 +08:00
$this->platform = (string)$this->retrieveFromServer(['HTTP_PLATFORM', 'Platform', 'platform'], true);
2022-03-20 22:14:00 +08:00
}
public static function js(string $js, string $position, bool $isFile, $key = null)
2022-04-03 16:03:47 +08:00
{
if ($isFile) {
$append = sprintf('<script type="text/javascript" src="%s"></script>', $js);
} else {
$append = sprintf('<script type="text/javascript">%s</script>', $js);
}
self::appendJsCss($append, $position, $key);
2022-04-03 16:03:47 +08:00
}
public static function css(string $css, string $position, bool $isFile, $key = null)
2022-04-03 16:03:47 +08:00
{
if ($isFile) {
$append = sprintf('<link rel="stylesheet" href="%s" type="text/css">', $css);
} else {
$append = sprintf('<style type="text/css">%s</style>', $css);
}
self::appendJsCss($append, $position, $key);
}
private static function appendJsCss($append, $position, $key = null)
{
2022-09-03 00:05:51 +08:00
$log = "position: $position, key: $key";
if ($key === null) {
$key = md5($append);
2022-09-03 00:05:51 +08:00
$log .= ", md5 key: $key";
}
2022-04-03 16:03:47 +08:00
if ($position == 'header') {
if (!isset(self::$appendHeaders[$key])) {
self::$appendHeaders[$key] = $append;
2022-09-03 00:05:51 +08:00
} else {
do_log("$log, [DUPLICATE]");
}
2022-04-03 16:03:47 +08:00
} elseif ($position == 'footer') {
if (!isset(self::$appendFooters[$key])) {
self::$appendFooters[$key] = $append;
2022-09-03 00:05:51 +08:00
} else {
do_log("$log, [DUPLICATE]");
}
2022-04-03 16:03:47 +08:00
} else {
throw new \InvalidArgumentException("Invalid position: $position");
}
}
public static function getAppendHeaders(): array
{
return self::$appendHeaders;
}
public static function getAppendFooters(): array
{
return self::$appendFooters;
}
2025-04-19 02:06:51 +07:00
public static function addTranslationNamespace($path, $namespace): void
2022-08-10 17:38:05 +08:00
{
if (empty($namespace)) {
throw new \InvalidArgumentException("namespace can not be empty");
}
self::$translationNamespaces[$namespace] = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
2025-04-19 02:06:51 +07:00
if (IN_NEXUS) {
//只有 Nexus 下需要Laravel 下是通过 configurePackage 中 hasTranslations() 加载的
self::getTranslator()->addNamespace($namespace, $path);
}
2022-08-10 17:38:05 +08:00
}
public static function trans($key, $replace = [], $locale = null)
{
2025-04-19 02:06:51 +07:00
if (is_null($locale)) {
$locale = get_langfolder_cookie(true);
2022-08-10 17:38:05 +08:00
}
2025-04-19 02:06:51 +07:00
if (IN_NEXUS) {
return self::getTranslator()->trans($key, $replace, $locale);
} else {
return trans($key, $replace, $locale);
2022-08-10 17:38:05 +08:00
}
2025-04-19 02:06:51 +07:00
// if (empty(self::$translations)) {
// //load from default lang dir
// $langDir = ROOT_PATH . 'resources/lang/';
// self::loadTranslations($langDir);
// //load from namespace
// foreach (self::$translationNamespaces as $namespace => $path) {
// self::loadTranslations($path, $namespace);
// }
// }
// return self::getTranslation($key, $replace, $locale ?? get_langfolder_cookie(true));
2022-08-10 17:38:05 +08:00
}
private static function loadTranslations($path, $namespace = null)
{
2022-11-23 17:34:45 +08:00
do_log("path: $path, namespace: $namespace", 'debug');
2022-08-10 17:38:05 +08:00
$files = glob($path . '*/*');
foreach ($files as $file) {
if (!is_file($file)) {
2022-11-23 17:34:45 +08:00
do_log("file: $file, is not file", 'debug');
2022-08-10 17:38:05 +08:00
continue;
}
if (!is_readable($file)) {
do_log("[TRANSLATION_FILE_NOT_READABLE], $file");
}
$values = require $file;
$setKey = substr($file, strlen($path));
if (substr($setKey, -4) == '.php') {
$setKey = substr($setKey, 0, -4);
}
$setKey = str_replace('/', '.', $setKey);
if ($namespace !== null) {
$setKey = "$namespace.$setKey";
}
2022-11-23 17:34:45 +08:00
do_log("path: $path, namespace: $namespace, file: $file, setKey: $setKey", 'debug');
2022-08-10 17:38:05 +08:00
arr_set(self::$translations, $setKey, $values);
}
}
private static function getTranslation($key, $replace = [], $locale = null)
{
if (!$locale) {
$lang = get_langfolder_cookie();
$locale = \App\Http\Middleware\Locale::$languageMaps[$lang] ?? 'en';
}
$getKey = self::getTranslationGetKey($key, $locale);
$result = arr_get(self::$translations, $getKey);
if (empty($result) && $locale != 'en') {
do_log("original getKey: $getKey can not get any translations", 'error');
$getKey = self::getTranslationGetKey($key, 'en');
$result = arr_get(self::$translations, $getKey);
}
if (!empty($replace)) {
$search = array_map(function ($value) {return ":$value";}, array_keys($replace));
$result = str_replace($search, array_values($replace), $result);
}
do_log("key: $key, replace: " . nexus_json_encode($replace) . ", locale: $locale, getKey: $getKey, result: $result", 'debug');
return $result;
}
private static function getTranslationGetKey($key, $locale): string
{
$namespace = strstr($key, '::', true);
if ($namespace !== false) {
$getKey = sprintf('%s.%s.%s', $namespace, $locale, substr($key, strlen($namespace) + 2));
} else {
$getKey = $locale . "." . $key;
}
// do_log("key: $key, locale: $locale, namespace: $namespace, getKey: $getKey", 'debug');
return $getKey;
}
2022-04-03 16:03:47 +08:00
2025-04-19 02:06:51 +07:00
private static function getTranslator(): NexusTranslator
{
if (is_null(self::$translator)) {
self::$translator = new NexusTranslator(Locale::getDefault());
}
return self::$translator;
}
2025-06-10 21:50:41 +07:00
private static function getQueueManager(): Manager
{
if (is_null(self::$queueManager)) {
$container = Container::getInstance();
$redisConfig = nexus_config('nexus.redis');
$redisConnectionName = "my_redis_connection";
$container->singleton('redis', function ($app) use ($redisConfig, $redisConnectionName) {
$redisDriver = "phpredis";
// 这里的配置应该匹配 redis.php 配置文件中的 default 连接
$connectionConfig = [
'client' => $redisDriver,
$redisConnectionName => $redisConfig
];
return new RedisManager($app, $redisDriver, $connectionConfig);
});
$queueManager = new Manager($container);
$queueManager->addConnection([
'driver' => 'redis',
'host' => $redisConfig['host'],
'password' => $redisConfig['password'],
'queue' => 'nexus_queue', // 队列名称
'connection' => $redisConnectionName, // Redis 连接名称,类似注册的 'redis' 服务中的 'default'
], self::QUEUE_CONNECTION_NAME); // 将这个 queue 连接起个不一样的名字
$queueManager->setAsGlobal();
self::$queueManager = $queueManager;
}
return self::$queueManager;
}
public static function dispatchQueueJob(ShouldQueue $job): void
{
self::getQueueManager()->connection(self::QUEUE_CONNECTION_NAME)->push($job);
2025-06-17 20:54:18 +07:00
do_log("dispatchQueueJob: " . nexus_json_encode($job));
2025-06-10 21:50:41 +07:00
}
2022-03-20 22:14:00 +08:00
}