Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Environment
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
10 / 10
26
100.00% covered (success)
100.00%
1 / 1
 load
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
8
 get
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 getString
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 getInteger
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getBoolean
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appEnv
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appC3
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 appDebug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEnvironment
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace App;
6
7 class Environment
8{
9    public const PROD = 'prod';
10    public const DEV = 'dev';
11    public const TEST = 'test';
12
13    private const ENVIRONMENTS = [
14        self::PROD,
15        self::DEV,
16        self::TEST,
17    ];
18
19    /** @var array<string, mixed> */
20    private static array $values = [];
21
22    public static function load(string $filePath): void
23    {
24        if (!file_exists($filePath)) {
25            return;
26        }
27
28        $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
29        if ($lines === false) {
30            return;
31        }
32
33        foreach ($lines as $line) {
34            if (str_starts_with(trim($line), '#')) {
35                continue;
36            }
37
38            if (str_contains($line, '=')) {
39                [$key, $value] = explode('=', $line, 2);
40                $key = trim($key);
41                $value = trim($value);
42
43                if (preg_match('/^"(.*)"$/', $value, $matches) || preg_match("/^'(.*)'$/", $value, $matches)) {
44                    $value = $matches[1];
45                }
46
47                self::$values[$key] = $value;
48                $_ENV[$key] = $value;
49                $_SERVER[$key] = $value;
50            }
51        }
52    }
53
54    public static function get(string $key, mixed $default = null): mixed
55    {
56        $val = $default;
57        if (array_key_exists($key, self::$values)) {
58            $val = self::$values[$key];
59        } elseif (array_key_exists($key, $_ENV)) {
60            $val = $_ENV[$key];
61        } elseif (array_key_exists($key, $_SERVER)) {
62            $val = $_SERVER[$key];
63        } elseif (($envVal = getenv($key)) !== false) {
64            $val = $envVal;
65        }
66        return $val;
67    }
68
69    public static function getString(string $key, string $default = ''): string
70    {
71        $val = self::get($key, $default);
72        return is_string($val) || is_numeric($val) ? (string) $val : $default;
73    }
74
75    public static function getInteger(string $key, int $default = 0): int
76    {
77        $val = self::get($key, $default);
78        return is_numeric($val) ? (int) $val : $default;
79    }
80
81    public static function getBoolean(string $key, bool $default = false): bool
82    {
83        $val = self::get($key, $default);
84        if (is_string($val)) {
85            return filter_var($val, FILTER_VALIDATE_BOOLEAN);
86        }
87        return (bool) $val;
88    }
89
90    public static function set(string $key, mixed $value): void
91    {
92        self::$values[$key] = $value;
93    }
94
95    public static function appEnv(): string
96    {
97        return self::getString('APP_ENV', self::PROD);
98    }
99
100    public static function appC3(): bool
101    {
102        return self::getBoolean('APP_C3');
103    }
104
105    public static function appDebug(): bool
106    {
107        return self::getBoolean('APP_DEBUG');
108    }
109
110    public static function setEnvironment(): void
111    {
112        $environment = self::getString('APP_ENV', self::PROD);
113
114        if (!in_array($environment, self::ENVIRONMENTS, true)) {
115            throw new \InvalidArgumentException(
116                sprintf('Environment "%s" is not valid. Valid environments are "%s".', $environment, implode('", "', self::ENVIRONMENTS))
117            );
118        }
119
120        self::set('APP_ENV', $environment);
121    }
122}