• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / bootstrap / 4796142811

pending completion
4796142811

push

github

Milan Felix Å ulc
PHP: require PHP 8.0

105 of 109 relevant lines covered (96.33%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.67
/src/ExtraConfigurator.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Bootstrap;
4

5
use Nette\Bootstrap\Configurator;
6
use Nette\InvalidStateException;
7

8
class ExtraConfigurator extends Configurator
9
{
10

11
        public const PARSE_NATURAL = 1;
12
        public const PARSE_LOWERCASE = 2;
13
        public const PARSE_UPPERCASE = 3;
14

15
        /** @var int How to parse the parameters */
16
        protected static int $parseCase = self::PARSE_LOWERCASE;
17

18
        /** @var non-empty-string Sections separator */
19
        protected static string $parseDelimiter = '__';
20

21
        /**
22
         * Parse environment parameters with NETTE{delimiter=__} prefix
23
         *
24
         * @return mixed[]
25
         */
26
        public static function parseEnvironmentParameters(): array
27
        {
28
                // @phpcs:ignore SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
29
                return static::parseParameters($_SERVER, 'NETTE' . self::$parseDelimiter);
1✔
30
        }
31

32
        /**
33
         * Parse given parameters with custom prefix
34
         *
35
         * @param mixed[] $variables
36
         * @return mixed[]
37
         */
38
        public static function parseParameters(array $variables, string $prefix): array
1✔
39
        {
40
                $parameters = [];
1✔
41
                foreach ($variables as $key => $value) {
1✔
42
                        // Ensure value
43
                        $value = getenv($key);
1✔
44
                        if (strpos($key, $prefix) === 0 && $value !== false) {
1✔
45
                                // Parse PREFIX{delimiter=__}{NAME-1}{delimiter=__}{NAME-N}
46
                                $keys = static::parseParameter(substr($key, strlen($prefix)));
1✔
47
                                // Make array structure
48
                                $parameters = static::deepParameters($parameters, $keys, $value);
1✔
49
                        }
50
                }
51

52
                return $parameters;
1✔
53
        }
54

55
        /**
56
         * @return string[]
57
         */
58
        public static function parseParameter(string $key): array
1✔
59
        {
60
                if (self::$parseCase === self::PARSE_LOWERCASE) {
1✔
61
                        return explode(self::$parseDelimiter, strtolower($key));
1✔
62
                }
63

64
                if (self::$parseCase === self::PARSE_UPPERCASE) {
1✔
65
                        return explode(self::$parseDelimiter, strtoupper($key));
1✔
66
                }
67

68
                return explode(self::$parseDelimiter, $key);
1✔
69
        }
70

71
        /**
72
         * Parse all environment variables
73
         *
74
         * @return mixed[]
75
         */
76
        public static function parseAllEnvironmentParameters(): array
77
        {
78
                $parameters = [];
1✔
79

80
                // @phpcs:ignore SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
81
                foreach ($_SERVER as $key => $value) {
1✔
82
                        // Ensure value
83
                        $value = getenv($key);
1✔
84
                        if ($value !== false) {
1✔
85
                                $parameters[$key] = $value;
1✔
86
                        }
87
                }
88

89
                return $parameters;
1✔
90
        }
91

92
        /**
93
         * @return bool|string
94
         */
95
        public static function parseEnvDebugMode(): mixed
96
        {
97
                $debug = getenv('NETTE_DEBUG');
1✔
98
                if ($debug !== false) {
1✔
99
                        return static::parseDebugValue($debug);
1✔
100
                }
101

102
                return false;
×
103
        }
104

105
        /**
106
         * @return bool|string
107
         */
108
        public static function parseDebugValue(string $debug): mixed
1✔
109
        {
110
                $value = $debug;
1✔
111

112
                if (strtolower($value) === 'true' || $value === '1') {
1✔
113
                        $debug = true;
1✔
114
                } elseif (strtolower($value) === 'false' || $value === '0') {
1✔
115
                        $debug = false;
1✔
116
                }
117

118
                return $debug;
1✔
119
        }
120

121
        /**
122
         * @param string[] $keys
123
         * @return array<string, mixed>
124
         */
125
        public static function deepParameters(mixed $array, array $keys, string $value): array
1✔
126
        {
127
                if ($keys === []) {
1✔
128
                        return [];
1✔
129
                }
130

131
                $key = array_shift($keys);
1✔
132

133
                if (!is_array($array)) {
1✔
134
                        throw new InvalidStateException(sprintf('Invalid structure for key "%s" value "%s"', implode($keys), $value));
×
135
                }
136

137
                if (!array_key_exists((string) $key, $array)) {
1✔
138
                        $array[$key] = [];
1✔
139
                }
140

141
                // Recursive
142
                $array[$key] = $keys === [] ? $value : self::deepParameters($array[$key], $keys, $value);
1✔
143

144
                return $array;
1✔
145
        }
146

147
        /**
148
         * Collect environment parameters with NETTE{delimiter=__} prefix
149
         *
150
         * @return mixed[]
151
         */
152
        public function getEnvironmentParameters(): array
153
        {
154
                return static::parseEnvironmentParameters();
1✔
155
        }
156

157
        /**
158
         * Collect all environment variables
159
         *
160
         * @return mixed[]
161
         */
162
        public function getAllEnvironmentParameters(): array
163
        {
164
                return static::parseAllEnvironmentParameters();
1✔
165
        }
166

167
        public function setEnvDebugMode(): void
168
        {
169
                $this->setDebugMode(static::parseEnvDebugMode());
1✔
170
        }
1✔
171

172
        public function setFileDebugMode(?string $fileName = null): void
1✔
173
        {
174
                // Given file name or default file path
175
                $appDir = $this->staticParameters['appDir'] ?? null;
1✔
176
                if ($fileName === null && $appDir === null)
1✔
177

178
                        return;
×
179

180
                // Try to load file
181
                $content = @file_get_contents($fileName ?? $appDir . '/../.debug');
1✔
182
                if ($content === false)
1✔
183

184
                        return;
×
185

186
                // File exists with no content
187
                if ($content === '') {
1✔
188
                        $this->setDebugMode(true);
1✔
189

190
                        return;
1✔
191
                }
192

193
                $debug = static::parseDebugValue(trim($content));
1✔
194
                $this->setDebugMode($debug);
1✔
195
        }
1✔
196

197
        public function addEnvParameters(): void
198
        {
199
                $this->addStaticParameters($this->getEnvironmentParameters());
1✔
200
        }
1✔
201

202
        public function setParseCase(int $mode): void
1✔
203
        {
204
                self::$parseCase = $mode;
1✔
205
        }
1✔
206

207
        /** @param non-empty-string $delimiter */
208
        public function setParseDelimiter(string $delimiter): void
1✔
209
        {
210
                self::$parseDelimiter = $delimiter;
1✔
211
        }
1✔
212

213
        /**
214
         * Collect default parameters
215
         *
216
         * @return mixed[]
217
         */
218
        protected function getDefaultParameters(): array
219
        {
220
                $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
1✔
221
                $last = (array) end($trace);
1✔
222
                $debugMode = static::detectDebugMode();
1✔
223

224
                return [
225
                        'appDir' => isset($trace[1]['file']) ? dirname($trace[1]['file']) : null,
1✔
226
                        'wwwDir' => isset($last['file']) ? dirname($last['file']) : null,
1✔
227
                        'debugMode' => $debugMode,
1✔
228
                        'productionMode' => !$debugMode,
1✔
229
                        'consoleMode' => PHP_SAPI === 'cli',
1✔
230
                ];
231
        }
232

233
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc