Merge branch '1.7' into php8

This commit is contained in:
xiaomlove
2022-03-31 16:43:51 +08:00
140 changed files with 21731 additions and 273 deletions
+73
View File
@@ -0,0 +1,73 @@
<?php
namespace Nexus\Core;
class Constant
{
/**
* @todo check RoadRunner environment
*/
public function define()
{
if (!empty($_SERVER['PWD']) && str_contains($_SERVER['PWD'], 'vendor/laravel/octane/bin')) {
$this->defineForOctane();
} else {
$this->defineForFPM();
}
$this->defineCommon();
}
private function defineForFPM()
{
defined('CURRENT_SCRIPT') || define('CURRENT_SCRIPT', strstr(basename($_SERVER['SCRIPT_FILENAME']), '.', true));
defined('CURRENT_PLATFORM') || define('CURRENT_PLATFORM', $_SERVER['HTTP_PLATFORM'] ?? '');
$requestId = '';
if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) {
$requestId = $_SERVER['HTTP_X_REQUEST_ID'];
} elseif (!empty($_SERVER['REQUEST_ID'])) {
$requestId = $_SERVER['REQUEST_ID'];
}
define('REQUEST_ID', $requestId ?: $this->generateRequestId());
}
private function defineForOctane()
{
$request = request();
defined('CURRENT_SCRIPT') || define('CURRENT_SCRIPT', $request->header('script_filename', ''));
defined('CURRENT_PLATFORM') || define('CURRENT_PLATFORM', $request->header('platform', ''));
$requestId = $request->header('request_id', '');
define('REQUEST_ID', $requestId ?: $this->generateRequestId());
}
private function generateRequestId()
{
$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;
}
private function defineCommon()
{
defined('VERSION_NUMBER') || define('VERSION_NUMBER', '1.6.0');
defined('RELEASE_DATE') || define('RELEASE_DATE', '2022-03-14');
defined('ROOT_PATH') || define('ROOT_PATH', dirname(dirname(__DIR__)) . '/');
defined('IN_TRACKER') || define('IN_TRACKER', true);
defined('PROJECTNAME') || define("PROJECTNAME","NexusPHP");
defined('NEXUSPHPURL') || define("NEXUSPHPURL","https://nexusphp.org");
defined('NEXUSWIKIURL') || define("NEXUSWIKIURL","https://doc.nexusphp.org");
defined('VERSION') || define("VERSION","Powered by <a href=\"aboutnexus.php\">".PROJECTNAME."</a>");
defined('THISTRACKER') || define("THISTRACKER","General");
defined('PLATFORM_ADMIN') || define('PLATFORM_ADMIN', 'admin');
defined('PLATFORM_USER') || define('PLATFORM_USER', 'user');
defined('PLATFORMS') || define('PLATFORMS', [PLATFORM_ADMIN, PLATFORM_USER]);
defined('IS_PLATFORM_ADMIN') || define('IS_PLATFORM_ADMIN', CURRENT_PLATFORM == PLATFORM_ADMIN);
defined('IS_PLATFORM_USER') || define('IS_PLATFORM_USER', CURRENT_PLATFORM == PLATFORM_USER);
defined('IS_ANNOUNCE') || define('IS_ANNOUNCE', CURRENT_SCRIPT == 'announce');
}
}
+64
View File
@@ -4,7 +4,9 @@ namespace Nexus\Database;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class NexusDB
{
@@ -267,6 +269,68 @@ class NexusDB
return DB::transaction($callback, $attempts);
}
public static function remember($key, $ttl, \Closure $callback)
{
if (IN_NEXUS) {
global $Cache;
$result = $Cache->get_value($key);
if ($result === false) {
do_log("cache miss, get from database.");
$result = $callback();
$Cache->cache_value($key, $result, $ttl);
} else {
do_log("cache hit.");
}
return $result;
} else {
return Cache::remember($key, $ttl, $callback);
}
}
public static function cache_put($key, $value, $ttl = 3600)
{
if (IN_NEXUS) {
global $Cache;
return $Cache->cache_value($key, $value, $ttl);
} else {
return Cache::put($key, $value, $ttl);
}
}
public static function cache_get($key)
{
if (IN_NEXUS) {
global $Cache;
return $Cache->get_value($key);
} else {
return Cache::get($key);
}
}
public static function cache_del($key)
{
if (IN_NEXUS) {
global $Cache;
$Cache->delete_value($key, true);
} else {
Cache::forget($key);
$langList = get_langfolder_list();
foreach ($langList as $lf) {
Cache::forget($lf . '_' . $key);
}
}
}
public static function redis()
{
if (IN_NEXUS) {
global $Cache;
$Cache->getRedis();
} else {
Redis::connection()->client();
}
}
public static function getMysqlColumnInfo($table, $column)
{
static $driver;
+3
View File
@@ -425,6 +425,9 @@ class Install
$this->doLog("[CREATE ENV] key: $key, new value: $value from example.");
$newData[$key] = $value;
}
if ($key == 'CACHE_DRIVER') {
$newData[$key] = 'redis';
}
if ($scene == 'install' || !file_exists($envFile)) {
if ($key == 'APP_ENV') {
$newData[$key] = 'production';
+2
View File
@@ -3,6 +3,8 @@ ini_set('error_reporting', E_ALL);
ini_set('display_errors', 0);
define('IN_NEXUS', true);
define('NEXUS_START', microtime(true));
require ROOT_PATH . 'include/globalfunctions.php';
require ROOT_PATH . 'include/functions.php';
require ROOT_PATH . 'vendor/autoload.php';
require ROOT_PATH . 'nexus/Database/helpers.php';
require ROOT_PATH . 'include/constants.php';
+1
View File
@@ -352,6 +352,7 @@ return array (
'watermarkquality' => '85',
'altthumbwidth' => '180',
'altthumbheight' => '135',
'download_support_passkey' => 'yes',
),
'advertisement' =>
array (
+176
View File
@@ -0,0 +1,176 @@
<?php
namespace Nexus;
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;
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';
}
public function incrementLogSequence()
{
$this->logSequence++;
}
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) {
return;
}
$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()
{
$requestId = '';
if ($this->runningInOctane()) {
$request = request();
$requestId = $request->server('request_id', $request->header('request_id', ''));
} else {
if (!empty($_SERVER['HTTP_X_REQUEST_ID'])) {
$requestId = $_SERVER['HTTP_X_REQUEST_ID'];
} elseif (!empty($_SERVER['REQUEST_ID'])) {
$requestId = $_SERVER['REQUEST_ID'];
}
}
if (empty($requestId)) {
$requestId = $this->generateRequestId();
}
$this->requestId = $requestId;
}
private function setScript()
{
if ($this->runningInOctane()) {
$request = request();
$script = $request->header('script_filename', '');
} else {
$script = strstr(basename($_SERVER['SCRIPT_FILENAME']), '.', true);
}
$this->script = $script;
}
private function setStartTimestamp()
{
$this->startTimestamp = microtime(true);
}
private function setPlatform()
{
if ($this->runningInOctane()) {
$request = request();
$platform = $request->header('platform', '');
} else {
$platform = $_SERVER['HTTP_PLATFORM'] ?? '';
}
$this->platform = $platform;
}
}