-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.class.php
64 lines (55 loc) · 1.31 KB
/
Config.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/** Prevent direct access to this file */
if (defined('APPLICATION') === false) {
die('Direct access not permitted!');
}
class Config
{
/**
* Name of the file this class represents.
* @var string
*/
private $filename = '';
/**
* Entire configuration array this class represents.
* @var array
*/
private $config = [];
/**
* Creates a new instance of this class.
* @param string $filename Path of the file to use.
* @return void
*/
public function __construct(string $filename)
{
$this->filename = $filename;
$this->config = $this->readConfig($filename);
}
/**
* Returns a specific item from the array by dot notation.
* @param string $path Dot notated path to the array item.
* @param mixed $default Default value if the result is null.
* @return mixed The value of that item in the array.
*/
public function get($path, $default = null)
{
$array = $this->config;
$parts = explode('.', $path);
foreach ($parts as $part) {
if (isset($array[$part]) === false) {
return $default;
}
$array = $array[$part];
}
return $array ?? $default;
}
/**
* Returns the content of the specified file.
* @param string $filename Path of the file to load.
* @return mixed File contents.
*/
private function readConfig(string $filename)
{
return include $filename;
}
}