ServerConfig.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Contract\Arrayable;
  13. use Hyperf\Server\Exception\InvalidArgumentException;
  14. /**
  15. * @method ServerConfig setType(string $type)
  16. * @method ServerConfig setMode(int $mode)
  17. * @method ServerConfig setServers(array $servers)
  18. * @method ServerConfig setProcesses(array $processes)
  19. * @method ServerConfig setSettings(array $settings)
  20. * @method ServerConfig setCallbacks(array $callbacks)
  21. * @method string getType()
  22. * @method int getMode()
  23. * @method Port[] getServers()
  24. * @method array getProcesses()
  25. * @method array getSettings()
  26. * @method array getCallbacks()
  27. */
  28. class ServerConfig implements Arrayable
  29. {
  30. public function __construct(protected array $config = [])
  31. {
  32. if (empty($config['servers'] ?? [])) {
  33. throw new InvalidArgumentException('Config server.servers not exist.');
  34. }
  35. $servers = [];
  36. foreach ($config['servers'] as $name => $item) {
  37. if (! isset($item['name']) && ! is_numeric($name)) {
  38. $item['name'] = $name;
  39. }
  40. $servers[] = Port::build($item);
  41. }
  42. $this->setType($config['type'] ?? Server::class)
  43. ->setMode($config['mode'] ?? 0)
  44. ->setServers($servers)
  45. ->setProcesses($config['processes'] ?? [])
  46. ->setSettings($config['settings'] ?? [])
  47. ->setCallbacks($config['callbacks'] ?? []);
  48. }
  49. public function __set($name, $value)
  50. {
  51. $this->set($name, $value);
  52. }
  53. public function __get($name)
  54. {
  55. if (! $this->isAvailableProperty($name)) {
  56. throw new \InvalidArgumentException(sprintf('Invalid property %s', $name));
  57. }
  58. return $this->config[$name] ?? null;
  59. }
  60. public function __call($name, $arguments)
  61. {
  62. $prefix = strtolower(substr($name, 0, 3));
  63. if (in_array($prefix, ['set', 'get'])) {
  64. $propertyName = strtolower(substr($name, 3));
  65. if (! $this->isAvailableProperty($propertyName)) {
  66. throw new \InvalidArgumentException(sprintf('Invalid property %s', $propertyName));
  67. }
  68. return $prefix === 'set' ? $this->set($propertyName, ...$arguments) : $this->__get($propertyName);
  69. }
  70. throw new \InvalidArgumentException(sprintf('Invalid method %s', $name));
  71. }
  72. public function addServer(Port $port): static
  73. {
  74. $this->config['servers'][] = $port;
  75. return $this;
  76. }
  77. public function toArray(): array
  78. {
  79. return $this->config;
  80. }
  81. protected function set($name, $value): self
  82. {
  83. if (! $this->isAvailableProperty($name)) {
  84. throw new \InvalidArgumentException(sprintf('Invalid property %s', $name));
  85. }
  86. $this->config[$name] = $value;
  87. return $this;
  88. }
  89. private function isAvailableProperty(string $name)
  90. {
  91. return in_array($name, [
  92. 'type', 'mode', 'servers', 'processes', 'settings', 'callbacks',
  93. ]);
  94. }
  95. }