workerman版本4.2.1,webman-framework版本1.6.6,php版本8.3

This commit is contained in:
wandoubaba 2024-11-29 15:40:03 +08:00
parent 49710a25d9
commit 08b9976af6
13 changed files with 276 additions and 146 deletions

10
app/process/Http.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace app\process;
use Webman\App;
class Http extends App
{
}

View File

@ -12,7 +12,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
*/ */
namespace process; namespace app\process;
use FilesystemIterator; use FilesystemIterator;
use RecursiveDirectoryIterator; use RecursiveDirectoryIterator;
@ -37,6 +37,11 @@ class Monitor
*/ */
protected $extensions = []; protected $extensions = [];
/**
* @var array
*/
protected $loadedFiles = [];
/** /**
* @var string * @var string
*/ */
@ -84,6 +89,12 @@ class Monitor
static::resume(); static::resume();
$this->paths = (array)$monitorDir; $this->paths = (array)$monitorDir;
$this->extensions = $monitorExtensions; $this->extensions = $monitorExtensions;
foreach (get_included_files() as $index => $file) {
$this->loadedFiles[$file] = $index;
if (strpos($file, 'webman-framework/src/support/App.php')) {
break;
}
}
if (!Worker::getAllWorkers()) { if (!Worker::getAllWorkers()) {
return; return;
} }
@ -134,13 +145,17 @@ class Monitor
} }
// check mtime // check mtime
if (in_array($file->getExtension(), $this->extensions, true) && $lastMtime < $file->getMTime()) { if (in_array($file->getExtension(), $this->extensions, true) && $lastMtime < $file->getMTime()) {
$lastMtime = $file->getMTime();
if (DIRECTORY_SEPARATOR === '/' && isset($this->loadedFiles[$file->getRealPath()])) {
echo "$file updated but cannot be reloaded because only auto-loaded files support reload.\n";
continue;
}
$var = 0; $var = 0;
exec('"'.PHP_BINARY . '" -l ' . $file, $out, $var); exec('"'.PHP_BINARY . '" -l ' . $file, $out, $var);
$lastMtime = $file->getMTime();
if ($var) { if ($var) {
continue; continue;
} }
echo $file . " update and reload\n"; echo $file . " updated and reload\n";
// send SIGUSR1 signal to master process for reload // send SIGUSR1 signal to master process for reload
if (DIRECTORY_SEPARATOR === '/') { if (DIRECTORY_SEPARATOR === '/') {
posix_kill(posix_getppid(), SIGUSR1); posix_kill(posix_getppid(), SIGUSR1);

View File

@ -4,7 +4,7 @@ read -r -d '' DOCKER_COMMAND <<'EOF'
docker run --rm \ docker run --rm \
-v "$(pwd)":/app \ -v "$(pwd)":/app \
-w /app \ -w /app \
quay.io/wandoubaba517/php:8.2-workerman \ quay.io/wandoubaba517/php:8.3-workerman \
composer composer
EOF EOF

View File

@ -25,8 +25,9 @@
}, },
"require": { "require": {
"php": ">=7.2", "php": ">=7.2",
"workerman/webman-framework": "^1.5.0", "workerman/webman-framework": "^1.6.6",
"monolog/monolog": "^3.5" "monolog/monolog": "^2.0",
"workerman/workerman": "^4.2.1"
}, },
"suggest": { "suggest": {
"ext-event": "For better performance. " "ext-event": "For better performance. "
@ -52,5 +53,7 @@
"pre-package-uninstall": [ "pre-package-uninstall": [
"support\\Plugin::uninstall" "support\\Plugin::uninstall"
] ]
} },
"minimum-stability": "dev",
"prefer-stable": true
} }

149
composer.lock generated
View File

