start.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use Workerman\Worker;
  4. use Workerman\Protocols\Http;
  5. use Workerman\Connection\TcpConnection;
  6. use Webman\App;
  7. use Webman\Config;
  8. use Webman\Route;
  9. use Webman\Middleware;
  10. use support\Request;
  11. use support\bootstrap\Log;
  12. use support\bootstrap\Container;
  13. Config::load(config_path(), ['route', 'container']);
  14. $config = config('server');
  15. if ($timezone = config('app.default_timezone')) {
  16. date_default_timezone_set($timezone);
  17. }
  18. Worker::$onMasterReload = function (){
  19. if ($status = opcache_get_status()) {
  20. foreach (array_keys($status['scripts']) as $file) {
  21. opcache_invalidate($file, true);
  22. }
  23. }
  24. };
  25. Worker::$pidFile = $config['pid_file'];
  26. Worker::$stdoutFile = $config['stdout_file'];
  27. TcpConnection::$defaultMaxPackageSize = $config['max_package_size'] ?? 10*1024*1024;
  28. $worker = new Worker($config['listen'], $config['context']);
  29. $property_map = [
  30. 'name',
  31. 'count',
  32. 'user',
  33. 'group',
  34. 'reusePort',
  35. 'transport',
  36. ];
  37. foreach ($property_map as $property) {
  38. if (isset($config[$property])) {
  39. $worker->$property = $config[$property];
  40. }
  41. }
  42. $worker->onWorkerStart = function ($worker) {
  43. Config::reload(config_path(), ['route', 'container']);
  44. foreach (config('bootstrap', []) as $class_name) {
  45. /** @var \Webman\Bootstrap $class_name */
  46. $class_name::start($worker);
  47. }
  48. $app = new App($worker, Container::instance(), Log::channel('default'), app_path(), public_path());
  49. Route::load(config_path() . '/route.php');
  50. Middleware::load(config('middleware', []));
  51. Middleware::load(['__static__' => config('static.middleware', [])]);
  52. Http::requestClass(Request::class);
  53. $worker->onMessage = [$app, 'onMessage'];
  54. };
  55. foreach (config('process', []) as $process_name => $config) {
  56. $worker = new Worker($config['listen'] ?? null, $config['context'] ?? []);
  57. $property_map = [
  58. 'count',
  59. 'user',
  60. 'group',
  61. 'reloadable',
  62. 'reusePort',
  63. 'transport',
  64. 'protocol',
  65. ];
  66. $worker->name = $process_name;
  67. foreach ($property_map as $property) {
  68. if (isset($config[$property])) {
  69. $worker->$property = $config[$property];
  70. }
  71. }
  72. $worker->onWorkerStart = function ($worker) use ($config) {
  73. Config::reload(config_path(), ['route']);
  74. $bootstrap = $config['bootstrap'] ?? config('bootstrap', []);
  75. if (!in_array(support\bootstrap\Log::class, $bootstrap)) {
  76. $bootstrap[] = support\bootstrap\Log::class;
  77. }
  78. foreach ($bootstrap as $class_name) {
  79. /** @var \Webman\Bootstrap $class_name */
  80. $class_name::start($worker);
  81. }
  82. foreach ($config['services'] ?? [] as $server) {
  83. if (!class_exists($server['handler'])) {
  84. echo "process error: class {$config['handler']} not exists\r\n";
  85. continue;
  86. }
  87. $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
  88. if (isset($server['listen'])) {
  89. echo "listen: {$server['listen']}\n";
  90. }
  91. $class = Container::make($server['handler'], $server['constructor'] ?? []);
  92. worker_bind($listen, $class);
  93. $listen->listen();
  94. }
  95. if (isset($config['handler'])) {
  96. if (!class_exists($config['handler'])) {
  97. echo "process error: class {$config['handler']} not exists\r\n";
  98. return;
  99. }
  100. $class = Container::make($config['handler'], $config['constructor'] ?? []);
  101. worker_bind($worker, $class);
  102. }
  103. };
  104. }
  105. Worker::runAll();