-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathEnvironment.php
More file actions
72 lines (60 loc) · 1.57 KB
/
Environment.php
File metadata and controls
72 lines (60 loc) · 1.57 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
<?php
declare(strict_types=1);
namespace Spiral\RoadRunner;
use Spiral\RoadRunner\Environment\Mode;
/**
* @psalm-import-type ModeType from Mode
* @psalm-type EnvironmentVariables = array{
* RR_MODE?: ModeType|string,
* RR_RELAY?: string,
* RR_RPC?: string,
* RR_VERSION?: string,
* }|array<string, string>
* @see Mode
*/
class Environment implements EnvironmentInterface
{
/**
* @param EnvironmentVariables $env
*/
public function __construct(
private array $env = [],
) {}
public static function fromGlobals(): self
{
/** @var array<string, string> $env */
$env = [...$_ENV, ...$_SERVER];
return new self($env);
}
public function getMode(): string
{
return $this->get('RR_MODE');
}
public function getRelayAddress(): string
{
return $this->get('RR_RELAY', 'pipes');
}
public function getRPCAddress(): string
{
return $this->get('RR_RPC', 'tcp://127.0.0.1:6001');
}
public function getVersion(): string
{
return $this->get('RR_VERSION');
}
/**
* @template TDefault of string
*
* @param non-empty-string $name
* @param TDefault $default
* @return string|TDefault
*/
private function get(string $name, string $default = ''): string
{
if (isset($this->env[$name]) || \array_key_exists($name, $this->env)) {
/** @psalm-suppress RedundantCastGivenDocblockType */
return (string) $this->env[$name];
}
return $default;
}
}