bootstrap.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Container;
  16. use Webman\Config;
  17. use Webman\Route;
  18. use Webman\Middleware;
  19. $worker = $worker ?? null;
  20. if ($timezone = config('app.default_timezone')) {
  21. date_default_timezone_set($timezone);
  22. }
  23. set_error_handler(function ($level, $message, $file = '', $line = 0, $context = []) {
  24. if (error_reporting() & $level) {
  25. throw new ErrorException($message, 0, $level, $file, $line);
  26. }
  27. });
  28. if ($worker) {
  29. register_shutdown_function(function ($start_time) {
  30. if (time() - $start_time <= 1) {
  31. sleep(1);
  32. }
  33. }, time());
  34. }
  35. if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
  36. if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
  37. Dotenv::createUnsafeImmutable(base_path())->load();
  38. } else {
  39. Dotenv::createMutable(base_path())->load();
  40. }
  41. }
  42. Config::reload(config_path(), ['route', 'container']);
  43. foreach (config('plugin', []) as $firm => $projects) {
  44. foreach ($projects as $name => $project) {
  45. foreach ($project['autoload']['files'] ?? [] as $file) {
  46. include_once $file;
  47. }
  48. }
  49. }
  50. foreach (config('autoload.files', []) as $file) {
  51. include_once $file;
  52. }
  53. $container = Container::instance();
  54. Route::container($container);
  55. Middleware::container($container);
  56. Middleware::load(config('middleware', []));
  57. foreach (config('plugin', []) as $firm => $projects) {
  58. foreach ($projects as $name => $project) {
  59. Middleware::load($project['middleware'] ?? []);
  60. }
  61. }
  62. Middleware::load(['__static__' => config('static.middleware', [])]);
  63. foreach (config('bootstrap', []) as $class_name) {
  64. /** @var \Webman\Bootstrap $class_name */
  65. $class_name::start($worker);
  66. }
  67. foreach (config('plugin', []) as $firm => $projects) {
  68. foreach ($projects as $name => $project) {
  69. foreach ($project['bootstrap'] ?? [] as $class_name) {
  70. /** @var \Webman\Bootstrap $class_name */
  71. $class_name::start($worker);
  72. }
  73. }
  74. }
  75. Route::load(config_path());