Repository navigation
Expand file tree
/
Copy pathServerConfig.php
More file actions
109 lines (95 loc) · 3.18 KB
/
Copy pathServerConfig.php
File metadata and controls
109 lines (95 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Server;
use Hyperf\Contract\Arrayable;
use Hyperf\Server\Exception\InvalidArgumentException;
/**
* @method ServerConfig setType(string $type)
* @method ServerConfig setMode(int $mode)
* @method ServerConfig setServers(array $servers)
* @method ServerConfig setProcesses(array $processes)
* @method ServerConfig setSettings(array $settings)
* @method ServerConfig setCallbacks(array $callbacks)
* @method string getType()
* @method int getMode()
* @method Port[] getServers()
* @method array getProcesses()
* @method array getSettings()
* @method array getCallbacks()
*/
class ServerConfig implements Arrayable
{
public function __construct(protected array $config = [])
{
if (empty($config['servers'] ?? [])) {
throw new InvalidArgumentException('Config server.servers not exist.');
}
$servers = [];
foreach ($config['servers'] as $name => $item) {
if (! isset($item['name']) && ! is_numeric($name)) {
$item['name'] = $name;
}
$servers[] = Port::build($item);
}
$this->setType($config['type'] ?? Server::class)
->setMode($config['mode'] ?? 0)
->setServers($servers)
->setProcesses($config['processes'] ?? [])
->setSettings($config['settings'] ?? [])
->setCallbacks($config['callbacks'] ?? []);
}
public function __set($name, $value)
{
$this->set($name, $value);
}
public function __get($name)
{
if (! $this->isAvailableProperty($name)) {
throw new \InvalidArgumentException(sprintf('Invalid property %s', $name));
}
return $this->config[$name] ?? null;
}
public function __call($name, $arguments)
{
$prefix = strtolower(substr($name, 0, 3));
if (in_array($prefix, ['set', 'get'])) {
$propertyName = strtolower(substr($name, 3));
if (! $this->isAvailableProperty($propertyName)) {
throw new \InvalidArgumentException(sprintf('Invalid property %s', $propertyName));
}
return $prefix === 'set' ? $this->set($propertyName, ...$arguments) : $this->__get($propertyName);
}
throw new \InvalidArgumentException(sprintf('Invalid method %s', $name));
}
public function addServer(Port $port): static
{
$this->config['servers'][] = $port;
return $this;
}
public function toArray(): array
{
return $this->config;
}
protected function set($name, $value): self
{
if (! $this->isAvailableProperty($name)) {
throw new \InvalidArgumentException(sprintf('Invalid property %s', $name));
}
$this->config[$name] = $value;
return $this;
}
private function isAvailableProperty(string $name)
{
return in_array($name, [
'type', 'mode', 'servers', 'processes', 'settings', 'callbacks',
]);
}
}