webman升级到2.1,workerman升级开5.1

This commit is contained in:
chenqiang 2025-04-16 17:05:35 +08:00
parent 08b9976af6
commit 4b509cc72a
12 changed files with 193 additions and 830 deletions

161
README.md
View File

@ -1,121 +1,70 @@
# 基于docker的webman
<div style="padding:18px;max-width: 1024px;margin:0 auto;background-color:#fff;color:#333">
<h1>webman</h1>
我们在php:8.1.27-cli基础上安装了运行webman必要的一些扩展和开发中常用的扩展以及composer制作成workerman:8.1.27镜像因此在开发环境中不需要再额外配置php环境直接写业务代码即可。
基于<a href="https://www.workerman.net" target="__blank">workerman</a>开发的超高性能PHP框架
> 把镜像的tag换成8.2.20即可把php版本换成8.2.20
## 依赖
<h1>学习</h1>
只要系统环境中有docker即可生产环境需要docker-compose不需要安装php环境。
<ul>
<li>
<a href="https://www.workerman.net/webman" target="__blank">主页 / Home page</a>
</li>
<li>
<a href="https://webman.workerman.net" target="__blank">文档 / Document</a>
</li>
<li>
<a href="https://www.workerman.net/doc/webman/install.html" target="__blank">安装 / Install</a>
</li>
<li>
<a href="https://www.workerman.net/questions" target="__blank">问答 / Questions</a>
</li>
<li>
<a href="https://www.workerman.net/apps" target="__blank">市场 / Apps</a>
</li>
<li>
<a href="https://www.workerman.net/sponsor" target="__blank">赞助 / Sponsors</a>
</li>
<li>
<a href="https://www.workerman.net/doc/webman/thanks.html" target="__blank">致谢 / Thanks</a>
</li>
</ul>
## 使用方法
<div style="float:left;padding-bottom:30px;">
```sh
# composer
./composer require xxxx
# 启动服务
./php start.php start
# 执行其它php命令
./php -m
./php webman build:bin
```
<h1>赞助商</h1>
## 生产环境使用
<h4>特别赞助</h4>
<a href="https://www.crmeb.com/?form=workerman" target="__blank">
<img src="https://www.workerman.net/img/sponsors/6429/20230719111500.svg" width="200">
</a>
创建一个docker-compose.yml文件
<h4>铂金赞助</h4>
<a href="https://www.fadetask.com/?from=workerman" target="__blank"><img src="https://www.workerman.net/img/sponsors/1/20230719084316.png" width="200"></a>
<a href="https://www.yilianyun.net/?from=workerman" target="__blank" style="margin-left:20px;"><img src="https://www.workerman.net/img/sponsors/6218/20230720114049.png" width="200"></a>
```yml
services:
webman:
image: quay.io/wandoubaba517/workerman:8.1.27
container_name: webman
restart: always
volumes:
- ./:/app/service
working_dir: /app/service
stdin_open: true
ports:
- 8787:8787
# 部署生产时启动这一句
command: ['php', 'start.php', 'start']
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8787/"]
interval: 5s
retries: 3
start_period: 5s
timeout: 10s
```
然后直接执行前提要有docker-compose插件
</div>
```sh
docker compose up -d
```
## 一些有用的文件
|文件|作用|用法或说明|
|---|---|---|
|composer|调起容器中的composer命令的shell脚本|用法与主机中的compose完全一致./composer $args...|
|php|调起容器中的php命令的shell脚本|用法与主机中的php一致但注意执行./php start.php start命令时不要加-d参数|
|php.ini|映射到容器中的php.ini配置文件|容器中已经安装的扩展都有单独的配置文件,这里不会有体现|
<div style="float:left;padding-bottom:30px;clear:both">
## 已安装PHP扩展
<h1>请作者喝咖啡</h1>
<img src="https://www.workerman.net/img/wx_donate.png" width="200">
<img src="https://www.workerman.net/img/ali_donate.png" width="200">
<br>
<b>如果您觉得webman对您有所帮助欢迎捐赠。</b>
</div>
<div style="clear: both">
<h1>LICENSE</h1>
The webman is open-sourced software licensed under the MIT.
</div>
</div>
```sh
php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
event
exif
fileinfo
filter
ftp
gd
gmp
hash
iconv
imagick
json
libxml
mbstring
mongodb
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_pgsql
pdo_sqlite
pgsql
Phar
posix
readline
redis
Reflection
session
SimpleXML
sockets
sodium
SPL
sqlite3
standard
tokenizer
xlswriter
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib
zookeeper
[Zend Modules]
Zend OPcache
```

