Completed
Push — master ( 396557...287168 )
by Alexander
01:58
created

Environment::addMiddle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Class Environment
7
 *
8
 * @package alkemann\h2l
9
 */
10
final class Environment
11
{
12
    const ALL = "ALL";
13
    const LOCAL = "LOCAL";
14
    const DEV = "DEV";
15
    const TEST = "TEST";
16
    const PROD = "PROD";
17
18
    private static $settings = [
19
        Environment::LOCAL => [
20
            'debug' => true,
21
        ],
22
        Environment::DEV => [
23
            'debug' => true,
24
        ],
25
        Environment::TEST => [
26
            'debug' => false,
27
        ],
28
        Environment::PROD => [
29
            'debug' => false,
30
        ]
31
    ];
32
33
    private static $middlewares = [
34
        Environment::LOCAL => [],
35
        Environment::DEV => [],
36
        Environment::TEST => [],
37
        Environment::PROD => []
38
    ];
39
40
    // Defaults to DEV
41
    private static $current_env = Environment::DEV;
42
43
    public static function setEnvironment(string $env): void
44
    {
45
        self::$current_env = $env;
46
    }
47
48
    public static function current(): string
49
    {
50
        return self::$current_env;
51
    }
52
53
    /**
54
     * Add a middleware to the environment, you can then register them to Dispatcher later based on the env.
55
     *
56
     * Middlewares should be closures that matches: `function(Request $request, Chain $chain): ?Response`
57
     *
58
     * @param callable $mw A callable closure that matches the middleware interface
59
     * @param null|string $environment if not specified, configures CURRENT environment only. E::ALL, sets for all envs.
60
     */
61
    public static function addMiddle(callable $mw, ?string $environment = null): void
62
    {
63
        $environment = $environment ?? static::current();
64
        if ($environment === Environment::ALL) {
65
            foreach (array_keys(self::$settings) as $env) {
66
                static::$middlewares[$env][] = $mw;
0 ignored issues
show
Bug introduced by
Since $middlewares is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $middlewares to at least protected.
Loading history...
67
            }
68
        } else {
69
            static::$middlewares[$environment][] = $mw;
70
        }
71
    }
72
73
    /**
74
     * Get all middlewares, mostly to be sent to Dispatch::registerMiddleware
75
     * @return array of callables that match the middleware interface
76
     */
77
    public static function middlewares(): array
78
    {
79
        return static::$middlewares[static::current()];
0 ignored issues
show
Bug introduced by
Since $middlewares is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $middlewares to at least protected.
Loading history...
80
    }
81
82
    /**
83
     * @TODO apply Util::getFromArrayByKey
84
     * @param string $config_name
85
     * @param mixed $default Value to return in named config is not configured
86
     * @return mixed|null returns `null` if config is not set or no default is specified
87
     */
88
    public static function get(string $config_name, $default = null)
89
    {
90
        $environment = static::current();
91
        if (!array_key_exists($environment, self::$settings)
92
            || !array_key_exists($config_name, self::$settings[$environment])
93
        ) {
94
            return $default;
95
        }
96
        return self::$settings[$environment][$config_name];
97
    }
98
99
    public static function grab(?string $environment = null): array
100
    {
101
        if (is_null($environment)) {
102
            $environment = static::current();
103
        }
104
        if ($environment === Environment::ALL) {
105
            return self::$settings;
106
        }
107
        return self::$settings[$environment] ?? [];
108
    }
109
110
    /**
111
     * Sets ONE config value to current, specified or all environments
112
     *
113
     * @param string $config_name
114
     * @param mixed $value
115
     * @param null|string $environment if not specified, configures CURRENT environment only. E::ALL, sets for all envs.
116
     */
117
    public static function put(string $config_name, $value, ?string $environment = null): void
118
    {
119
        if (is_null($environment)) {
120
            $environment = static::current();
121
        }
122
        if ($environment === Environment::ALL) {
123
            foreach (array_keys(self::$settings) as $env) {
124
                static::put($config_name, $value, $env);
125
            }
126
            static::put($config_name, $value, static::current());
127
            return;
128
        }
129
        if (!array_key_exists($environment, self::$settings)) {
130
            self::$settings[$environment] = [];
131
        }
132
        self::$settings[$environment][$config_name] = $value;
133
    }
134
135
    /**
136
     * Array merges `$config` with current value of the specified or current environments existing configs
137
     *
138
     * @param array $configs
139
     * @param null|string $environment
140
     */
141
    public static function add(array $configs, ?string $environment = null): void
142
    {
143
        if (is_null($environment)) {
144
            $environment = static::current();
145
        }
146
147
        if ($environment === Environment::ALL) {
148
            foreach (array_keys(self::$settings) as $env) {
149
                static::add($configs, $env);
150
            }
151
            static::add($configs, Environment::current());
152
            return;
153
        }
154
        foreach ($configs as $key => $value) {
155
            self::$settings[$environment][$key] = $value;
156
        }
157
    }
158
159
    /**
160
     * Completely replaces the set of configurations that the current or specified environment has.
161
     *
162
     * @param array $configs
163
     * @param null|string $environment
164
     */
165
    public static function set(array $configs, ?string $environment = null): void
166
    {
167
        if (is_null($environment)) {
168
            $environment = static::current();
169
        }
170
        if ($environment === Environment::ALL) {
171
            self::$settings = $configs;
172
        } else {
173
            self::$settings[$environment] = $configs;
174
        }
175
    }
176
}
177