start.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env php
  2. <?php
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. use Workerman\Worker;
  5. use Workerman\Protocols\Http;
  6. use Workerman\Connection\TcpConnection;
  7. use Webman\App;
  8. use Webman\Config;
  9. use Webman\Route;
  10. use Webman\Middleware;
  11. use Dotenv\Dotenv;
  12. use support\Request;
  13. use support\Log;
  14. use support\Container;
  15. ini_set('display_errors', 'on');
  16. error_reporting(E_ALL);
  17. if (class_exists('Dotenv\Dotenv') && file_exists(base_path() . '/.env')) {
  18. if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
  19. Dotenv::createUnsafeImmutable(base_path())->load();
  20. } else {
  21. Dotenv::createMutable(base_path())->load();
  22. }
  23. }
  24. Config::load(config_path(), ['route', 'container']);
  25. $error_reporting = config('app.error_reporting');
  26. if (isset($error_reporting)) {
  27. error_reporting($error_reporting);
  28. }
  29. if ($timezone = config('app.default_timezone')) {
  30. date_default_timezone_set($timezone);
  31. }
  32. $runtime_logs_path = runtime_path() . DIRECTORY_SEPARATOR . 'logs';
  33. if ( !file_exists($runtime_logs_path) || !is_dir($runtime_logs_path) ) {
  34. if (!mkdir($runtime_logs_path,0777,true)) {
  35. throw new \RuntimeException("Failed to create runtime logs directory. Please check the permission.");
  36. }
  37. }
  38. $runtime_views_path = runtime_path() . DIRECTORY_SEPARATOR . 'views';
  39. if ( !file_exists($runtime_views_path) || !is_dir($runtime_views_path) ) {
  40. if (!mkdir($runtime_views_path,0777,true)) {
  41. throw new \RuntimeException("Failed to create runtime views directory. Please check the permission.");
  42. }
  43. }
  44. Worker::$onMasterReload = function () {
  45. if (function_exists('opcache_get_status')) {
  46. if ($status = opcache_get_status()) {
  47. if (isset($status['scripts']) && $scripts = $status['scripts']) {
  48. foreach (array_keys($scripts) as $file) {
  49. opcache_invalidate($file, true);
  50. }
  51. }
  52. }
  53. }
  54. };
  55. $config = config('server');
  56. Worker::$pidFile = $config['pid_file'];
  57. Worker::$stdoutFile = $config['stdout_file'];
  58. Worker::$logFile = $config['log_file'];
  59. Worker::$eventLoopClass = $config['event_loop'] ?? '';
  60. TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10 * 1024 * 1024;
  61. if (property_exists(Worker::class, 'statusFile')) {
  62. Worker::$statusFile = $config['status_file'] ?? '';
  63. }
  64. if (property_exists(Worker::class, 'stopTimeout')) {
  65. Worker::$stopTimeout = $config['stop_timeout'] ?? 2;
  66. }
  67. if ($config['listen']) {
  68. $worker = new Worker($config['listen'], $config['context']);
  69. $property_map = [
  70. 'name',
  71. 'count',
  72. 'user',
  73. 'group',
  74. 'reusePort',
  75. 'transport',
  76. 'protocol'
  77. ];
  78. foreach ($property_map as $property) {
  79. if (isset($config[$property])) {
  80. $worker->$property = $config[$property];
  81. }
  82. }
  83. $worker->onWorkerStart = function ($worker) {
  84. require_once base_path() . '/support/bootstrap.php';
  85. $app = new App($worker, Container::instance(), Log::channel('default'), app_path(), public_path());
  86. Http::requestClass(config('app.request_class', config('server.request_class', Request::class)));
  87. $worker->onMessage = [$app, 'onMessage'];
  88. };
  89. }
  90. // Windows does not support custom processes.
  91. if (\DIRECTORY_SEPARATOR === '/') {
  92. foreach (config('process', []) as $process_name => $config) {
  93. worker_start($process_name, $config);
  94. }
  95. foreach (config('plugin', []) as $firm => $projects) {
  96. foreach ($projects as $name => $project) {
  97. foreach ($project['process'] ?? [] as $process_name => $config) {
  98. worker_start("plugin.$firm.$name.$process_name", $config);
  99. }
  100. }
  101. }
  102. }
  103. Worker::runAll();