Option.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. use Hyperf\HttpServer\PriorityMiddleware;
  13. use function Hyperf\Tappable\tap;
  14. class Option
  15. {
  16. /**
  17. * Send Channel Capacity, Only support multiplex server mode.
  18. */
  19. protected int $sendChannelCapacity = 0;
  20. /**
  21. * Whether to enable request lifecycle event.
  22. */
  23. protected bool $enableRequestLifecycle = false;
  24. /**
  25. * Whether to sort middlewares by priority.
  26. */
  27. protected bool $mustSortMiddlewares = false;
  28. public static function make(array|Option $options): Option
  29. {
  30. if ($options instanceof Option) {
  31. return $options;
  32. }
  33. return tap(new self(), function (Option $option) use ($options) {
  34. $option->setSendChannelCapacity($options['send_channel_capacity'] ?? 0);
  35. $option->setEnableRequestLifecycle($options['enable_request_lifecycle'] ?? false);
  36. });
  37. }
  38. public function getSendChannelCapacity(): int
  39. {
  40. return $this->sendChannelCapacity;
  41. }
  42. public function setSendChannelCapacity(int $sendChannelCapacity): static
  43. {
  44. $this->sendChannelCapacity = $sendChannelCapacity;
  45. return $this;
  46. }
  47. public function isEnableRequestLifecycle(): bool
  48. {
  49. return $this->enableRequestLifecycle;
  50. }
  51. public function setEnableRequestLifecycle(bool $enableRequestLifecycle): static
  52. {
  53. $this->enableRequestLifecycle = $enableRequestLifecycle;
  54. return $this;
  55. }
  56. public function isMustSortMiddlewares(): bool
  57. {
  58. return $this->mustSortMiddlewares;
  59. }
  60. public function setMustSortMiddlewares(bool $mustSortMiddlewares): static
  61. {
  62. $this->mustSortMiddlewares = $mustSortMiddlewares;
  63. return $this;
  64. }
  65. public function setMustSortMiddlewaresByMiddlewares(array $middlewares): static
  66. {
  67. foreach ($middlewares as $middleware) {
  68. if (is_int($middleware) || $middleware instanceof PriorityMiddleware) {
  69. return $this->setMustSortMiddlewares(true);
  70. }
  71. }
  72. return $this;
  73. }
  74. }