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