/
config.php
136 lines (118 loc) · 3.32 KB
/
config.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Config.php
*
* Created by Keyur Ajmera.
* User: Keyur Ajmera
* Date: 10/12/2015
* Time: 5:09 PM
*
* Read .config file and load all variables and access by
* - Config::get('VARIABLE NAME');
*
*/
class Config {
/**
* store all list of configuration
*
* @var array
*/
private static $configList;
/**
* with application load initial all list of configuration from
* .config file
*
* @param string $path
* @param string $file
*/
public static function init($path, $file = '.config')
{
// get file path of '.config' file
$filePath = rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
self::readFileAndSetInArray($filePath);
}
/**
* @param string $key
* @param null $defaultValue
* @return object
*/
public static function get($key, $defaultValue = null)
{
if (!isset(self::$configList))
self::init( explode('vendor', __DIR__)[0] );
if (array_key_exists($key, self::$configList))
{
return self::$configList[$key];
}
else
{
return $defaultValue;
}
}
/**
* remove comments from lines
* - Comments consider start line with '#', '-' or '=' sign
*
* @param array $lines
* @return array mixed
*/
private static function removeCommentFromLines($lines)
{
foreach ($lines as $key => $value) {
// trim all lines and set again in array
$lines[$key] = trim($value);
// Remove all lines start with hash (#), dash (-) and equal (=)
if (trim($value)[0] === '#'
|| trim($value)[0] === '-'
|| trim($value)[0] === '=') {
unset($lines[$key]);
}
}
return $lines;
}
/**
* read '.config' file and set all config variable on accessible
* array by `Config::get()`
*
* @param string $filePath
*/
protected static function readFileAndSetInArray($filePath)
{
// read file
$lines = self::readFile($filePath);
// Remove all lines start with hash (#) and dash (-)
$lines = self::removeCommentFromLines($lines);
// create new array with key and value
self::$configList = array();
foreach ($lines as $line) {
self::$configList[ explode('=', $line, 2)[0] ] = explode('=', $line, 2)[1];
}
}
/**
* read '.config' file and return all lines
*
* @param string $filePath
* @return array
*/
private static function readFile($filePath)
{
// get current setting of auto_detect_line_endings
$autodetect = ini_get('auto_detect_line_endings');
ini_set('auto_detect_line_endings', '1');
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// set again setting of auto_detect_line_endings
ini_set('auto_detect_line_endings', $autodetect);
// return all lines
return $lines;
}
}
/**
* add `getConfig()` function for configuration call without namespace call
* function can be call from anywhere global
*
*/
if(!function_exists("getConfig")) {
function getConfig($key, $defaultValue = null) {
return Config::get($key, $defaultValue);
}
}