1: | <?php |
2: | /** |
3: | * @package OpenCart |
4: | * |
5: | * @author Daniel Kerr |
6: | * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) |
7: | * @license https://opensource.org/licenses/GPL-3.0 |
8: | * |
9: | * @see https://www.opencart.com |
10: | */ |
11: | namespace Opencart\System\Engine; |
12: | /** |
13: | * Class Config |
14: | */ |
15: | class Config { |
16: | /** |
17: | * @var string |
18: | */ |
19: | protected string $directory; |
20: | /** |
21: | * @var array<string, string> |
22: | */ |
23: | private array $path = []; |
24: | /** |
25: | * @var array<string, string> |
26: | */ |
27: | private array $data = []; |
28: | |
29: | /** |
30: | * addPath |
31: | * |
32: | * @param string $namespace |
33: | * @param string $directory |
34: | */ |
35: | public function addPath(string $namespace, string $directory = ''): void { |
36: | if (!$directory) { |
37: | $this->directory = $namespace; |
38: | } else { |
39: | $this->path[$namespace] = $directory; |
40: | } |
41: | } |
42: | |
43: | /** |
44: | * Get |
45: | * |
46: | * @param string $key |
47: | * |
48: | * @return mixed |
49: | */ |
50: | public function get(string $key) { |
51: | return $this->data[$key] ?? ''; |
52: | } |
53: | |
54: | /** |
55: | * Set |
56: | * |
57: | * @param string $key |
58: | * @param mixed $value |
59: | */ |
60: | public function set(string $key, $value): void { |
61: | $this->data[$key] = $value; |
62: | } |
63: | |
64: | /** |
65: | * Has |
66: | * |
67: | * @param string $key |
68: | * |
69: | * @return bool |
70: | */ |
71: | public function has(string $key): bool { |
72: | return isset($this->data[$key]); |
73: | } |
74: | |
75: | /** |
76: | * Load |
77: | * |
78: | * @param string $filename |
79: | * |
80: | * @return array<string, string> |
81: | */ |
82: | public function load(string $filename): array { |
83: | $file = $this->directory . $filename . '.php'; |
84: | |
85: | $namespace = ''; |
86: | |
87: | $parts = explode('/', $filename); |
88: | |
89: | foreach ($parts as $part) { |
90: | if (!$namespace) { |
91: | $namespace .= $part; |
92: | } else { |
93: | $namespace .= '/' . $part; |
94: | } |
95: | |
96: | if (isset($this->path[$namespace])) { |
97: | $file = $this->path[$namespace] . substr($filename, strlen($namespace)) . '.php'; |
98: | } |
99: | } |
100: | |
101: | if (is_file($file)) { |
102: | $_ = []; |
103: | |
104: | require($file); |
105: | |
106: | $this->data = array_merge($this->data, $_); |
107: | |
108: | return $this->data; |
109: | } else { |
110: | return []; |
111: | } |
112: | } |
113: | } |
114: |