@ -4,45 +4,52 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c02c3a666509c2442d4834e05169b0ea", "content-hash": "afbf9db9e4592c74428dc015992026ea",
"packages": [ "packages": [
{ {
"name": "monolog/monolog", "name": "monolog/monolog",
"version": "3.7.0", "version": "2.10.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/Seldaek/monolog.git", "url": "https://github.com/Seldaek/monolog.git",
"reference": "f4393b648b78a5408747de94fca38beb5f7e9ef8" "reference": "5cf826f2991858b54d5c3809bee745560a1042a7"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/f4393b648b78a5408747de94fca38beb5f7e9ef8", "url": "https://api.github.com/repos/Seldaek/monolog/zipball/5cf826f2991858b54d5c3809bee745560a1042a7",
"reference": "f4393b648b78a5408747de94fca38beb5f7e9ef8", "reference": "5cf826f2991858b54d5c3809bee745560a1042a7",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"php": ">=8.1", "php": ">=7.2",
"psr/log": "^2.0 || ^3.0" "psr/log": "^1.0.1 || ^2.0 || ^3.0"
}, },
"provide": { "provide": {
"psr/log-implementation": "3.0.0" "psr/log-implementation": "1.0.0 || 2.0.0 || 3.0.0"
}, },
"require-dev": { "require-dev": {
"aws/aws-sdk-php": "^3.0", "aws/aws-sdk-php": "^2.4.9 || ^3.0",
"doctrine/couchdb": "~1.0@dev", "doctrine/couchdb": "~1.0@dev",
"elasticsearch/elasticsearch": "^7 || ^8", "elasticsearch/elasticsearch": "^7 || ^8",
"ext-json": "*", "ext-json": "*",
"graylog2/gelf-php": "^1.4.2 || ^2.0", "graylog2/gelf-php": "^1.4.2 || ^2@dev",
"guzzlehttp/guzzle": "^7.4.5", "guzzlehttp/guzzle": "^7.4",
"guzzlehttp/psr7": "^2.2", "guzzlehttp/psr7": "^2.2",
"mongodb/mongodb": "^1.8", "mongodb/mongodb": "^1.8",
"php-amqplib/php-amqplib": "~2.4 || ^3", "php-amqplib/php-amqplib": "~2.4 || ^3",
"phpstan/phpstan": "^1.9", "phpspec/prophecy": "^1.15",
"phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan": "^1.10",
"phpstan/phpstan-strict-rules": "^1.4", "phpunit/phpunit": "^8.5.38 || ^9.6.19",
"phpunit/phpunit": "^10.5.17", "predis/predis": "^1.1 || ^2.0",
"predis/predis": "^1.1 || ^2", "rollbar/rollbar": "^1.3 || ^2 || ^3",
"ruflin/elastica": "^7", "ruflin/elastica": "^7",
"swiftmailer/swiftmailer": "^5.3|^6.0",
"symfony/mailer": "^5.4 || ^6", "symfony/mailer": "^5.4 || ^6",
"symfony/mime": "^5.4 || ^6" "symfony/mime": "^5.4 || ^6"
}, },
@ -65,7 +72,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-main": "3.x-dev" "dev-main": "2.x-dev"
} }
}, },
"autoload": { "autoload": {
@ -93,7 +100,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/Seldaek/monolog/issues", "issues": "https://github.com/Seldaek/monolog/issues",
"source": "https://github.com/Seldaek/monolog/tree/3.7.0" "source": "https://github.com/Seldaek/monolog/tree/2.10.0"
}, },
"funding": [ "funding": [
{ {
@ -105,7 +112,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-06-28T09:40:51+00:00" "time": "2024-11-12T12:43:37+00:00"
}, },
{ {
"name": "nikic/fast-route", "name": "nikic/fast-route",
@ -119,7 +126,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nikic/FastRoute/zipball/181d480e08d9476e61381e04a71b34dc0432e812", "url": "https://api.github.com/repos/nikic/FastRoute/zipball/181d480e08d9476e61381e04a71b34dc0432e812",
"reference": "181d480e08d9476e61381e04a71b34dc0432e812", "reference": "181d480e08d9476e61381e04a71b34dc0432e812",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"php": ">=5.4.0" "php": ">=5.4.0"
@ -169,7 +182,13 @@
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963", "url": "https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963", "reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"php": ">=7.4.0" "php": ">=7.4.0"
@ -212,17 +231,23 @@
}, },
{ {
"name": "psr/log", "name": "psr/log",
"version": "3.0.0", "version": "3.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/php-fig/log.git", "url": "https://github.com/php-fig/log.git",
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" "reference": "79dff0b268932c640297f5208d6298f71855c03e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", "url": "https://api.github.com/repos/php-fig/log/zipball/79dff0b268932c640297f5208d6298f71855c03e",
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", "reference": "79dff0b268932c640297f5208d6298f71855c03e",
"shasum": "" "shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
}, },
"require": { "require": {
"php": ">=8.0.0" "php": ">=8.0.0"
@ -256,30 +281,25 @@
"psr-3" "psr-3"
], ],
"support": { "support": {
"source": "https://github.com/php-fig/log/tree/3.0.0" "source": "https://github.com/php-fig/log/tree/3.0.1"
}, },
"time": "2021-07-14T16:46:02+00:00" "time": "2024-08-21T13:31:24+00:00"
}, },
{ {
"name": "workerman/webman-framework", "name": "workerman/webman-framework",
"version": "v1.5.20", "version": "v1.6.6",
"source": {
"type": "git",
"url": "https://github.com/walkor/webman-framework.git",
"reference": "6fcbf1b2a2eb448a09d1cbe44fdc4deb1516f7f8"
},
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/6fcbf1b2a2eb448a09d1cbe44fdc4deb1516f7f8", "url": "https://mirrors.cloud.tencent.com/repository/composer/workerman/webman-framework/v1.6.6/workerman-webman-framework-v1.6.6.zip",
"reference": "6fcbf1b2a2eb448a09d1cbe44fdc4deb1516f7f8", "reference": "9a89995796acff015d303f3a7eda62e0a6a5a361",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-json": "*", "ext-json": "*",
"nikic/fast-route": "^1.3", "nikic/fast-route": "^1.3",
"php": ">=7.2", "php": ">=8.0",
"psr/container": ">=1.0", "psr/container": ">=1.0",
"workerman/workerman": "^4.0.4 || ^5.0.0" "workerman/workerman": "^4.2.1 || ^5.0.0 || dev-master"
}, },
"suggest": { "suggest": {
"ext-event": "For better performance. " "ext-event": "For better performance. "
@ -295,7 +315,6 @@
"Support\\Exception\\": "./src/support/exception" "Support\\Exception\\": "./src/support/exception"
} }
}, },
"notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"MIT" "MIT"
], ],
@ -313,31 +332,19 @@
"High Performance", "High Performance",
"http service" "http service"
], ],
"support": { "time": "2024-11-25T06:39:00+00:00"
"email": "walkor@workerman.net",
"forum": "https://wenda.workerman.net/",
"issues": "https://github.com/walkor/webman/issues",
"source": "https://github.com/walkor/webman-framework",
"wiki": "https://doc.workerman.net/"
},
"time": "2024-07-04T07:12:58+00:00"
}, },
{ {
"name": "workerman/workerman", "name": "workerman/workerman",
"version": "v4.1.16", "version": "v4.2.1",
"source": {
"type": "git",
"url": "https://github.com/walkor/workerman.git",
"reference": "405d904d33026e19497dffc3d085bbc16e66534e"
},
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/walkor/workerman/zipball/405d904d33026e19497dffc3d085bbc16e66534e", "url": "https://mirrors.cloud.tencent.com/repository/composer/workerman/workerman/v4.2.1/workerman-workerman-v4.2.1.zip",
"reference": "405d904d33026e19497dffc3d085bbc16e66534e", "reference": "cafb5a43d93d7d30a16b32a57948581cca993562",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=7.0" "php": ">=8.0"
}, },
"suggest": { "suggest": {
"ext-event": "For better performance. " "ext-event": "For better performance. "
@ -348,7 +355,6 @@
"Workerman\\": "./" "Workerman\\": "./"
} }
}, },
"notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"MIT" "MIT"
], ],
@ -366,35 +372,18 @@
"asynchronous", "asynchronous",
"event-loop" "event-loop"
], ],
"support": { "time": "2024-11-24T11:45:37+00:00"
"email": "walkor@workerman.net",
"forum": "http://wenda.workerman.net/",
"issues": "https://github.com/walkor/workerman/issues",
"source": "https://github.com/walkor/workerman",
"wiki": "http://doc.workerman.net/"
},
"funding": [
{
"url": "https://opencollective.com/workerman",
"type": "open_collective"
},
{
"url": "https://www.patreon.com/walkor",
"type": "patreon"
}
],
"time": "2024-07-04T08:26:39+00:00"
} }
], ],
"packages-dev": [], "packages-dev": [],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "dev",
"stability-flags": [], "stability-flags": {},
"prefer-stable": false, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=7.2" "php": ">=7.2"
}, },
"platform-dev": [], "platform-dev": {},
"plugin-api-version": "2.6.0" "plugin-api-version": "2.6.0"
} }

30
config/cache.php Normal file
View File

@ -0,0 +1,30 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
'default' => 'file',
'stores' => [
'file' => [
'driver' => 'file',
'path' => runtime_path('cache')
],
'redis' => [
'driver' => 'redis',
'connection' => 'default'
],
'array' => [
'driver' => 'array'
]
]
];

View File

@ -20,7 +20,7 @@ return [
'constructor' => [ 'constructor' => [
runtime_path() . '/logs/webman.log', runtime_path() . '/logs/webman.log',
7, //$maxFiles 7, //$maxFiles
Monolog\Level::Debug, Monolog\Logger::DEBUG,
], ],
'formatter' => [ 'formatter' => [
'class' => Monolog\Formatter\LineFormatter::class, 'class' => Monolog\Formatter\LineFormatter::class,

View File

@ -12,12 +12,32 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
*/ */
use support\Log;
use support\Request;
use app\process\Http;
global $argv; global $argv;
return [ return [
'webman' => [
'handler' => Http::class,
'listen' => 'http://0.0.0.0:8787',
'count' => cpu_count() * 4,
'user' => '',
'group' => '',
'reusePort' => false,
'eventLoop' => '',
'context' => [],
'constructor' => [
'requestClass' => Request::class,
'logger' => Log::channel('default'),
'appPath' => app_path(),
'publicPath' => public_path()
]
],
// File update detection and automatic reload // File update detection and automatic reload
'monitor' => [ 'monitor' => [
'handler' => process\Monitor::class, 'handler' => app\process\Monitor::class,
'reloadable' => false, 'reloadable' => false,
'constructor' => [ 'constructor' => [
// Monitor these directories // Monitor these directories

View File

@ -13,14 +13,6 @@
*/ */
return [ return [
'listen' => 'http://0.0.0.0:8787',
'transport' => 'tcp',
'context' => [],
'name' => 'webman',
'count' => cpu_count() * 4,
'user' => '',
'group' => '',
'reusePort' => false,
'event_loop' => '', 'event_loop' => '',
'stop_timeout' => 2, 'stop_timeout' => 2,
'pid_file' => runtime_path() . '/webman.pid', 'pid_file' => runtime_path() . '/webman.pid',

4
php
View File

@ -9,11 +9,11 @@ cd "$SCRIPT_DIR" || exit 1
# 使用heredoc构建docker run命令 # 使用heredoc构建docker run命令
read -r -d '' DOCKER_COMMAND <<'EOF' read -r -d '' DOCKER_COMMAND <<'EOF'
docker run --rm \ docker run --rm \
--network host \ -p 8787:8787 \
-v "$(pwd)":/app \ -v "$(pwd)":/app \
-v "$(pwd)/php.ini":/usr/local/etc/php/php.ini \ -v "$(pwd)/php.ini":/usr/local/etc/php/php.ini \
-w /app \ -w /app \
quay.io/wandoubaba517/php:8.2-workerman \ quay.io/wandoubaba517/php:8.3-workerman \
php php
EOF EOF

View File

@ -19,9 +19,15 @@ use Webman\Config;
use Webman\Middleware; use Webman\Middleware;
use Webman\Route; use Webman\Route;
use Webman\Util; use Webman\Util;
use Workerman\Events\Select;
use Workerman\Worker;
$worker = $worker ?? null; $worker = $worker ?? null;
if (empty(Worker::$eventLoopClass)) {
Worker::$eventLoopClass = Select::class;
}
set_error_handler(function ($level, $message, $file = '', $line = 0) { set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) { if (error_reporting() & $level) {
throw new ErrorException($message, 0, $level, $file, $line); throw new ErrorException($message, 0, $level, $file, $line);

View File

@ -73,15 +73,25 @@ function app_path(string $path = ''): string
/** /**
* Public path * Public path
* @param string $path * @param string $path
* @param string|null $plugin
* @return string * @return string
*/ */
function public_path(string $path = ''): string function public_path(string $path = '', ?string $plugin = null): string
{ {
static $publicPath = ''; static $publicPaths = [];
if (!$publicPath) { $plugin = $plugin ?? '';
$publicPath = \config('app.public_path') ?: run_path('public'); if (isset($publicPaths[$plugin])) {
$publicPath = $publicPaths[$plugin];
} else {
$prefix = $plugin ? "plugin.$plugin." : '';
$pathPrefix = $plugin ? 'plugin' . DIRECTORY_SEPARATOR . $plugin . DIRECTORY_SEPARATOR : '';
$publicPath = \config("{$prefix}app.public_path", run_path("{$pathPrefix}public"));
if (count($publicPaths) > 32) {
$publicPaths = [];
}
$publicPaths[$plugin] = $publicPath;
} }
return path_combine($publicPath, $path); return $path === '' ? $publicPath : path_combine($publicPath, $path);
} }
/** /**
@ -187,70 +197,70 @@ function redirect(string $location, int $status = 302, array $headers = []): Res
/** /**
* View response * View response
* @param string $template * @param mixed $template
* @param array $vars * @param array $vars
* @param string|null $app * @param string|null $app
* @param string|null $plugin * @param string|null $plugin
* @return Response * @return Response
*/ */
function view(string $template, array $vars = [], string $app = null, string $plugin = null): Response function view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{ {
$request = \request(); [$template, $vars, $app, $plugin] = template_inputs($template, $vars, $app, $plugin);
$plugin = $plugin === null ? ($request->plugin ?? '') : $plugin;
$handler = \config($plugin ? "plugin.$plugin.view.handler" : 'view.handler'); $handler = \config($plugin ? "plugin.$plugin.view.handler" : 'view.handler');
return new Response(200, [], $handler::render($template, $vars, $app, $plugin)); return new Response(200, [], $handler::render($template, $vars, $app, $plugin));
} }
/** /**
* Raw view response * Raw view response
* @param string $template * @param mixed $template
* @param array $vars * @param array $vars
* @param string|null $app * @param string|null $app
* @param string|null $plugin
* @return Response * @return Response
* @throws Throwable * @throws Throwable
*/ */
function raw_view(string $template, array $vars = [], string $app = null): Response function raw_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{ {
return new Response(200, [], Raw::render($template, $vars, $app)); return new Response(200, [], Raw::render(...template_inputs($template, $vars, $app, $plugin)));
} }
/** /**
* Blade view response * Blade view response
* @param string $template * @param mixed $template
* @param array $vars * @param array $vars
* @param string|null $app * @param string|null $app
* @param string|null $plugin
* @return Response * @return Response
*/ */
function blade_view(string $template, array $vars = [], string $app = null): Response function blade_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{ {
return new Response(200, [], Blade::render($template, $vars, $app)); return new Response(200, [], Blade::render(...template_inputs($template, $vars, $app, $plugin)));
} }
/** /**
* Think view response * Think view response
* @param string $template * @param mixed $template
* @param array $vars * @param array $vars
* @param string|null $app * @param string|null $app
* @param string|null $plugin
* @return Response * @return Response
*/ */
function think_view(string $template, array $vars = [], string $app = null): Response function think_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{ {
return new Response(200, [], ThinkPHP::render($template, $vars, $app)); return new Response(200, [], ThinkPHP::render(...template_inputs($template, $vars, $app, $plugin)));
} }
/** /**
* Twig view response * Twig view response
* @param string $template * @param mixed $template
* @param array $vars * @param array $vars
* @param string|null $app * @param string|null $app
* @param string|null $plugin
* @return Response * @return Response
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/ */
function twig_view(string $template, array $vars = [], string $app = null): Response function twig_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{ {
return new Response(200, [], Twig::render($template, $vars, $app)); return new Response(200, [], Twig::render(...template_inputs($template, $vars, $app, $plugin)));
} }
/** /**
@ -265,10 +275,10 @@ function request()
/** /**
* Get config * Get config
* @param string|null $key * @param string|null $key
* @param $default * @param mixed $default
* @return array|mixed|null * @return mixed
*/ */
function config(string $key = null, $default = null) function config(?string $key = null, mixed $default = null)
{ {
return Config::get($key, $default); return Config::get($key, $default);
} }
@ -299,11 +309,12 @@ function route(string $name, ...$parameters): string
/** /**
* Session * Session
* @param mixed $key * @param array|string|null $key
* @param mixed $default * @param mixed $default
* @return mixed|bool|Session * @return mixed|bool|Session
* @throws Exception
*/ */
function session($key = null, $default = null) function session(array|string|null $key = null, mixed $default = null): mixed
{ {
$session = \request()->session(); $session = \request()->session();
if (null === $key) { if (null === $key) {
@ -335,7 +346,7 @@ function session($key = null, $default = null)
* @param string|null $locale * @param string|null $locale
* @return string * @return string
*/ */
function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
{ {
$res = Translation::trans($id, $parameters, $domain, $locale); $res = Translation::trans($id, $parameters, $domain, $locale);
return $res === '' ? $id : $res; return $res === '' ? $id : $res;
@ -346,7 +357,7 @@ function trans(string $id, array $parameters = [], string $domain = null, string
* @param string|null $locale * @param string|null $locale
* @return string * @return string
*/ */
function locale(string $locale = null): string function locale(?string $locale = null): string
{ {
if (!$locale) { if (!$locale) {
return Translation::getLocale(); return Translation::getLocale();
@ -441,8 +452,13 @@ function worker_bind($worker, $class)
*/ */
function worker_start($processName, $config) function worker_start($processName, $config)
{ {
$worker = new Worker($config['listen'] ?? null, $config['context'] ?? []); if (isset($config['enable']) && !$config['enable']) {
$propertyMap = [ return;
}
// featcustom worker class [default: Workerman\Worker]
$class = is_a($class = $config['workerClass'] ?? '' , Worker::class, true) ? $class : Worker::class;
$worker = new $class($config['listen'] ?? null, $config['context'] ?? []);
$properties = [
'count', 'count',
'user', 'user',
'group', 'group',
@ -450,9 +466,10 @@ function worker_start($processName, $config)
'reusePort', 'reusePort',
'transport', 'transport',
'protocol', 'protocol',
'eventLoop',
]; ];
$worker->name = $processName; $worker->name = $processName;
foreach ($propertyMap as $property) { foreach ($properties as $property) {
if (isset($config[$property])) { if (isset($config[$property])) {
$worker->$property = $config[$property]; $worker->$property = $config[$property];
} }
@ -495,6 +512,32 @@ function is_phar(): bool
return class_exists(Phar::class, false) && Phar::running(); return class_exists(Phar::class, false) && Phar::running();
} }
/**
* Get template vars
* @param mixed $template
* @param array $vars
* @param string|null $app
* @param string|null $plugin
* @return array
*/
function template_inputs($template, array $vars, ?string $app, ?string $plugin): array
{
$request = \request();
$plugin = $plugin === null ? ($request->plugin ?? '') : $plugin;
if (is_array($template)) {
$vars = $template;
$template = null;
}
if ($template === null && $controller = $request->controller) {
$controllerSuffix = config($plugin ? "plugin.$plugin.app.controller_suffix" : "app.controller_suffix", '');
$controllerName = $controllerSuffix !== '' ? substr($controller, 0, -strlen($controllerSuffix)) : $controller;
$path = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', substr(strrchr($controllerName, '\\'), 1)));
$actionFileBaseName = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $request->action));
$template = "$path/$actionFileBaseName";
}
return [$template, $vars, $app, $plugin];
}
/** /**
* Get cpu count * Get cpu count
* @return int * @return int
@ -510,19 +553,24 @@ function cpu_count(): int
if (strtolower(PHP_OS) === 'darwin') { if (strtolower(PHP_OS) === 'darwin') {
$count = (int)shell_exec('sysctl -n machdep.cpu.core_count'); $count = (int)shell_exec('sysctl -n machdep.cpu.core_count');
} else { } else {
$count = (int)shell_exec('nproc'); try {
$count = (int)shell_exec('nproc');
} catch (\Throwable $ex) {
// Do nothing
}
} }
} }
return $count > 0 ? $count : 4; return $count > 0 ? $count : 4;
} }
/** /**
* Get request parameters, if no parameter name is passed, an array of all values is returned, default values is supported * Get request parameters, if no parameter name is passed, an array of all values is returned, default values is supported
* @param string|null $param param's name * @param string|null $param param's name
* @param mixed|null $default default value * @param mixed $default default value
* @return mixed|null * @return mixed
*/ */
function input(string $param = null, $default = null) function input(?string $param = null, mixed $default = null): mixed
{ {
return is_null($param) ? request()->all() : request()->input($param, $default); return is_null($param) ? request()->all() : request()->input($param, $default);
} }

View File

@ -5,7 +5,6 @@
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
use Dotenv\Dotenv; use Dotenv\Dotenv;
use process\Monitor;
use support\App; use support\App;
use Workerman\Worker; use Workerman\Worker;
@ -28,12 +27,21 @@ if (isset($errorReporting)) {
} }
$runtimeProcessPath = runtime_path() . DIRECTORY_SEPARATOR . '/windows'; $runtimeProcessPath = runtime_path() . DIRECTORY_SEPARATOR . '/windows';
if (!is_dir($runtimeProcessPath)) { $paths = [
mkdir($runtimeProcessPath); $runtimeProcessPath,
} runtime_path('logs'),
$processFiles = [ runtime_path('views')
__DIR__ . DIRECTORY_SEPARATOR . 'start.php'
]; ];
foreach ($paths as $path) {
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
}
$processFiles = [];
if (config('server.listen')) {
$processFiles[] = __DIR__ . DIRECTORY_SEPARATOR . 'start.php';
}
foreach (config('process', []) as $processName => $config) { foreach (config('process', []) as $processName => $config) {
$processFiles[] = write_process_file($runtimeProcessPath, $processName, ''); $processFiles[] = write_process_file($runtimeProcessPath, $processName, '');
} }
@ -72,6 +80,14 @@ if (is_callable('opcache_reset')) {
opcache_reset(); opcache_reset();
} }
if (!\$appConfigFile = config_path('app.php')) {
throw new RuntimeException('Config file not found: app.php');
}
\$appConfig = require \$appConfigFile;
if (\$timezone = \$appConfig['default_timezone'] ?? '') {
date_default_timezone_set(\$timezone);
}
App::loadAllConfig(['route']); App::loadAllConfig(['route']);
worker_start('$processParam', $configParam); worker_start('$processParam', $configParam);
@ -90,7 +106,8 @@ EOF;
} }
if ($monitorConfig = config('process.monitor.constructor')) { if ($monitorConfig = config('process.monitor.constructor')) {
$monitor = new Monitor(...array_values($monitorConfig)); $monitorHandler = config('process.monitor.handler');
$monitor = new $monitorHandler(...array_values($monitorConfig));
} }
function popen_processes($processFiles) function popen_processes($processFiles)