bootstrap.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. use Dotenv\Dotenv;
  15. use support\Log;
  16. use Webman\Bootstrap;
  17. use Webman\Config;
  18. use Webman\Route;
  19. use Webman\Middleware;
  20. use Webman\Util;
  21. $worker = $worker ?? null;
  22. set_error_handler(function ($level, $message, $file = '', $line = 0) {
  23. if (error_reporting() & $level) {
  24. throw new ErrorException($message, 0, $level, $file, $line);
  25. }
  26. });
  27. if ($worker) {
  28. register_shutdown_function(function ($start_time) {
  29. if (time() - $start_time <= 1) {
  30. sleep(1);
  31. }
  32. }, time());
  33. }
  34. if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
  35. if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
  36. Dotenv::createUnsafeImmutable(base_path())->load();
  37. } else {
  38. Dotenv::createMutable(base_path())->load();
  39. }
  40. }
  41. Config::clear();
  42. support\App::loadAllConfig(['route']);
  43. if ($timezone = config('app.default_timezone')) {
  44. date_default_timezone_set($timezone);
  45. }
  46. foreach (config('autoload.files', []) as $file) {
  47. include_once $file;
  48. }
  49. foreach (config('plugin', []) as $firm => $projects) {
  50. foreach ($projects as $name => $project) {
  51. if (!is_array($project)) {
  52. continue;
  53. }
  54. foreach ($project['autoload']['files'] ?? [] as $file) {
  55. include_once $file;
  56. }
  57. }
  58. foreach ($projects['autoload']['files'] ?? [] as $file) {
  59. include_once $file;
  60. }
  61. }
  62. Middleware::load(config('middleware', []), '');
  63. foreach (config('plugin', []) as $firm => $projects) {
  64. foreach ($projects as $name => $project) {
  65. if (!is_array($project) || $name === 'static') {
  66. continue;
  67. }
  68. Middleware::load($project['middleware'] ?? [], '');
  69. }
  70. Middleware::load($projects['middleware'] ?? [], $firm);
  71. if ($static_middlewares = config("plugin.$firm.static.middleware")) {
  72. Middleware::load(['__static__' => $static_middlewares], $firm);
  73. }
  74. }
  75. Middleware::load(['__static__' => config('static.middleware', [])], '');
  76. foreach (config('bootstrap', []) as $class_name) {
  77. if (!class_exists($class_name)) {
  78. $log = "Warning: Class $class_name setting in config/bootstrap.php not found\r\n";
  79. echo $log;
  80. Log::error($log);
  81. continue;
  82. }
  83. /** @var Bootstrap $class_name */
  84. $class_name::start($worker);
  85. }
  86. foreach (config('plugin', []) as $firm => $projects) {
  87. foreach ($projects as $name => $project) {
  88. if (!is_array($project)) {
  89. continue;
  90. }
  91. foreach ($project['bootstrap'] ?? [] as $class_name) {
  92. if (!class_exists($class_name)) {
  93. $log = "Warning: Class $class_name setting in config/plugin/$firm/$name/bootstrap.php not found\r\n";
  94. echo $log;
  95. Log::error($log);
  96. continue;
  97. }
  98. /** @var Bootstrap $class_name */
  99. $class_name::start($worker);
  100. }
  101. }
  102. foreach ($projects['bootstrap'] ?? [] as $class_name) {
  103. if (!class_exists($class_name)) {
  104. $log = "Warning: Class $class_name setting in plugin/$firm/config/bootstrap.php not found\r\n";
  105. echo $log;
  106. Log::error($log);
  107. continue;
  108. }
  109. /** @var Bootstrap $class_name */
  110. $class_name::start($worker);
  111. }
  112. }
  113. $directory = base_path() . '/plugin';
  114. $paths = [config_path()];
  115. foreach (Util::scanDir($directory) as $path) {
  116. if (is_dir($path = "$path/config")) {
  117. $paths[] = $path;
  118. }
  119. }
  120. Route::load($paths);