From 4b509cc72ac0b4cdf32e829a7c3630b2950032e8 Mon Sep 17 00:00:00 2001 From: chenqiang Date: Wed, 16 Apr 2025 17:05:35 +0800 Subject: [PATCH] =?UTF-8?q?webman=E5=8D=87=E7=BA=A7=E5=88=B02.1=EF=BC=8Cwo?= =?UTF-8?q?rkerman=E5=8D=87=E7=BA=A7=E5=BC=805.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 161 ++++------- app/process/Monitor.php | 22 +- composer.json | 12 +- composer.lock | 170 ++++++++---- config/bootstrap.php | 1 - config/cache.php | 30 --- config/database.php | 15 -- config/redis.php | 22 -- public/404.html | 12 - start.php | 1 + support/helpers.php | 576 ---------------------------------------- windows.php | 1 + 12 files changed, 193 insertions(+), 830 deletions(-) delete mode 100644 config/cache.php delete mode 100644 config/database.php delete mode 100644 config/redis.php delete mode 100644 public/404.html delete mode 100644 support/helpers.php diff --git a/README.md b/README.md index 147ffbf..4031784 100644 --- a/README.md +++ b/README.md @@ -1,121 +1,70 @@ -# 基于docker的webman +
+

webman

-我们在php:8.1.27-cli基础上安装了运行webman必要的一些扩展和开发中常用的扩展,以及composer,制作成workerman:8.1.27镜像,因此在开发环境中不需要再额外配置php环境,直接写业务代码即可。 +基于workerman开发的超高性能PHP框架 -> 把镜像的tag换成8.2.20即可把php版本换成8.2.20 -## 依赖 +

学习

-只要系统环境中有docker即可(生产环境需要docker-compose),不需要安装php环境。 + -## 使用方法 +
-```sh -# composer -./composer require xxxx -# 启动服务 -./php start.php start -# 执行其它php命令 -./php -m -./php webman build:bin -``` +

赞助商

-## 生产环境使用 +

特别赞助

+ + + -创建一个docker-compose.yml文件: +

铂金赞助

+ + -```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插件) +
-```sh -docker compose up -d -``` -## 一些有用的文件 -|文件|作用|用法或说明| -|---|---|---| -|composer|调起容器中的composer命令的shell脚本|用法与主机中的compose完全一致,./composer $args...| -|php|调起容器中的php命令的shell脚本|用法与主机中的php一致,但注意执行./php start.php start命令时不要加-d参数| -|php.ini|映射到容器中的php.ini配置文件|容器中已经安装的扩展都有单独的配置文件,这里不会有体现| +
-## 已安装PHP扩展 +

请作者喝咖啡

+ + + +
+如果您觉得webman对您有所帮助,欢迎捐赠。 + + +
+ + +
+

LICENSE

+The webman is open-sourced software licensed under the MIT. +
+ +
-```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 -``` diff --git a/app/process/Monitor.php b/app/process/Monitor.php index 4fab730..399ce62 100644 --- a/app/process/Monitor.php +++ b/app/process/Monitor.php @@ -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'); } /** diff --git a/composer.json b/composer.json index f6617e2..d25c083 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/composer.lock b/composer.lock index 5f642e0..8265136 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "=7.2" + "php": ">=8.1" }, "platform-dev": {}, "plugin-api-version": "2.6.0" diff --git a/config/bootstrap.php b/config/bootstrap.php index 44054e0..95d2e87 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -14,5 +14,4 @@ return [ support\bootstrap\Session::class, - support\bootstrap\LaravelDb::class, ]; diff --git a/config/cache.php b/config/cache.php deleted file mode 100644 index 13b94e6..0000000 --- a/config/cache.php +++ /dev/null @@ -1,30 +0,0 @@ - - * @copyright walkor - * @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' - ] - ] -]; diff --git a/config/database.php b/config/database.php deleted file mode 100644 index 7dc463a..0000000 --- a/config/database.php +++ /dev/null @@ -1,15 +0,0 @@ - - * @copyright walkor - * @link http://www.workerman.net/ - * @license http://www.opensource.org/licenses/mit-license.php MIT License - */ - -return []; diff --git a/config/redis.php b/config/redis.php deleted file mode 100644 index 2f9757a..0000000 --- a/config/redis.php +++ /dev/null @@ -1,22 +0,0 @@ - - * @copyright walkor - * @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, - ], -]; diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 2bde119..0000000 --- a/public/404.html +++ /dev/null @@ -1,12 +0,0 @@ - - - 404 Not Found - webman - - -
-

404 Not Found

-
-
-
webman
- - diff --git a/start.php b/start.php index 489e447..41ad7ef 100755 --- a/start.php +++ b/start.php @@ -1,4 +1,5 @@ #!/usr/bin/env php - * @copyright walkor - * @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; - } - // feat:custom 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('/(?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); -} diff --git a/windows.php b/windows.php index 32ff91f..f37a72c 100644 --- a/windows.php +++ b/windows.php @@ -2,6 +2,7 @@ /** * Start file for windows */ +chdir(__DIR__); require_once __DIR__ . '/vendor/autoload.php'; use Dotenv\Dotenv;