bootstrap.php 4.0 KB

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