1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Application configuration class to import `Config.ini` file and set other defaults |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2018 Jared Howland |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @author Jared Howland <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Gospel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Application configuration class to import `Config.ini` file and set other defaults |
17
|
|
|
*/ |
18
|
|
|
class Config |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array|false $config Array of values in `Config.ini`. `false` if file cannot be parsed. |
22
|
|
|
*/ |
23
|
|
|
private static $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Set error reporting based on if app is in development or production |
27
|
|
|
* |
28
|
|
|
* @param null |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public static function setErrorReporting(): void |
33
|
|
|
{ |
34
|
|
|
if (self::get('development')) { |
35
|
|
|
ini_set('display_errors', '1'); |
36
|
|
|
ini_set('error_reporting', 'E_ALL ^ E_NOTICE'); |
37
|
|
|
} else { |
38
|
|
|
ini_set('display_errors', '0'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get config setting from `.ini` file |
44
|
|
|
* |
45
|
|
|
* @param string $setting Setting name |
46
|
|
|
* |
47
|
|
|
* @return string Return setting value from `.ini` file |
48
|
|
|
*/ |
49
|
|
|
public static function get(string $setting): string |
50
|
|
|
{ |
51
|
|
|
if (empty(self::$config) && is_array(parse_ini_file('Config.ini'))) { |
52
|
|
|
self::$config = parse_ini_file('Config.ini'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return self::settingExists($setting); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if setting in `.ini` file exists |
60
|
|
|
* |
61
|
|
|
* @param string $setting Setting name to check for |
62
|
|
|
* |
63
|
|
|
* @throws \UnexpectedValueException when `$setting` does not exist |
64
|
|
|
* |
65
|
|
|
* @return string Setting value from `.ini` file |
66
|
|
|
*/ |
67
|
|
|
private static function settingExists(string $setting): string |
68
|
|
|
{ |
69
|
|
|
if (isset(self::$config[$setting])) { |
70
|
|
|
return self::$config[$setting]; |
71
|
|
|
} else { |
72
|
|
|
throw new \UnexpectedValueException("'$setting' is not a valid config setting. Please check your 'config.ini' file for valid config options.\n"); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
config::setErrorReporting(); |
78
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.