bootstrap.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  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\Middleware;
  19. use Webman\Route;
  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 ($startTime) {
  29. if (time() - $startTime <= 0.1) {
  30. sleep(1);
  31. }
  32. }, time());
  33. }
  34. if (class_exists('Dotenv\Dotenv') && file_exists(base_path(false) . '/.env')) {
  35. if (method_exists('Dotenv\Dotenv', 'createUnsafeMutable')) {
  36. Dotenv::createUnsafeMutable(base_path(false))->load();
  37. } else {
  38. Dotenv::createMutable(base_path(false))->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 ($staticMiddlewares = config("plugin.$firm.static.middleware")) {
  72. Middleware::load(['__static__' => $staticMiddlewares], $firm);
  73. }
  74. }
  75. Middleware::load(['__static__' => config('static.middleware', [])]);
  76. foreach (config('bootstrap', []) as $className) {
  77. if (!class_exists($className)) {
  78. $log = "Warning: Class $className setting in config/bootstrap.php not found\r\n";
  79. echo $log;
  80. Log::error($log);
  81. continue;
  82. }
  83. /** @var Bootstrap $className */
  84. $className::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 $className) {
  92. if (!class_exists($className)) {
  93. $log = "Warning: Class $className 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 $className */
  99. $className::start($worker);
  100. }
  101. }
  102. foreach ($projects['bootstrap'] ?? [] as $className) {
  103. /** @var string $className */
  104. if (!class_exists($className)) {
  105. $log = "Warning: Class $className setting in plugin/$firm/config/bootstrap.php not found\r\n";
  106. echo $log;
  107. Log::error($log);
  108. continue;
  109. }
  110. /** @var Bootstrap $className */
  111. $className::start($worker);
  112. }
  113. }
  114. $directory = base_path() . '/plugin';
  115. $paths = [config_path()];
  116. foreach (Util::scanDir($directory) as $path) {
  117. if (is_dir($path = "$path/config")) {
  118. $paths[] = $path;
  119. }
  120. }
  121. Route::load($paths);