|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of webman. |
| 4 | + * |
| 5 | + * Licensed under The MIT License |
| 6 | + * For full copyright and license information, please see the MIT-LICENSE.txt |
| 7 | + * Redistributions of files must retain the above copyright notice. |
| 8 | + * |
| 9 | + * @author walkor<[email protected]> |
| 10 | + * @copyright walkor<[email protected]> |
| 11 | + * @link http://www.workerman.net/ |
| 12 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
| 13 | + */ |
| 14 | + |
| 15 | +use Dotenv\Dotenv; |
| 16 | +use support\Log; |
| 17 | +use Webman\Bootstrap; |
| 18 | +use Webman\Config; |
| 19 | +use Webman\Middleware; |
| 20 | +use Webman\Route; |
| 21 | +use Webman\Util; |
| 22 | + |
| 23 | +$worker = $worker ?? null; |
| 24 | + |
| 25 | +set_error_handler(function ($level, $message, $file = '', $line = 0) { |
| 26 | + if (error_reporting() & $level) { |
| 27 | + throw new ErrorException($message, 0, $level, $file, $line); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +if ($worker) { |
| 32 | + register_shutdown_function(function ($startTime) { |
| 33 | + if (time() - $startTime <= 0.1) { |
| 34 | + sleep(1); |
| 35 | + } |
| 36 | + }, time()); |
| 37 | +} |
| 38 | + |
| 39 | +if (class_exists('Dotenv\Dotenv') && file_exists(base_path(false) . '/.env')) { |
| 40 | + if (method_exists('Dotenv\Dotenv', 'createUnsafeMutable')) { |
| 41 | + Dotenv::createUnsafeMutable(base_path(false))->load(); |
| 42 | + } else { |
| 43 | + Dotenv::createMutable(base_path(false))->load(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +Config::clear(); |
| 48 | +support\App::loadAllConfig(['route']); |
| 49 | +if ($timezone = config('app.default_timezone')) { |
| 50 | + date_default_timezone_set($timezone); |
| 51 | +} |
| 52 | + |
| 53 | +foreach (config('autoload.files', []) as $file) { |
| 54 | + include_once $file; |
| 55 | +} |
| 56 | +foreach (config('plugin', []) as $firm => $projects) { |
| 57 | + foreach ($projects as $name => $project) { |
| 58 | + if (!is_array($project)) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + foreach ($project['autoload']['files'] ?? [] as $file) { |
| 62 | + include_once $file; |
| 63 | + } |
| 64 | + } |
| 65 | + foreach ($projects['autoload']['files'] ?? [] as $file) { |
| 66 | + include_once $file; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +Middleware::load(config('middleware', [])); |
| 71 | +foreach (config('plugin', []) as $firm => $projects) { |
| 72 | + foreach ($projects as $name => $project) { |
| 73 | + if (!is_array($project) || $name === 'static') { |
| 74 | + continue; |
| 75 | + } |
| 76 | + Middleware::load($project['middleware'] ?? []); |
| 77 | + } |
| 78 | + Middleware::load($projects['middleware'] ?? [], $firm); |
| 79 | + if ($staticMiddlewares = config("plugin.$firm.static.middleware")) { |
| 80 | + Middleware::load(['__static__' => $staticMiddlewares], $firm); |
| 81 | + } |
| 82 | +} |
| 83 | +Middleware::load(['__static__' => config('static.middleware', [])]); |
| 84 | + |
| 85 | +foreach (config('bootstrap', []) as $className) { |
| 86 | + if (!class_exists($className)) { |
| 87 | + $log = "Warning: Class $className setting in config/bootstrap.php not found\r\n"; |
| 88 | + echo $log; |
| 89 | + Log::error($log); |
| 90 | + continue; |
| 91 | + } |
| 92 | + /** @var Bootstrap $className */ |
| 93 | + $className::start($worker); |
| 94 | +} |
| 95 | + |
| 96 | +foreach (config('plugin', []) as $firm => $projects) { |
| 97 | + foreach ($projects as $name => $project) { |
| 98 | + if (!is_array($project)) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + foreach ($project['bootstrap'] ?? [] as $className) { |
| 102 | + if (!class_exists($className)) { |
| 103 | + $log = "Warning: Class $className setting in config/plugin/$firm/$name/bootstrap.php not found\r\n"; |
| 104 | + echo $log; |
| 105 | + Log::error($log); |
| 106 | + continue; |
| 107 | + } |
| 108 | + /** @var Bootstrap $className */ |
| 109 | + $className::start($worker); |
| 110 | + } |
| 111 | + } |
| 112 | + foreach ($projects['bootstrap'] ?? [] as $className) { |
| 113 | + /** @var string $className */ |
| 114 | + if (!class_exists($className)) { |
| 115 | + $log = "Warning: Class $className setting in plugin/$firm/config/bootstrap.php not found\r\n"; |
| 116 | + echo $log; |
| 117 | + Log::error($log); |
| 118 | + continue; |
| 119 | + } |
| 120 | + /** @var Bootstrap $className */ |
| 121 | + $className::start($worker); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +$directory = base_path() . '/plugin'; |
| 126 | +$paths = [config_path()]; |
| 127 | +foreach (Util::scanDir($directory) as $path) { |
| 128 | + if (is_dir($path = "$path/config")) { |
| 129 | + $paths[] = $path; |
| 130 | + } |
| 131 | +} |
| 132 | +Route::load($paths); |
| 133 | + |
0 commit comments