Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
21 / 21
CRAP
100.00% covered (success)
100.00%
1 / 1
App
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
21 / 21
45
100.00% covered (success)
100.00%
1 / 1
 set
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
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 config
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 router
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 services
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 events
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 modules
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 autoloader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 env
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
10
 environment
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 isLocal
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDev
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isTesting
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isStaging
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
2
 isDown
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isUp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isSecretRequest
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Pop PHP Framework (https://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@noladev.com>
7 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
8 * @license    https://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop;
15
16use Pop\Cookie\Cookie;
17use Pop\Router\Router;
18
19/**
20 * Application helper class
21 *
22 * @category   Pop
23 * @package    Pop
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2026 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    4.4.0
28 */
29class App
30{
31
32    /**
33     * Application object
34     * @var ?Application
35     */
36    private static ?Application $application = null;
37
38    /**
39     * Set application object
40     *
41     * @param  Application $application
42     * @return void
43     */
44    public static function set(Application $application): void
45    {
46        self::$application = $application;
47    }
48
49    /**
50     * Get application object
51     *
52     * @return ?Application
53     */
54    public static function get(): ?Application
55    {
56        return self::$application;
57    }
58
59    /**
60     * Has application object
61     *
62     * @return bool
63     */
64    public static function has(): bool
65    {
66        return (self::$application !== null);
67    }
68
69    /**
70     * Get configuration
71     *
72     * @param  ?string $key
73     * @return mixed
74     */
75    public static function config(?string $key = null): mixed
76    {
77        if (self::$application !== null) {
78            return ($key !== null) ? self::$application->config[$key] : self::$application->config();
79        } else {
80            return null;
81        }
82    }
83
84    /**
85     * Get router
86     *
87     * @return ?Router
88     */
89    public static function router(): ?Router
90    {
91        return (self::$application !== null) ? self::$application->router() : null;
92    }
93
94    /**
95     * Get service locator
96     *
97     * @param  ?string $key
98     * @return mixed
99     */
100    public static function services(?string $key = null): mixed
101    {
102        if (self::$application !== null) {
103            return ($key !== null) ? self::$application->services[$key] : self::$application->services();
104        } else {
105            return null;
106        }
107    }
108
109    /**
110     * Get event manager
111     *
112     * @param  ?string $key
113     * @return mixed
114     */
115    public static function events(?string $key = null): mixed
116    {
117        if (self::$application !== null) {
118            return ($key !== null) ? self::$application->events[$key] : self::$application->events();
119        } else {
120            return null;
121        }
122    }
123
124    /**
125     * Get module manager
126     *
127     * @param  ?string $key
128     * @return mixed
129     */
130    public static function modules(?string $key = null): mixed
131    {
132        if (self::$application !== null) {
133            return ($key !== null) ? self::$application->modules[$key] : self::$application->modules();
134        } else {
135            return null;
136        }
137    }
138
139    /**
140     * Get autoloader
141     *
142     * @return mixed
143     */
144    public static function autoloader(): mixed
145    {
146        return (self::$application !== null) ? self::$application->autoloader() : null;
147    }
148
149    /**
150     * Get application name
151     *
152     * @return ?string
153     */
154    public static function name(): ?string
155    {
156        return self::env('APP_NAME');
157    }
158
159    /**
160     * Get application URL
161     *
162     * @return ?string
163     */
164    public static function url(): ?string
165    {
166        return self::env('APP_URL');
167    }
168
169    /**
170     * Get environment value
171     *
172     * @param  string $key
173     * @param  mixed  $default
174     * @return mixed
175     */
176    public static function env(string $key, mixed $default = null): mixed
177    {
178        if (!isset($_ENV[$key])) {
179            return $default;
180        }
181
182        $value = $_ENV[$key];
183
184        switch ($value) {
185            case 'true':
186            case '(true)':
187                $value = true;
188                break;
189            case 'false':
190            case '(false)':
191                $value = false;
192                break;
193            case 'null':
194            case '(null)':
195                $value = null;
196                break;
197            case 'empty':
198            case '(empty)':
199                $value = '';
200                break;
201        }
202
203        return $value;
204    }
205
206    /**
207     * Get application environment
208     *
209     * @param  mixed $env
210     * @return string|null|bool
211     */
212    public static function environment(mixed $env = null): string|null|bool
213    {
214        if ($env === null) {
215            return self::env('APP_ENV');
216        }
217
218        if (!is_array($env)) {
219            $env = [$env];
220        }
221
222        return in_array(self::env('APP_ENV'), $env);
223    }
224
225    /**
226     * Check if application environment is local
227     *
228     * @return bool
229     */
230    public static function isLocal(): bool
231    {
232        return (self::env('APP_ENV') == 'local');
233    }
234
235    /**
236     * Check if application environment is dev
237     *
238     * @return bool
239     */
240    public static function isDev(): bool
241    {
242        return (self::env('APP_ENV') == 'dev');
243    }
244
245    /**
246     * Check if application environment is testing
247     *
248     * @return bool
249     */
250    public static function isTesting(): bool
251    {
252        return (self::env('APP_ENV') == 'testing');
253    }
254
255    /**
256     * Check if application environment is staging
257     *
258     * @return bool
259     */
260    public static function isStaging(): bool
261    {
262        return (self::env('APP_ENV') == 'staging');
263    }
264
265    /**
266     * Check if application environment is production
267     *
268     * @return bool
269     */
270    public static function isProduction(): bool
271    {
272        return !empty(self::env('APP_ENV')) && str_starts_with(self::env('APP_ENV'), 'prod');
273    }
274
275    /**
276     * Check if application is in maintenance mode
277     *
278     * @return bool
279     */
280    public static function isDown(): bool
281    {
282        return (self::env('MAINTENANCE_MODE') === true);
283    }
284
285    /**
286     * Check if application is in not maintenance mode
287     *
288     * @return bool
289     */
290    public static function isUp(): bool
291    {
292        return (self::env('MAINTENANCE_MODE') === false);
293    }
294
295    /**
296     * Check if application is in not maintenance mode
297     *
298     * @return bool
299     */
300    public static function isSecretRequest(): bool
301    {
302        if (isset($_GET['secret'])) {
303            $secret = $_GET['secret'];
304            $cookie = Cookie::getInstance();
305            $cookie->set('pop_mm_secret', $_GET['secret']);
306        } else {
307            $cookie = Cookie::getInstance();
308            $secret = $cookie['pop_mm_secret'];
309        }
310
311        return (!empty($secret) && ($secret == App::env('MAINTENANCE_MODE_SECRET')));
312    }
313
314}