Application.php 4.4 KB

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