1 | <?php |
||
5 | class Config |
||
6 | { |
||
7 | |||
8 | protected static $config = false; |
||
9 | |||
10 | 11 | public function __construct() |
|
11 | { |
||
12 | 11 | if (!is_file(CONFIG_FILE)) { |
|
13 | throw new \Exception("Config file not found: '" . CONFIG_FILE . "'"); |
||
14 | } |
||
15 | 11 | if (self::$config === false) { |
|
16 | 1 | self::$config = require CONFIG_FILE; |
|
17 | } |
||
18 | 11 | } |
|
19 | |||
20 | 11 | protected function get($name, $default = null) |
|
21 | { |
||
22 | 11 | if (isset(self::$config[$name])) { |
|
23 | 11 | return self::$config[$name]; |
|
24 | } |
||
25 | return $default; |
||
26 | } |
||
27 | |||
28 | public function debug() |
||
29 | { |
||
30 | return (bool) $this->get('debug', false); |
||
31 | } |
||
32 | |||
33 | public function mode() |
||
34 | { |
||
35 | return $this->get('mode', 'production'); |
||
36 | } |
||
37 | |||
38 | public function mail() |
||
39 | { |
||
40 | return $this->get('mail', ['transport' => 'mail']); |
||
41 | } |
||
42 | |||
43 | 11 | public function filesystems() |
|
44 | { |
||
45 | $default = [ |
||
46 | 11 | 'cache' => [ |
|
47 | 'type' => 'local', |
||
48 | 'root' => __DIR__ . '/../data/cache', |
||
49 | ], |
||
50 | 'storage' => [ |
||
51 | 'type' => 'local', |
||
52 | 'root' => __DIR__ . '/../data/storage', |
||
53 | ], |
||
54 | ]; |
||
55 | 11 | return $this->get('filesystems', $default); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get the Base URL of the application. Never has a trailing slash. |
||
60 | * @return string |
||
61 | */ |
||
62 | public function baseUrl($absolute = false) |
||
|
|||
63 | { |
||
64 | $calculatedBaseUrl = substr($_SERVER['SCRIPT_NAME'], 0, -(strlen('index.php'))); |
||
65 | $baseUrl = $this->get('base_url', $calculatedBaseUrl); |
||
66 | $baseUrlTrimmed = rtrim($baseUrl, ' /'); |
||
67 | $protocol = (!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://'; |
||
68 | $host = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : 'localhost'; |
||
69 | return $absolute ? $protocol . $host . $baseUrlTrimmed : $baseUrlTrimmed; |
||
70 | } |
||
71 | |||
72 | public function siteTitle() |
||
73 | { |
||
74 | return $this->get('site_title', 'A ' . App::name() . ' website'); |
||
75 | } |
||
76 | |||
77 | public function siteEmail() |
||
78 | { |
||
79 | return $this->get('site_email', '[email protected]'); |
||
80 | } |
||
81 | |||
82 | public function databaseHost() |
||
83 | { |
||
84 | return self::get('databaseHost', 'localhost'); |
||
85 | } |
||
86 | |||
87 | public function databaseName() |
||
88 | { |
||
89 | return self::get('databaseName', 'taggedfiles'); |
||
90 | } |
||
91 | |||
92 | public function databaseUser() |
||
93 | { |
||
94 | return self::get('databaseUser', 'root'); |
||
95 | } |
||
96 | |||
97 | public function databasePassword() |
||
98 | { |
||
99 | return self::get('databasePassword', ''); |
||
100 | } |
||
101 | } |
||
102 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: