-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
137 lines (125 loc) · 3.68 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
137
<?php
namespace Colibri\Config;
use Colibri\Pattern\Helper;
use Colibri\Util\Arr;
/**
* Config.
*
* @method static mixed application(string $key = null, mixed $default = null) gets config value by keys, separated with dot
* @method static string divisions(string $key = null, mixed $default = null) gets config value by keys, separated with dot
*/
class Config extends Helper
{
/**
* @var array in-memory cache
*/
protected static $allLoadedConfigs = [];
/**
* @var string path to directory with config files
*/
protected static $baseDir = null;
/**
* Retrieve full real path to config.
*
* @param string $name config file name
*
* @return string
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
protected static function getFilepath($name)
{
return static::getBaseDir() . '/' . $name . '.php';
}
/**
* @param string $name config file name
*
* @return array
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
protected static function load($name)
{
/* @noinspection PhpIncludeInspection */
return include static::getFilepath($name);
}
/**
* @param string $name config file name
*
* @return bool
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
public static function exists($name)
{
return file_exists(static::getFilepath($name));
}
/**
* @param string $name config file name
*
* @return array
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
final public static function get($name)
{
return isset(self::$allLoadedConfigs[$name])
? self::$allLoadedConfigs[$name]
: self::$allLoadedConfigs[$name] = Arr::overwrite(static::load($name), LocalConfig::load($name));
}
/**
* Returns config if exists or empty array if not.
*
* @param string $name config file name
*
* @return array
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
final public static function getOrEmpty($name)
{
return static::exists($name) ? self::get($name) : [];
}
/**
* Sets path of the directory where config files are stored.
*
* @param string $path
*
* @return string returns the real path
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
public static function setBaseDir($path)
{
$path = realpath(rtrim($path, '/\\ '));
if ($path === false) {
throw new \InvalidArgumentException("cat`t get real path: seems like path does`t exists: $path");
}
return static::$baseDir = $path;
}
/**
* @return string
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
public static function getBaseDir()
{
return static::$baseDir === null
? static::setBaseDir(__DIR__ . '/../../../../application/configs')
: static::$baseDir;
}
/**
* @param string $name config file name
* @param array $arguments (string $key, mixed $default = null)
*
* @return mixed
*
* @throws \InvalidArgumentException if can`t get the real-path of config file
*/
public static function __callStatic($name, $arguments)
{
$key = isset($arguments[0]) ? $arguments[0] : null;
$default = isset($arguments[1]) ? $arguments[1] : null;
return Arr::get(static::get($name), $key, $default);
}
}