Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
38 / 40
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Environment
95.00% covered (success)
95.00%
38 / 40
90.00% covered (success)
90.00%
9 / 10
18
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 enableTestMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadEnvironment
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 setupDebugMode
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
6
 isDebugMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldLogQueries
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEnvironment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isProduction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDevelopment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Environment configuration manager
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2025-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2025-08-03
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ;
21
22use Monolog\Handler\StreamHandler;
23use Monolog\Level;
24use Monolog\Logger;
25use Symfony\Component\Dotenv\Dotenv;
26use Symfony\Component\ErrorHandler\Debug;
27use Symfony\Component\ErrorHandler\ErrorHandler;
28
29class Environment
30{
31    private static bool $debugMode = false;
32
33    private static bool $debugLogQueries = false;
34
35    private static bool $initialized = false;
36
37    private static string $environment = 'production';
38
39    private static bool $testMode = false;
40
41    private static ?Dotenv $dotenv = null;
42
43    public static function init(): void
44    {
45        if (self::$initialized) {
46            return;
47        }
48
49        if (!self::$testMode) {
50            self::loadEnvironment();
51        }
52
53        self::setupDebugMode();
54        self::$initialized = true;
55    }
56
57    public static function enableTestMode(): void
58    {
59        self::$testMode = true;
60    }
61
62    private static function loadEnvironment(): void
63    {
64        $envPath = dirname(__DIR__, levels: 2);
65        $envFile = $envPath . '/.env';
66
67        if (file_exists($envFile)) {
68            self::$dotenv = new Dotenv();
69            self::$dotenv->load($envFile);
70        }
71    }
72
73    private static function setupDebugMode(): void
74    {
75        if (self::$testMode) {
76            return;
77        }
78
79        self::$debugMode = filter_var($_ENV['DEBUG'] ?? false, FILTER_VALIDATE_BOOLEAN);
80        self::$debugLogQueries = filter_var($_ENV['DEBUG_LOG_QUERIES'] ?? false, FILTER_VALIDATE_BOOLEAN);
81        // Fall back to the process environment (getenv) so APP_ENV can be set via a
82        // real environment variable in CLI/CI/Docker, where variables_order often
83        // omits "E" and therefore does not populate $_ENV from the shell.
84        $appEnv = $_ENV['APP_ENV'] ?? getenv('APP_ENV');
85        self::$environment = is_string($appEnv) && $appEnv !== '' ? $appEnv : 'production';
86
87        error_reporting(self::$debugMode ? E_ALL : E_ERROR | E_WARNING | E_PARSE);
88
89        if (self::$debugMode) {
90            Debug::enable();
91            ErrorHandler::register();
92            return;
93        }
94
95        $errorHandler = ErrorHandler::register();
96
97        $logger = new Logger(name: 'phpmyfaq');
98        $logTarget = $_ENV['ERROR_LOG'] ?? 'php://stderr';
99        $logger->pushHandler(new StreamHandler($logTarget, Level::Warning));
100
101        $levelsMap = [
102            E_DEPRECATED => null,
103            E_USER_DEPRECATED => null,
104        ];
105        $errorHandler->setDefaultLogger($logger, $levelsMap);
106
107        $errorHandler->screamAt(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
108        $errorHandler->scopeAt(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
109    }
110
111    public static function isDebugMode(): bool
112    {
113        return self::$debugMode;
114    }
115
116    public static function shouldLogQueries(): bool
117    {
118        return self::$debugLogQueries;
119    }
120
121    public static function get(string $key, mixed $default = null): mixed
122    {
123        return $_ENV[$key] ?? $default;
124    }
125
126    public static function getEnvironment(): string
127    {
128        return self::$environment;
129    }
130
131    public static function isProduction(): bool
132    {
133        return self::getEnvironment() === 'production';
134    }
135
136    public static function isDevelopment(): bool
137    {
138        return self::getEnvironment() === 'development';
139    }
140}