fix:修复BUG/升级1.1.6版本

This commit is contained in:
Ying
2023-04-25 20:11:49 +08:00
parent 445e5f9662
commit 6a6866bbaf
2357 changed files with 456920 additions and 140567 deletions

View File

@@ -20,13 +20,14 @@ class Template extends ThinkPHP
* @param string $template
* @param array $vars
* @param string|null $app
* @return array|false|string|string[]|null
* @param string|null $plugin
* @return string
* @throws DataNotFoundException
* @throws DbException
* @throws InvalidArgumentException
* @throws ModelNotFoundException
*/
public static function render(string $template, array $vars, string $app = null)
public static function render(string $template, array $vars, string $app = null, string $plugin = null): string
{
$content = parent::render($template, $vars, $app);
if (saenv('minify_page')) {

View File

@@ -16,8 +16,8 @@ use Dotenv\Dotenv;
use support\Log;
use Webman\Bootstrap;
use Webman\Config;
use Webman\Route;
use Webman\Middleware;
use Webman\Route;
use Webman\Util;
$worker = $worker ?? null;
@@ -29,16 +29,16 @@ set_error_handler(function ($level, $message, $file = '', $line = 0) {
});
if ($worker) {
register_shutdown_function(function ($start_time) {
if (time() - $start_time <= 1) {
register_shutdown_function(function ($startTime) {
if (time() - $startTime <= 0.1) {
sleep(1);
}
}, time());
}
if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
Dotenv::createUnsafeImmutable(base_path())->load();
if (method_exists('Dotenv\Dotenv', 'createUnsafeMutable')) {
Dotenv::createUnsafeMutable(base_path())->load();
} else {
Dotenv::createMutable(base_path())->load();
}
@@ -67,30 +67,30 @@ foreach (config('plugin', []) as $firm => $projects) {
}
}
Middleware::load(config('middleware', []), '');
Middleware::load(config('middleware', []));
foreach (config('plugin', []) as $firm => $projects) {
foreach ($projects as $name => $project) {
if (!is_array($project) || $name === 'static') {
continue;
}
Middleware::load($project['middleware'] ?? [], '');
Middleware::load($project['middleware'] ?? []);
}
Middleware::load($projects['middleware'] ?? [], $firm);
if ($static_middlewares = config("plugin.$firm.static.middleware")) {
Middleware::load(['__static__' => $static_middlewares], $firm);
if ($staticMiddlewares = config("plugin.$firm.static.middleware")) {
Middleware::load(['__static__' => $staticMiddlewares], $firm);
}
}
Middleware::load(['__static__' => config('static.middleware', [])], '');
Middleware::load(['__static__' => config('static.middleware', [])]);
foreach (config('bootstrap', []) as $class_name) {
if (!class_exists($class_name)) {
$log = "Warning: Class $class_name setting in config/bootstrap.php not found\r\n";
foreach (config('bootstrap', []) as $className) {
if (!class_exists($className)) {
$log = "Warning: Class $className setting in config/bootstrap.php not found\r\n";
echo $log;
Log::error($log);
continue;
}
/** @var Bootstrap $class_name */
$class_name::start($worker);
/** @var Bootstrap $className */
$className::start($worker);
}
foreach (config('plugin', []) as $firm => $projects) {
@@ -98,26 +98,27 @@ foreach (config('plugin', []) as $firm => $projects) {
if (!is_array($project)) {
continue;
}
foreach ($project['bootstrap'] ?? [] as $class_name) {
if (!class_exists($class_name)) {
$log = "Warning: Class $class_name setting in config/plugin/$firm/$name/bootstrap.php not found\r\n";
foreach ($project['bootstrap'] ?? [] as $className) {
if (!class_exists($className)) {
$log = "Warning: Class $className setting in config/plugin/$firm/$name/bootstrap.php not found\r\n";
echo $log;
Log::error($log);
continue;
}
/** @var Bootstrap $class_name */
$class_name::start($worker);
/** @var Bootstrap $className */
$className::start($worker);
}
}
foreach ($projects['bootstrap'] ?? [] as $class_name) {
if (!class_exists($class_name)) {
$log = "Warning: Class $class_name setting in plugin/$firm/config/bootstrap.php not found\r\n";
foreach ($projects['bootstrap'] ?? [] as $className) {
/** @var string $className */
if (!class_exists($className)) {
$log = "Warning: Class $className setting in plugin/$firm/config/bootstrap.php not found\r\n";
echo $log;
Log::error($log);
continue;
}
/** @var Bootstrap $class_name */
$class_name::start($worker);
/** @var Bootstrap $className */
$className::start($worker);
}
}

View File

@@ -17,132 +17,171 @@ use support\Container;
use support\Request;
use support\Response;
use support\Translation;
use support\view\Raw;
use support\view\Blade;
use support\view\Raw;
use support\view\ThinkPHP;
use support\view\Twig;
use Workerman\Worker;
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Webman\App;
use Webman\Config;
use Webman\Route;
// Webman version
define('WEBMAN_VERSION', '1.4');
use Workerman\Worker;
// Project base path
define('BASE_PATH', dirname(__DIR__));
/**
* return the program execute directory
* @param string $path
* @return string
*/
function run_path(string $path = ''): string
{
static $runPath = '';
if (!$runPath) {
$runPath = is_phar() ? dirname(Phar::running(false)) : BASE_PATH;
}
return path_combine($runPath, $path);
}
/**
* if the param $path equal false,will return this program current execute directory
* @param string|false $path
* @return string
*/
function base_path($path = ''): string
{
if (false === $path) {
return run_path();
}
return path_combine(BASE_PATH, $path);
}
/**
* App path
* @param string $path
* @return string
*/
function app_path(string $path = ''): string
{
return path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'app', $path);
}
/**
* Public path
* @param string $path
* @return string
*/
function public_path(string $path = ''): string
{
static $publicPath = '';
if (!$publicPath) {
$publicPath = \config('app.public_path') ?: run_path('public');
}
return path_combine($publicPath, $path);
}
/**
* Config path
* @param string $path
* @return string
*/
function config_path(string $path = ''): string
{
return path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'config', $path);
}
/**
* Runtime path
* @param string $path
* @return string
*/
function runtime_path(string $path = ''): string
{
static $runtimePath = '';
if (!$runtimePath) {
$runtimePath = \config('app.runtime_path') ?: run_path('runtime');
}
return path_combine($runtimePath, $path);
}
if (!function_exists('halt')) {
/**
* 调试变量并且中断输出
* @param mixed $vars 调试变量或者信息
*/
function halt(...$vars)
{
ob_start();
$cloner = new VarCloner();
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
$dumper = new HtmlDumper();
$dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);
foreach ($vars as $var) {
$dumper->dump($cloner->cloneVar($var));
}
$ob_response = (string)ob_get_clean();
throw new \RuntimeException($ob_response, 600);
}
}
if (!function_exists('get_env')) {
/**
* Get environment variable
*/
function get_env($var)
{
$dir = str_replace('\\', '/', realpath(__DIR__ . '/../'));
$env_path = $dir . '/.env';
$env_info = parse_ini_file($env_path, true);
return $env_info[$var] ?? '';
}
}
/**
* Generate paths based on given information
* @param string $front
* @param string $back
* @return string
*/
function path_combine(string $front, string $back)
function path_combine(string $front, string $back): string
{
return $front . ($back ? (DIRECTORY_SEPARATOR . ltrim($back, DIRECTORY_SEPARATOR)) : $back);
}
/**
* return the program execute directory
* @param string $path
* @return string
*/
function run_path(string $path = '')
{
static $run_path = '';
if (!$run_path) {
$run_path = \is_phar() ? \dirname(\Phar::running(false)) : BASE_PATH;
}
return \path_combine($run_path, $path);
}
/**
* if the param $path equal false,will return this program current execute directory
* @param string|false $path
* @return false|string
*/
function base_path($path = '')
{
if (false === $path) {
return \run_path();
}
return \path_combine(BASE_PATH, $path);
}
/**
* @param string $path
* @return string
*/
function app_path(string $path = '')
{
return \path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'app', $path);
}
/**
* @param string $path
* @return string
*/
function public_path(string $path = '')
{
static $public_path = '';
if (!$public_path) {
$public_path = \config('app.public_path') ? : \run_path('public');
}
return \path_combine($public_path, $path);
}
/**
* @param string $path
* @return string
*/
function config_path(string $path = '')
{
return \path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'config', $path);
}
/**
* Phar support.
* Compatible with the 'realpath' function in the phar file.
* @param string $path
* @return string
*/
function runtime_path(string $path = '')
{
static $runtime_path = '';
if (!$runtime_path) {
$runtime_path = \config('app.runtime_path') ? : \run_path('runtime');
}
return \path_combine($runtime_path, $path);
}
/**
* Response
* @param int $status
* @param array $headers
* @param string $body
* @return Response
*/
function response($body = '', $status = 200, $headers = [])
function response(string $body = '', int $status = 200, array $headers = []): Response
{
return new Response($status, $headers, $body);
}
/**
* Json response
* @param $data
* @param int $options
* @return Response
*/
function json($data, $options = JSON_UNESCAPED_UNICODE)
function json($data, int $options = JSON_UNESCAPED_UNICODE): Response
{
return new Response(200, ['Content-Type' => 'application/json'], \json_encode($data, $options));
return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
}
/**
* Xml response
* @param $xml
* @return Response
*/
function xml($xml)
function xml($xml): Response
{
if ($xml instanceof SimpleXMLElement) {
$xml = $xml->asXML();
@@ -151,25 +190,27 @@ function xml($xml)
}
/**
* Jsonp response
* @param $data
* @param string $callback_name
* @param string $callbackName
* @return Response
*/
function jsonp($data, $callback_name = 'callback')
function jsonp($data, string $callbackName = 'callback'): Response
{
if (!\is_scalar($data) && null !== $data) {
$data = \json_encode($data);
if (!is_scalar($data) && null !== $data) {
$data = json_encode($data);
}
return new Response(200, [], "$callback_name($data)");
return new Response(200, [], "$callbackName($data)");
}
/**
* Redirect response
* @param string $location
* @param int $status
* @param array $headers
* @return Response
*/
function redirect(string $location, int $status = 302, array $headers = [])
function redirect(string $location, int $status = 302, array $headers = []): Response
{
$response = new Response($status, ['Location' => $location]);
if (!empty($headers)) {
@@ -179,65 +220,75 @@ function redirect(string $location, int $status = 302, array $headers = [])
}
/**
* @param $template
* View response
* @param string $template
* @param array $vars
* @param null $app
* @param string|null $app
* @param string|null $plugin
* @return Response
*/
function view(string $template, array $vars = [], string $app = null)
function view(string $template, array $vars = [], string $app = null, string $plugin = null): Response
{
$request = \request();
$plugin = $request->plugin ?? '';
$plugin = $plugin === null ? ($request->plugin ?? '') : $plugin;
$handler = \config($plugin ? "plugin.$plugin.view.handler" : 'view.handler');
return new Response(200, [], $handler::render($template, $vars, $app));
return new Response(200, [], $handler::render($template, $vars, $app, $plugin));
}
/**
* Raw view response
* @param string $template
* @param array $vars
* @param string|null $app
* @return Response
* @throws Throwable
*/
function raw_view(string $template, array $vars = [], string $app = null)
function raw_view(string $template, array $vars = [], string $app = null): Response
{
return new Response(200, [], Raw::render($template, $vars, $app));
}
/**
* Blade view response
* @param string $template
* @param array $vars
* @param string|null $app
* @return Response
*/
function blade_view(string $template, array $vars = [], string $app = null)
function blade_view(string $template, array $vars = [], string $app = null): Response
{
return new Response(200, [], Blade::render($template, $vars, $app));
}
/**
* Think view response
* @param string $template
* @param array $vars
* @param string|null $app
* @return Response
*/
function think_view(string $template, array $vars = [], string $app = null)
function think_view(string $template, array $vars = [], string $app = null): Response
{
return new Response(200, [], ThinkPHP::render($template, $vars, $app));
}
/**
* Twig view response
* @param string $template
* @param array $vars
* @param string|null $app
* @return Response
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
function twig_view(string $template, array $vars = [], string $app = null)
function twig_view(string $template, array $vars = [], string $app = null): Response
{
return new Response(200, [], Twig::render($template, $vars, $app));
}
/**
* Get request
* @return \Webman\Http\Request|Request|null
*/
function request()
@@ -246,6 +297,7 @@ function request()
}
/**
* Get config
* @param string|null $key
* @param $default
* @return array|mixed|null
@@ -256,11 +308,12 @@ function config(string $key = null, $default = null)
}
/**
* Create url
* @param string $name
* @param ...$parameters
* @return string
*/
function route(string $name, ...$parameters)
function route(string $name, ...$parameters): string
{
$route = Route::getByName($name);
if (!$route) {
@@ -271,14 +324,15 @@ function route(string $name, ...$parameters)
return $route->url();
}
if (\is_array(\current($parameters))) {
$parameters = \current($parameters);
if (is_array(current($parameters))) {
$parameters = current($parameters);
}
return $route->url($parameters);
}
/**
* Session
* @param mixed $key
* @param mixed $default
* @return mixed
@@ -289,14 +343,14 @@ function session($key = null, $default = null)
if (null === $key) {
return $session;
}
if (\is_array($key)) {
if (is_array($key)) {
$session->put($key);
return null;
}
if (\strpos($key, '.')) {
$key_array = \explode('.', $key);
if (strpos($key, '.')) {
$keyArray = explode('.', $key);
$value = $session->all();
foreach ($key_array as $index) {
foreach ($keyArray as $index) {
if (!isset($value[$index])) {
return $default;
}
@@ -308,43 +362,44 @@ function session($key = null, $default = null)
}
/**
* Translation
* @param string $id
* @param array $parameters
* @param string|null $domain
* @param string|null $locale
* @return string
*/
function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
{
$res = Translation::trans($id, $parameters, $domain, $locale);
return $res === '' ? $id : $res;
}
/**
* @param null|string $locale
* Locale
* @param string|null $locale
* @return string
*/
function locale(string $locale = null)
function locale(string $locale = null): string
{
if (!$locale) {
return Translation::getLocale();
}
Translation::setLocale($locale);
return $locale;
}
/**
* 404 not found
*
* @return Response
*/
function not_found()
function not_found(): Response
{
return new Response(404, [], \file_get_contents(public_path() . '/404.html'));
return new Response(404, [], file_get_contents(public_path() . '/404.html'));
}
/**
* Copy dir.
*
* Copy dir
* @param string $source
* @param string $dest
* @param bool $overwrite
@@ -352,46 +407,46 @@ function not_found()
*/
function copy_dir(string $source, string $dest, bool $overwrite = false)
{
if (\is_dir($source)) {
if (is_dir($source)) {
if (!is_dir($dest)) {
\mkdir($dest);
mkdir($dest);
}
$files = \scandir($source);
$files = scandir($source);
foreach ($files as $file) {
if ($file !== "." && $file !== "..") {
\copy_dir("$source/$file", "$dest/$file");
copy_dir("$source/$file", "$dest/$file");
}
}
} else if (\file_exists($source) && ($overwrite || !\file_exists($dest))) {
\copy($source, $dest);
} else if (file_exists($source) && ($overwrite || !file_exists($dest))) {
copy($source, $dest);
}
}
/**
* Remove dir.
*
* Remove dir
* @param string $dir
* @return bool
*/
function remove_dir(string $dir)
function remove_dir(string $dir): bool
{
if (\is_link($dir) || \is_file($dir)) {
return \unlink($dir);
if (is_link($dir) || is_file($dir)) {
return unlink($dir);
}
$files = \array_diff(\scandir($dir), array('.', '..'));
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(\is_dir("$dir/$file") && !\is_link($dir)) ? \remove_dir("$dir/$file") : \unlink("$dir/$file");
(is_dir("$dir/$file") && !is_link($dir)) ? remove_dir("$dir/$file") : unlink("$dir/$file");
}
return \rmdir($dir);
return rmdir($dir);
}
/**
* Bind worker
* @param $worker
* @param $class
*/
function worker_bind($worker, $class)
{
$callback_map = [
$callbackMap = [
'onConnect',
'onMessage',
'onClose',
@@ -399,27 +454,29 @@ function worker_bind($worker, $class)
'onBufferFull',
'onBufferDrain',
'onWorkerStop',
'onWebSocketConnect'
'onWebSocketConnect',
'onWorkerReload'
];
foreach ($callback_map as $name) {
if (\method_exists($class, $name)) {
foreach ($callbackMap as $name) {
if (method_exists($class, $name)) {
$worker->$name = [$class, $name];
}
}
if (\method_exists($class, 'onWorkerStart')) {
\call_user_func([$class, 'onWorkerStart'], $worker);
if (method_exists($class, 'onWorkerStart')) {
call_user_func([$class, 'onWorkerStart'], $worker);
}
}
/**
* @param $process_name
* Start worker
* @param $processName
* @param $config
* @return void
*/
function worker_start($process_name, $config)
function worker_start($processName, $config)
{
$worker = new Worker($config['listen'] ?? null, $config['context'] ?? []);
$property_map = [
$propertyMap = [
'count',
'user',
'group',
@@ -428,81 +485,66 @@ function worker_start($process_name, $config)
'transport',
'protocol',
];
$worker->name = $process_name;
foreach ($property_map as $property) {
$worker->name = $processName;
foreach ($propertyMap as $property) {
if (isset($config[$property])) {
$worker->$property = $config[$property];
}
}
$worker->onWorkerStart = function ($worker) use ($config) {
require_once \base_path() . '/support/bootstrap.php';
foreach ($config['services'] ?? [] as $server) {
if (!\class_exists($server['handler'])) {
echo "process error: class {$server['handler']} not exists\r\n";
continue;
}
$listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
if (isset($server['listen'])) {
echo "listen: {$server['listen']}\n";
}
$instance = Container::make($server['handler'], $server['constructor'] ?? []);
\worker_bind($listen, $instance);
$listen->listen();
}
require_once base_path('/support/bootstrap.php');
if (isset($config['handler'])) {
if (!\class_exists($config['handler'])) {
if (!class_exists($config['handler'])) {
echo "process error: class {$config['handler']} not exists\r\n";
return;
}
$instance = Container::make($config['handler'], $config['constructor'] ?? []);
\worker_bind($worker, $instance);
worker_bind($worker, $instance);
}
};
}
/**
* Phar support.
* Compatible with the 'realpath' function in the phar file.
*
* @param string $file_path
* Get realpath
* @param string $filePath
* @return string
*/
function get_realpath(string $file_path): string
function get_realpath(string $filePath): string
{
if (\strpos($file_path, 'phar://') === 0) {
return $file_path;
if (strpos($filePath, 'phar://') === 0) {
return $filePath;
} else {
return \realpath($file_path);
return realpath($filePath);
}
}
/**
* Is phar
* @return bool
*/
function is_phar()
function is_phar(): bool
{
return \class_exists(\Phar::class, false) && Phar::running();
return class_exists(Phar::class, false) && Phar::running();
}
/**
* Get cpu count
* @return int
*/
function cpu_count()
function cpu_count(): int
{
// Windows does not support the number of processes setting.
if (\DIRECTORY_SEPARATOR === '\\') {
if (DIRECTORY_SEPARATOR === '\\') {
return 1;
}
$count = 4;
if (\is_callable('shell_exec')) {
if (\strtolower(PHP_OS) === 'darwin') {
$count = (int)\shell_exec('sysctl -n machdep.cpu.core_count');
if (is_callable('shell_exec')) {
if (strtolower(PHP_OS) === 'darwin') {
$count = (int)shell_exec('sysctl -n machdep.cpu.core_count');
} else {
$count = (int)\shell_exec('nproc');
$count = (int)shell_exec('nproc');
}
}
return $count > 0 ? $count : 4;