Port.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Server;
  12. class Port
  13. {
  14. protected string $name = 'http';
  15. protected int $type = ServerInterface::SERVER_HTTP;
  16. protected string $host = '0.0.0.0';
  17. protected int $port = 9501;
  18. protected int $sockType = 0;
  19. protected array $callbacks = [];
  20. protected array $settings = [];
  21. protected ?Option $options = null;
  22. public static function build(array $config): static
  23. {
  24. $config = self::filter($config);
  25. $port = new static();
  26. isset($config['name']) && $port->setName($config['name']);
  27. isset($config['type']) && $port->setType($config['type']);
  28. isset($config['host']) && $port->setHost($config['host']);
  29. isset($config['port']) && $port->setPort($config['port']);
  30. isset($config['sock_type']) && $port->setSockType($config['sock_type']);
  31. isset($config['callbacks']) && $port->setCallbacks($config['callbacks']);
  32. isset($config['settings']) && $port->setSettings($config['settings']);
  33. isset($config['options']) && $port->setOptions(Option::make($config['options']));
  34. return $port;
  35. }
  36. public function getName(): string
  37. {
  38. return $this->name;
  39. }
  40. public function setName(string $name): static
  41. {
  42. $this->name = $name;
  43. return $this;
  44. }
  45. public function getType(): int
  46. {
  47. return $this->type;
  48. }
  49. public function setType(int $type): static
  50. {
  51. $this->type = $type;
  52. return $this;
  53. }
  54. public function getHost(): string
  55. {
  56. return $this->host;
  57. }
  58. public function setHost(string $host): static
  59. {
  60. $this->host = $host;
  61. return $this;
  62. }
  63. public function getPort(): int
  64. {
  65. return $this->port;
  66. }
  67. public function setPort(int $port): static
  68. {
  69. $this->port = $port;
  70. return $this;
  71. }
  72. public function getSockType(): int
  73. {
  74. return $this->sockType;
  75. }
  76. public function setSockType(int $sockType): static
  77. {
  78. $this->sockType = $sockType;
  79. return $this;
  80. }
  81. public function getCallbacks(): array
  82. {
  83. return $this->callbacks;
  84. }
  85. public function setCallbacks(array $callbacks): static
  86. {
  87. $this->callbacks = $callbacks;
  88. return $this;
  89. }
  90. public function getSettings(): array
  91. {
  92. return $this->settings;
  93. }
  94. public function setSettings(array $settings): static
  95. {
  96. $this->settings = $settings;
  97. return $this;
  98. }
  99. public function getOptions(): ?Option
  100. {
  101. return $this->options;
  102. }
  103. public function setOptions(Option $options): static
  104. {
  105. $this->options = $options;
  106. return $this;
  107. }
  108. private static function filter(array $config): array
  109. {
  110. if ((int) $config['type'] === ServerInterface::SERVER_BASE) {
  111. $default = [
  112. 'open_http2_protocol' => false,
  113. 'open_http_protocol' => false,
  114. ];
  115. $config['settings'] = array_merge($default, $config['settings'] ?? []);
  116. }
  117. return $config;
  118. }
  119. }