forked from xtrime-ru/TelegramApiServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
54 lines (44 loc) · 1.2 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
<?php
namespace TelegramApiServer;
final class Config
{
private static ?Config $instance = null;
private array $config;
public static function getInstance(): Config
{
if (null === self::$instance) {
self::$instance = new static();
}
return self::$instance;
}
/**
* is not allowed to call from outside to prevent from creating multiple instances,
* to use the singleton, you have to obtain the instance from Singleton::getInstance() instead.
*/
private function __construct()
{
$this->config = include ROOT_DIR . '/config.php';
}
/**
* @param string $key
* @param null $default
* @return mixed|null
*/
public function get($key = '', $default = null)
{
return $this->findByKey($key) ?? $default;
}
private function findByKey($key)
{
$key = (string) $key;
$path = \explode('.', $key);
$value = &$this->config;
foreach ($path as $pathKey) {
if (!\is_array($value) || !\array_key_exists($pathKey, $value)) {
return null;
}
$value = &$value[$pathKey];
}
return $value;
}
}