start.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if ($timezone = config('app.default_timezone')) {
  26. date_default_timezone_set($timezone);
  27. }
  28. $runtime_logs_path = runtime_path() . DIRECTORY_SEPARATOR . 'logs';
  29. if ( !file_exists($runtime_logs_path) || !is_dir($runtime_logs_path) ) {
  30. if (!mkdir($runtime_logs_path,0777,true)) {
  31. throw new \RuntimeException("Failed to create runtime logs directory. Please check the permission.");
  32. }
  33. }
  34. $runtime_views_path = runtime_path() . DIRECTORY_SEPARATOR . 'views';
  35. if ( !file_exists($runtime_views_path) || !is_dir($runtime_views_path) ) {
  36. if (!mkdir($runtime_views_path,0777,true)) {
  37. throw new \RuntimeException("Failed to create runtime views directory. Please check the permission.");
  38. }
  39. }
  40. Worker::$onMasterReload = function () {
  41. if (function_exists('opcache_get_status')) {
  42. if ($status = opcache_get_status()) {
  43. if (isset($status['scripts']) && $scripts = $status['scripts']) {
  44. foreach (array_keys($scripts) as $file) {
  45. opcache_invalidate($file, true);
  46. }
  47. }
  48. }
  49. }
  50. };
  51. $config = config('server');
  52. Worker::$pidFile = $config['pid_file'];
  53. Worker::$stdoutFile = $config['stdout_file'];
  54. Worker::$logFile = $config['log_file'];
  55. Worker::$eventLoopClass = $config['event_loop'] ?? '';
  56. TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10 * 1024 * 1024;
  57. if (property_exists(Worker::class, 'statusFile')) {
  58. Worker::$statusFile = $config['status_file'] ?? '';
  59. }
  60. if ($config['listen']) {
  61. $worker = new Worker($config['listen'], $config['context']);
  62. $property_map = [
  63. 'name',
  64. 'count',
  65. 'user',
  66. 'group',
  67. 'reusePort',
  68. 'transport',
  69. ];
  70. foreach ($property_map as $property) {
  71. if (isset($config[$property])) {
  72. $worker->$property = $config[$property];
  73. }
  74. }
  75. $worker->onWorkerStart = function ($worker) {
  76. require_once base_path() . '/support/bootstrap.php';
  77. $app = new App($worker, Container::instance(), Log::channel('default'), app_path(), public_path());
  78. Http::requestClass(config('app.request_class', config('server.request_class', Request::class)));
  79. $worker->onMessage = [$app, 'onMessage'];
  80. };
  81. }
  82. // Windows does not support custom processes.
  83. if (\DIRECTORY_SEPARATOR === '/') {
  84. foreach (config('process', []) as $process_name => $config) {
  85. worker_start($process_name, $config);
  86. }
  87. foreach (config('plugin', []) as $firm => $projects) {
  88. foreach ($projects as $name => $project) {
  89. foreach ($project['process'] ?? [] as $process_name => $config) {
  90. worker_start("plugin.$firm.$name.$process_name", $config);
  91. }
  92. }
  93. }
  94. }
  95. Worker::runAll();