From ff67e0ce665462b3831cb0f20fc3e98986712e58 Mon Sep 17 00:00:00 2001 From: walkor Date: Thu, 18 Aug 2022 14:59:40 +0800 Subject: [PATCH] 1.4.1 --- composer.json | 2 +- start.php | 115 +----------------------------------------- support/bootstrap.php | 76 +++++++++++++++++++++------- support/helpers.php | 4 +- 4 files changed, 63 insertions(+), 134 deletions(-) diff --git a/composer.json b/composer.json index df5097e..29dd7be 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require": { "php": ">=7.2", - "workerman/webman-framework": "^1.4.0", + "workerman/webman-framework": "^1.4.3", "monolog/monolog": "^2.0" }, "suggest": { diff --git a/start.php b/start.php index b5159a5..489e447 100755 --- a/start.php +++ b/start.php @@ -1,117 +1,4 @@ #!/usr/bin/env php load(); - } else { - Dotenv::createMutable(base_path())->load(); - } -} - -Config::load(config_path(), ['route', 'container']); - -$error_reporting = config('app.error_reporting'); -if (isset($error_reporting)) { - error_reporting($error_reporting); -} - -if ($timezone = config('app.default_timezone')) { - date_default_timezone_set($timezone); -} - -$runtime_logs_path = runtime_path() . DIRECTORY_SEPARATOR . 'logs'; -if ( !file_exists($runtime_logs_path) || !is_dir($runtime_logs_path) ) { - if (!mkdir($runtime_logs_path,0777,true)) { - throw new \RuntimeException("Failed to create runtime logs directory. Please check the permission."); - } -} - -$runtime_views_path = runtime_path() . DIRECTORY_SEPARATOR . 'views'; -if ( !file_exists($runtime_views_path) || !is_dir($runtime_views_path) ) { - if (!mkdir($runtime_views_path,0777,true)) { - throw new \RuntimeException("Failed to create runtime views directory. Please check the permission."); - } -} - -Worker::$onMasterReload = function () { - if (function_exists('opcache_get_status')) { - if ($status = opcache_get_status()) { - if (isset($status['scripts']) && $scripts = $status['scripts']) { - foreach (array_keys($scripts) as $file) { - opcache_invalidate($file, true); - } - } - } - } -}; - -$config = config('server'); -Worker::$pidFile = $config['pid_file']; -Worker::$stdoutFile = $config['stdout_file']; -Worker::$logFile = $config['log_file']; -Worker::$eventLoopClass = $config['event_loop'] ?? ''; -TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10 * 1024 * 1024; -if (property_exists(Worker::class, 'statusFile')) { - Worker::$statusFile = $config['status_file'] ?? ''; -} -if (property_exists(Worker::class, 'stopTimeout')) { - Worker::$stopTimeout = $config['stop_timeout'] ?? 2; -} - -if ($config['listen']) { - $worker = new Worker($config['listen'], $config['context']); - $property_map = [ - 'name', - 'count', - 'user', - 'group', - 'reusePort', - 'transport', - 'protocol' - ]; - foreach ($property_map as $property) { - if (isset($config[$property])) { - $worker->$property = $config[$property]; - } - } - - $worker->onWorkerStart = function ($worker) { - require_once base_path() . '/support/bootstrap.php'; - $app = new App($worker, Container::instance(), Log::channel('default'), app_path(), public_path()); - Http::requestClass(config('app.request_class', config('server.request_class', Request::class))); - $worker->onMessage = [$app, 'onMessage']; - }; -} - -// Windows does not support custom processes. -if (\DIRECTORY_SEPARATOR === '/') { - foreach (config('process', []) as $process_name => $config) { - worker_start($process_name, $config); - } - foreach (config('plugin', []) as $firm => $projects) { - foreach ($projects as $name => $project) { - foreach ($project['process'] ?? [] as $process_name => $config) { - worker_start("plugin.$firm.$name.$process_name", $config); - } - } - } -} - -Worker::runAll(); +support\App::run(); diff --git a/support/bootstrap.php b/support/bootstrap.php index 344f543..7831755 100644 --- a/support/bootstrap.php +++ b/support/bootstrap.php @@ -13,10 +13,12 @@ */ use Dotenv\Dotenv; -use support\Container; +use support\Log; +use Webman\Bootstrap; use Webman\Config; use Webman\Route; use Webman\Middleware; +use Webman\Util; $worker = $worker ?? null; @@ -24,7 +26,7 @@ date_default_timezone_set($timezone); } -set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) { +set_error_handler(function ($level, $message, $file = '', $line = 0) { if (error_reporting() & $level) { throw new ErrorException($message, 0, $level, $file, $line); } @@ -46,45 +48,85 @@ } } -Config::reload(config_path(), ['route', 'container']); +support\App::loadAllConfig(['route']); +foreach (config('autoload.files', []) as $file) { + include_once $file; +} foreach (config('plugin', []) as $firm => $projects) { foreach ($projects as $name => $project) { + if (!is_array($project)) { + continue; + } foreach ($project['autoload']['files'] ?? [] as $file) { include_once $file; } } + foreach ($projects['autoload']['files'] ?? [] as $file) { + include_once $file; + } } -foreach (config('autoload.files', []) as $file) { - include_once $file; -} - -$container = Container::instance(); -Route::container($container); -Middleware::container($container); - -Middleware::load(config('middleware', [])); +Middleware::load(config('middleware', []), ''); foreach (config('plugin', []) as $firm => $projects) { foreach ($projects as $name => $project) { - Middleware::load($project['middleware'] ?? []); + if (!is_array($project) || $name === 'static') { + continue; + } + Middleware::load($project['middleware'] ?? [], ''); + } + Middleware::load($projects['middleware'] ?? [], $firm); + if ($static_middlewares = config("plugin.$firm.static.middleware")) { + Middleware::load(['__static__' => $static_middlewares], $firm); } } -Middleware::load(['__static__' => config('static.middleware', [])]); +Middleware::load(['__static__' => config('static.middleware', [])], ''); foreach (config('bootstrap', []) as $class_name) { - /** @var \Webman\Bootstrap $class_name */ + if (!class_exists($class_name)) { + $log = "Warning: Class $class_name setting in config/bootstrap.php not found\r\n"; + echo $log; + Log::error($log); + continue; + } + /** @var Bootstrap $class_name */ $class_name::start($worker); } foreach (config('plugin', []) as $firm => $projects) { foreach ($projects as $name => $project) { + if (!is_array($project)) { + continue; + } foreach ($project['bootstrap'] ?? [] as $class_name) { - /** @var \Webman\Bootstrap $class_name */ + if (!class_exists($class_name)) { + $log = "Warning: Class $class_name 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); } } + 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"; + echo $log; + Log::error($log); + continue; + } + /** @var Bootstrap $class_name */ + $class_name::start($worker); + } } -Route::load(config_path()); +$directory = base_path() . '/plugin'; +$paths = [config_path()]; +foreach (Util::scanDir($directory) as $path) { + if (is_dir($path = "$path/config")) { + $paths[] = $path; + } +} +Route::load($paths); diff --git a/support/helpers.php b/support/helpers.php index 564bcd1..af97b15 100644 --- a/support/helpers.php +++ b/support/helpers.php @@ -210,7 +210,7 @@ function twig_view(string $template, array $vars = [], string $app = null) } /** - * @return Request + * @return \Webman\Http\Request|null */ function request() { @@ -379,7 +379,7 @@ function worker_bind($worker, $class) } } if (\method_exists($class, 'onWorkerStart')) { - [$class, 'onWorkerStart']($worker); + \call_user_func([$class, 'onWorkerStart'], $worker); } }