summaryrefslogtreecommitdiff
path: root/tests/src/Environment.php
blob: 9b3060634138f7fca29c496348962a6a40636a47 (plain)
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
<?php

/**
 * High-performance PHP process supervisor and load balancer written in Go.
 *
 * @author Wolfy-J
 */

declare(strict_types=1);

namespace Spiral\RoadRunner;

use Spiral\RoadRunner\Exception\EnvironmentException;

class Environment implements EnvironmentInterface
{
    /** @var array */
    private array $env;

    /**
     * @param array $env
     */
    public function __construct(array $env)
    {
        $this->env = $env;
    }

    /**
     * Returns worker mode assigned to the PHP process.
     *
     * @return string
     * @throws EnvironmentException
     */
    public function getMode(): string
    {
        return $this->getValue('RR_MODE');
    }

    /**
     * Address worker should be connected to (or pipes).
     *
     * @return string
     * @throws EnvironmentException
     */
    public function getRelayAddress(): string
    {
        return $this->getValue('RR_RELAY');
    }

    /**
     * RPC address.
     *
     * @return string
     * @throws EnvironmentException
     */
    public function getRPCAddress(): string
    {
        return $this->getValue('RR_RPC');
    }

    /**
     * @param string $name
     * @return string
     * @throws EnvironmentException
     */
    private function getValue(string $name): string
    {
        if (!isset($this->env[$name])) {
            throw new EnvironmentException(sprintf("Missing environment value `%s`", $name));
        }

        return (string) $this->env[$name];
    }

    /**
     * @return EnvironmentInterface
     */
    public static function fromGlobals(): EnvironmentInterface
    {
        return new static(array_merge($_SERVER, $_ENV));
    }
}