View File

@ -42,18 +42,13 @@ class Monitor
*/
protected $loadedFiles = [];
/**
* @var string
*/
public static $lockFile = __DIR__ . '/../runtime/monitor.lock';
/**
* Pause monitor
* @return void
*/
public static function pause()
{
file_put_contents(static::$lockFile, time());
file_put_contents(static::lockFile(), time());
}
/**
@ -63,8 +58,8 @@ class Monitor
public static function resume(): void
{
clearstatcache();
if (is_file(static::$lockFile)) {
unlink(static::$lockFile);
if (is_file(static::lockFile())) {
unlink(static::lockFile());
}
}
@ -75,7 +70,16 @@ class Monitor
public static function isPaused(): bool
{
clearstatcache();
return file_exists(static::$lockFile);
return file_exists(static::lockFile());
}
/**
* Lock file
* @return string
*/
protected static function lockFile(): string
{
return runtime_path('monitor.lock');
}
/**

View File

@ -24,10 +24,9 @@
"source": "https://github.com/walkor/webman"
},
"require": {
"php": ">=7.2",
"workerman/webman-framework": "^1.6.6",
"monolog/monolog": "^2.0",
"workerman/workerman": "^4.2.1"
"php": ">=8.1",
"workerman/webman-framework": "^2.1",
"monolog/monolog": "^2.0"
},
"suggest": {
"ext-event": "For better performance. "
@ -38,10 +37,7 @@
"app\\": "./app",
"App\\": "./app",
"app\\View\\Components\\": "./app/view/components"
},
"files": [
"./support/helpers.php"
]
}
},
"scripts": {
"post-package-install": [

170
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "afbf9db9e4592c74428dc015992026ea",
"content-hash": "691f538563ac6695008ddc51b7722c80",
"packages": [
{
"name": "monolog/monolog",
@ -18,13 +18,7 @@
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/5cf826f2991858b54d5c3809bee745560a1042a7",
"reference": "5cf826f2991858b54d5c3809bee745560a1042a7",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
"shasum": ""
},
"require": {
"php": ">=7.2",
@ -126,13 +120,7 @@
"type": "zip",
"url": "https://api.github.com/repos/nikic/FastRoute/zipball/181d480e08d9476e61381e04a71b34dc0432e812",
"reference": "181d480e08d9476e61381e04a71b34dc0432e812",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
"shasum": ""
},
"require": {
"php": ">=5.4.0"
@ -182,13 +170,7 @@
"type": "zip",
"url": "https://api.github.com/repos/php-fig/container/zipball/c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"reference": "c71ecc56dfe541dbd90c5360474fbc405f8d5963",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
"shasum": ""
},
"require": {
"php": ">=7.4.0"
@ -231,23 +213,17 @@
},
{
"name": "psr/log",
"version": "3.0.1",
"version": "3.0.2",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "79dff0b268932c640297f5208d6298f71855c03e"
"reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/79dff0b268932c640297f5208d6298f71855c03e",
"reference": "79dff0b268932c640297f5208d6298f71855c03e",
"shasum": "",
"mirrors": [
{
"url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
"preferred": true
}
]
"url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3",
"reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3",
"shasum": ""
},
"require": {
"php": ">=8.0.0"
@ -281,31 +257,79 @@
"psr-3"
],
"support": {
"source": "https://github.com/php-fig/log/tree/3.0.1"
"source": "https://github.com/php-fig/log/tree/3.0.2"
},
"time": "2024-08-21T13:31:24+00:00"
"time": "2024-09-11T13:17:53+00:00"
},
{
"name": "workerman/coroutine",
"version": "v1.1.3",
"source": {
"type": "git",
"url": "https://github.com/workerman-php/coroutine.git",
"reference": "df8fc428967d512a74a8a7d80355c1d40228c9fa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/workerman-php/coroutine/zipball/df8fc428967d512a74a8a7d80355c1d40228c9fa",
"reference": "df8fc428967d512a74a8a7d80355c1d40228c9fa",
"shasum": ""
},
"require": {
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "^11.0",
"psr/log": "*"
},
"type": "library",
"autoload": {
"psr-4": {
"Workerman\\": "src",
"Workerman\\Coroutine\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Workerman coroutine",
"support": {
"issues": "https://github.com/workerman-php/coroutine/issues",
"source": "https://github.com/workerman-php/coroutine/tree/v1.1.3"
},
"time": "2025-02-17T03:34:21+00:00"
},
{
"name": "workerman/webman-framework",
"version": "v1.6.6",
"version": "v2.1.2",
"source": {
"type": "git",
"url": "https://github.com/walkor/webman-framework.git",
"reference": "f803bd867f07bb0929faef060b59a19a44186bfc"
},
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/workerman/webman-framework/v1.6.6/workerman-webman-framework-v1.6.6.zip",
"reference": "9a89995796acff015d303f3a7eda62e0a6a5a361",
"url": "https://api.github.com/repos/walkor/webman-framework/zipball/f803bd867f07bb0929faef060b59a19a44186bfc",
"reference": "f803bd867f07bb0929faef060b59a19a44186bfc",
"shasum": ""
},
"require": {
"ext-json": "*",
"nikic/fast-route": "^1.3",
"php": ">=8.0",
"php": ">=8.1",
"psr/container": ">=1.0",
"workerman/workerman": "^4.2.1 || ^5.0.0 || dev-master"
"psr/log": "^3.0",
"workerman/workerman": "^5.1 || dev-master"
},
"suggest": {
"ext-event": "For better performance. "
},
"type": "library",
"autoload": {
"files": [
"./src/support/helpers.php"
],
"psr-4": {
"Webman\\": "./src",
"Support\\": "./src/support",
@ -315,6 +339,7 @@
"Support\\Exception\\": "./src/support/exception"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
@ -332,19 +357,42 @@
"High Performance",
"http service"
],
"time": "2024-11-25T06:39:00+00:00"
"support": {
"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": "2025-03-10T11:52:22+00:00"
},
{
"name": "workerman/workerman",
"version": "v4.2.1",
"version": "v5.1.1",
"source": {
"type": "git",
"url": "https://github.com/walkor/workerman.git",
"reference": "72cafa19e488896abd2576ea67b3b1af1abf71c0"
},
"dist": {
"type": "zip",
"url": "https://mirrors.cloud.tencent.com/repository/composer/workerman/workerman/v4.2.1/workerman-workerman-v4.2.1.zip",
"reference": "cafb5a43d93d7d30a16b32a57948581cca993562",
"url": "https://api.github.com/repos/walkor/workerman/zipball/72cafa19e488896abd2576ea67b3b1af1abf71c0",
"reference": "72cafa19e488896abd2576ea67b3b1af1abf71c0",
"shasum": ""
},
"require": {
"php": ">=8.0"
"ext-json": "*",
"php": ">=8.1",
"workerman/coroutine": "^1.1 || dev-main"
},
"conflict": {
"ext-swow": "<v1.0.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.0",
"mockery/mockery": "^1.6",
"pestphp/pest": "2.x-dev",
"phpstan/phpstan": "1.11.x-dev"
},
"suggest": {
"ext-event": "For better performance. "
@ -352,9 +400,10 @@
"type": "library",
"autoload": {
"psr-4": {
"Workerman\\": "./"
"Workerman\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
@ -362,17 +411,36 @@
{
"name": "walkor",
"email": "walkor@workerman.net",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"role": "Developer"
}
],
"description": "An asynchronous event driven PHP framework for easily building fast, scalable network applications.",
"homepage": "http://www.workerman.net",
"homepage": "https://www.workerman.net",
"keywords": [
"asynchronous",
"event-loop"
"event-loop",
"framework",
"http"
],
"time": "2024-11-24T11:45:37+00:00"
"support": {
"email": "walkor@workerman.net",
"forum": "https://www.workerman.net/questions",
"issues": "https://github.com/walkor/workerman/issues",
"source": "https://github.com/walkor/workerman",
"wiki": "https://www.workerman.net/doc/workerman/"
},
"funding": [
{
"url": "https://opencollective.com/workerman",
"type": "open_collective"
},
{
"url": "https://www.patreon.com/walkor",
"type": "patreon"
}
],
"time": "2025-04-02T14:01:24+00:00"
}
],
"packages-dev": [],
@ -382,7 +450,7 @@
"prefer-stable": true,
"prefer-lowest": false,
"platform": {
"php": ">=7.2"
"php": ">=8.1"
},
"platform-dev": {},
"plugin-api-version": "2.6.0"

View File

@ -14,5 +14,4 @@
return [
support\bootstrap\Session::class,
support\bootstrap\LaravelDb::class,
];

View File

@ -1,30 +0,0 @@
<?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

@ -1,15 +0,0 @@
<?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 [];

View File

@ -1,22 +0,0 @@
<?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' => [
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'database' => 0,
],
];

View File

@ -1,12 +0,0 @@
<html>
<head>
<title>404 Not Found - webman</title>
</head>
<body>
<center>
<h1>404 Not Found</h1>
</center>
<hr>
<center>webman</center>
</body>
</html>

View File

@ -1,4 +1,5 @@
#!/usr/bin/env php
<?php
chdir(__DIR__);
require_once __DIR__ . '/vendor/autoload.php';
support\App::run();

View File

@ -1,576 +0,0 @@
<?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
*/
use support\Container;
use support\Request;
use support\Response;
use support\Translation;
use support\view\Blade;
use support\view\Raw;
use support\view\ThinkPHP;
use support\view\Twig;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Webman\App;
use Webman\Config;
use Webman\Route;
use Workerman\Protocols\Http\Session;
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
* @param string|null $plugin
* @return string
*/
function public_path(string $path = '', ?string $plugin = null): string
{
static $publicPaths = [];
$plugin = $plugin ?? '';
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 === '' ? $publicPath : 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);
}
/**
* Generate paths based on given information
* @param string $front
* @param string $back
* @return string
*/
function path_combine(string $front, string $back): string
{
return $front . ($back ? (DIRECTORY_SEPARATOR . ltrim($back, DIRECTORY_SEPARATOR)) : $back);
}
/**
* Response
* @param int $status
* @param array $headers
* @param string $body
* @return Response
*/
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, int $options = JSON_UNESCAPED_UNICODE): Response
{
return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
}
/**
* Xml response
* @param $xml
* @return Response
*/
function xml($xml): Response
{
if ($xml instanceof SimpleXMLElement) {
$xml = $xml->asXML();
}
return new Response(200, ['Content-Type' => 'text/xml'], $xml);
}
/**
* Jsonp response
* @param $data
* @param string $callbackName
* @return Response
*/
function jsonp($data, string $callbackName = 'callback'): Response
{
if (!is_scalar($data) && null !== $data) {
$data = json_encode($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 = []): Response
{
$response = new Response($status, ['Location' => $location]);
if (!empty($headers)) {
$response->withHeaders($headers);
}
return $response;
}
/**
* View response
* @param mixed $template
* @param array $vars
* @param string|null $app
* @param string|null $plugin
* @return Response
*/
function view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{
[$template, $vars, $app, $plugin] = template_inputs($template, $vars, $app, $plugin);
$handler = \config($plugin ? "plugin.$plugin.view.handler" : 'view.handler');
return new Response(200, [], $handler::render($template, $vars, $app, $plugin));
}
/**
* Raw view response
* @param mixed $template
* @param array $vars
* @param string|null $app
* @param string|null $plugin
* @return Response
* @throws Throwable
*/
function raw_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{
return new Response(200, [], Raw::render(...template_inputs($template, $vars, $app, $plugin)));
}
/**
* Blade view response
* @param mixed $template
* @param array $vars
* @param string|null $app
* @param string|null $plugin
* @return Response
*/
function blade_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{
return new Response(200, [], Blade::render(...template_inputs($template, $vars, $app, $plugin)));
}
/**
* Think view response
* @param mixed $template
* @param array $vars
* @param string|null $app
* @param string|null $plugin
* @return Response
*/
function think_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{
return new Response(200, [], ThinkPHP::render(...template_inputs($template, $vars, $app, $plugin)));
}
/**
* Twig view response
* @param mixed $template
* @param array $vars
* @param string|null $app
* @param string|null $plugin
* @return Response
*/
function twig_view(mixed $template = null, array $vars = [], ?string $app = null, ?string $plugin = null): Response
{
return new Response(200, [], Twig::render(...template_inputs($template, $vars, $app, $plugin)));
}
/**
* Get request
* @return \Webman\Http\Request|Request|null
*/
function request()
{
return App::request();
}
/**
* Get config
* @param string|null $key
* @param mixed $default
* @return mixed
*/
function config(?string $key = null, mixed $default = null)
{
return Config::get($key, $default);
}
/**
* Create url
* @param string $name
* @param ...$parameters
* @return string
*/
function route(string $name, ...$parameters): string
{
$route = Route::getByName($name);
if (!$route) {
return '';
}
if (!$parameters) {
return $route->url();
}
if (is_array(current($parameters))) {
$parameters = current($parameters);
}
return $route->url($parameters);
}
/**
* Session
* @param array|string|null $key
* @param mixed $default
* @return mixed|bool|Session
* @throws Exception
*/
function session(array|string|null $key = null, mixed $default = null): mixed
{
$session = \request()->session();
if (null === $key) {
return $session;
}
if (is_array($key)) {
$session->put($key);
return null;
}
if (strpos($key, '.')) {
$keyArray = explode('.', $key);
$value = $session->all();
foreach ($keyArray as $index) {
if (!isset($value[$index])) {
return $default;
}
$value = $value[$index];
}
return $value;
}
return $session->get($key, $default);
}
/**
* 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): string
{
$res = Translation::trans($id, $parameters, $domain, $locale);
return $res === '' ? $id : $res;
}
/**
* Locale
* @param string|null $locale
* @return string
*/
function locale(?string $locale = null): string
{
if (!$locale) {
return Translation::getLocale();
}
Translation::setLocale($locale);
return $locale;
}
/**
* 404 not found
* @return Response
*/
function not_found(): Response
{
return new Response(404, [], file_get_contents(public_path() . '/404.html'));
}
/**
* Copy dir
* @param string $source
* @param string $dest
* @param bool $overwrite
* @return void
*/
function copy_dir(string $source, string $dest, bool $overwrite = false)
{
if (is_dir($source)) {
if (!is_dir($dest)) {
mkdir($dest);
}
$files = scandir($source);
foreach ($files as $file) {
if ($file !== "." && $file !== "..") {
copy_dir("$source/$file", "$dest/$file", $overwrite);
}
}
} else if (file_exists($source) && ($overwrite || !file_exists($dest))) {
copy($source, $dest);
}
}
/**
* Remove dir
* @param string $dir
* @return bool
*/
function remove_dir(string $dir): bool
{
if (is_link($dir) || is_file($dir)) {
return unlink($dir);
}
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $file) {
(is_dir("$dir/$file") && !is_link($dir)) ? remove_dir("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
/**
* Bind worker
* @param $worker
* @param $class
*/
function worker_bind($worker, $class)
{
$callbackMap = [
'onConnect',
'onMessage',
'onClose',
'onError',
'onBufferFull',
'onBufferDrain',
'onWorkerStop',
'onWebSocketConnect',
'onWorkerReload'
];
foreach ($callbackMap as $name) {
if (method_exists($class, $name)) {
$worker->$name = [$class, $name];
}
}
if (method_exists($class, 'onWorkerStart')) {
call_user_func([$class, 'onWorkerStart'], $worker);
}
}
/**
* Start worker
* @param $processName
* @param $config
* @return void
*/
function worker_start($processName, $config)
{
if (isset($config['enable']) && !$config['enable']) {
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',
'user',
'group',
'reloadable',
'reusePort',
'transport',
'protocol',
'eventLoop',
];
$worker->name = $processName;
foreach ($properties as $property) {
if (isset($config[$property])) {
$worker->$property = $config[$property];
}
}
$worker->onWorkerStart = function ($worker) use ($config) {
require_once base_path('/support/bootstrap.php');
if (isset($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);
}
};
}
/**
* Get realpath
* @param string $filePath
* @return string
*/
function get_realpath(string $filePath): string
{
if (strpos($filePath, 'phar://') === 0) {
return $filePath;
} else {
return realpath($filePath);
}
}
/**
* Is phar
* @return bool
*/
function is_phar(): bool
{
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
* @return int
*/
function cpu_count(): int
{
// Windows does not support the number of processes setting.
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');
} else {
try {
$count = (int)shell_exec('nproc');
} catch (\Throwable $ex) {
// Do nothing
}
}
}
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
* @param string|null $param param's name
* @param mixed $default default value
* @return mixed
*/
function input(?string $param = null, mixed $default = null): mixed
{
return is_null($param) ? request()->all() : request()->input($param, $default);
}

View File

@ -2,6 +2,7 @@
/**
* Start file for windows
*/
chdir(__DIR__);
require_once __DIR__ . '/vendor/autoload.php';
use Dotenv\Dotenv;