1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: |
|
11: | namespace Opencart\System\Library;
|
12: | |
13: | |
14: |
|
15: | class Language {
|
16: | |
17: | |
18: |
|
19: | protected string $code;
|
20: | |
21: | |
22: |
|
23: | protected string $directory;
|
24: | |
25: | |
26: |
|
27: | protected array $path = [];
|
28: | |
29: | |
30: |
|
31: | protected array $data = [];
|
32: | |
33: | |
34: |
|
35: | protected array $cache = [];
|
36: |
|
37: | |
38: | |
39: | |
40: | |
41: |
|
42: | public function __construct(string $code) {
|
43: | $this->code = $code;
|
44: | }
|
45: |
|
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: |
|
54: | public function addPath(string $namespace, string $directory = ''): void {
|
55: | if (!$directory) {
|
56: | $this->directory = $namespace;
|
57: | } else {
|
58: | $this->path[$namespace] = $directory;
|
59: | }
|
60: | }
|
61: |
|
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: |
|
69: | public function get(string $key): string {
|
70: | return $this->data[$key] ?? $key;
|
71: | }
|
72: |
|
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: |
|
83: | public function set(string $key, string $value): void {
|
84: | $this->data[$key] = $value;
|
85: | }
|
86: |
|
87: | |
88: | |
89: | |
90: | |
91: | |
92: | |
93: |
|
94: | public function all(string $prefix = ''): array {
|
95: | if (!$prefix) {
|
96: | return $this->data;
|
97: | }
|
98: |
|
99: | $_ = [];
|
100: |
|
101: | $length = strlen($prefix);
|
102: |
|
103: | foreach ($this->data as $key => $value) {
|
104: | if (substr($key, 0, $length) == $prefix) {
|
105: | $_[substr($key, $length + 1)] = $value;
|
106: | }
|
107: | }
|
108: |
|
109: | return $_;
|
110: | }
|
111: |
|
112: | |
113: | |
114: | |
115: | |
116: |
|
117: | public function clear(): void {
|
118: | $this->data = [];
|
119: | }
|
120: |
|
121: | |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: |
|
130: | public function load(string $filename, string $prefix = '', string $code = ''): array {
|
131: | if (!$code) {
|
132: | $code = $this->code;
|
133: | }
|
134: |
|
135: | if (!isset($this->cache[$code][$filename])) {
|
136: | $_ = [];
|
137: |
|
138: |
|
139: | $file = $this->directory . $code . '/' . $filename . '.php';
|
140: |
|
141: | $namespace = '';
|
142: |
|
143: | $parts = explode('/', $filename);
|
144: |
|
145: | foreach ($parts as $part) {
|
146: | if (!$namespace) {
|
147: | $namespace .= $part;
|
148: | } else {
|
149: | $namespace .= '/' . $part;
|
150: | }
|
151: |
|
152: | if (isset($this->path[$namespace])) {
|
153: | $file = $this->path[$namespace] . $code . substr($filename, strlen($namespace)) . '.php';
|
154: | }
|
155: | }
|
156: |
|
157: | if (is_file($file)) {
|
158: | require($file);
|
159: | }
|
160: |
|
161: | $this->cache[$code][$filename] = $_;
|
162: | } else {
|
163: | $_ = $this->cache[$code][$filename];
|
164: | }
|
165: |
|
166: | if ($prefix) {
|
167: | foreach ($_ as $key => $value) {
|
168: | $_[$prefix . '_' . $key] = $value;
|
169: |
|
170: | unset($_[$key]);
|
171: | }
|
172: | }
|
173: |
|
174: | $this->data = array_merge($this->data, $_);
|
175: |
|
176: | return $this->data;
|
177: | }
|
178: | }
|
179: | |