This repository was archived by the owner on Dec 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfiguration.php
114 lines (97 loc) · 3.66 KB
/
Configuration.php
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
110
111
112
113
114
<?php
namespace BenTools\MercurePHP\Configuration;
use Symfony\Component\Console\Input\InputInterface;
use function BenTools\MercurePHP\nullify;
final class Configuration
{
public const ADDR = 'addr';
public const TRANSPORT_URL = 'transport_url';
public const STORAGE_URL = 'storage_url';
public const METRICS_URL = 'metrics_url';
public const CORS_ALLOWED_ORIGINS = 'cors_allowed_origins';
public const PUBLISH_ALLOWED_ORIGINS = 'publish_allowed_origins';
public const JWT_KEY = 'jwt_key';
public const JWT_ALGORITHM = 'jwt_algorithm';
public const PUBLISHER_JWT_KEY = 'publisher_jwt_key';
public const PUBLISHER_JWT_ALGORITHM = 'publisher_jwt_algorithm';
public const SUBSCRIBER_JWT_KEY = 'subscriber_jwt_key';
public const SUBSCRIBER_JWT_ALGORITHM = 'subscriber_jwt_algorithm';
public const ALLOW_ANONYMOUS = 'allow_anonymous';
public const DEFAULT_ADDR = '127.0.0.1:3000';
public const DEFAULT_TRANSPORT_URL = 'php://localhost?size=1000';
public const DEFAULT_JWT_ALGORITHM = 'HS256';
public const DEFAULT_CORS_ALLOWED_ORIGINS = '*';
public const DEFAULT_PUBLISH_ALLOWED_ORIGINS = '*';
private const DEFAULT_CONFIG = [
self::ADDR => self::DEFAULT_ADDR,
self::TRANSPORT_URL => self::DEFAULT_TRANSPORT_URL,
self::STORAGE_URL => null,
self::METRICS_URL => null,
self::CORS_ALLOWED_ORIGINS => self::DEFAULT_CORS_ALLOWED_ORIGINS,
self::PUBLISH_ALLOWED_ORIGINS => self::DEFAULT_PUBLISH_ALLOWED_ORIGINS,
self::JWT_KEY => null,
self::JWT_ALGORITHM => self::DEFAULT_JWT_ALGORITHM,
self::PUBLISHER_JWT_KEY => null,
self::PUBLISHER_JWT_ALGORITHM => null,
self::SUBSCRIBER_JWT_KEY => null,
self::SUBSCRIBER_JWT_ALGORITHM => null,
self::ALLOW_ANONYMOUS => false,
];
private array $config = self::DEFAULT_CONFIG;
public function __construct(array $config = [])
{
foreach ($config as $key => $value) {
$this->set($key, $value);
}
}
private function export(): array
{
$config = \array_map(fn($value) => \is_string($value) && '' === \trim($value) ? null : $value, $this->config);
if (null === $config[self::JWT_KEY] && null === $config[self::PUBLISHER_JWT_KEY]) {
throw new \InvalidArgumentException(
"One of \"jwt_key\" or \"publisher_jwt_key\" configuration parameter must be defined."
);
}
return $config;
}
public function asArray(): array
{
return $this->export();
}
private function set(string $key, $value): void
{
$key = self::normalize($key);
if (!\array_key_exists($key, self::DEFAULT_CONFIG)) {
return;
}
if (null === $value && \is_bool(self::DEFAULT_CONFIG[$key])) {
$value = self::DEFAULT_CONFIG[$key];
}
$this->config[$key] = $value;
}
public function overrideWith(array $values): self
{
$clone = clone $this;
foreach ($values as $key => $value) {
$clone->set($key, $value);
}
return $clone;
}
private static function normalize(string $key): string
{
return \strtolower(\strtr($key, ['-' => '_']));
}
public static function bootstrapFromCLI(InputInterface $input): self
{
return (new self())
->overrideWith($_SERVER)
->overrideWith(self::filterCLIInput($input));
}
private static function filterCLIInput(InputInterface $input): array
{
return \array_filter(
$input->getOptions(),
fn($value) => null !== nullify($value) && false !== $value
);
}
}