1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: |
|
11: | namespace Opencart\System\Engine;
|
12: | |
13: | |
14: | |
15: | |
16: |
|
17: | class Loader {
|
18: | |
19: | |
20: |
|
21: | protected \Opencart\System\Engine\Registry $registry;
|
22: |
|
23: | |
24: | |
25: | |
26: | |
27: |
|
28: | public function __construct(\Opencart\System\Engine\Registry $registry) {
|
29: | $this->registry = $registry;
|
30: | }
|
31: |
|
32: | |
33: | |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: |
|
41: | public function __get(string $key): object {
|
42: | return $this->registry->get($key);
|
43: | }
|
44: |
|
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: |
|
55: | public function __set(string $key, object $value): void {
|
56: | $this->registry->set($key, $value);
|
57: | }
|
58: |
|
59: | |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: |
|
69: | public function controller(string $route, ...$args) {
|
70: |
|
71: | $route = preg_replace('/[^a-zA-Z0-9_|\/\.]/', '', str_replace('|', '.', $route));
|
72: |
|
73: | $trigger = $route;
|
74: |
|
75: |
|
76: | $this->event->trigger('controller/' . $trigger . '/before', [&$route, &$args]);
|
77: |
|
78: |
|
79: | $action = new \Opencart\System\Engine\Action($route);
|
80: |
|
81: | while ($action) {
|
82: |
|
83: | $output = $action->execute($this->registry, $args);
|
84: |
|
85: |
|
86: | $action = '';
|
87: |
|
88: |
|
89: | if ($output instanceof \Opencart\System\Engine\Action) {
|
90: | $action = $output;
|
91: | }
|
92: | }
|
93: |
|
94: |
|
95: | $this->event->trigger('controller/' . $trigger . '/after', [&$route, &$args, &$output]);
|
96: |
|
97: | return $output;
|
98: | }
|
99: |
|
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: |
|
107: | public function model(string $route): void {
|
108: |
|
109: | $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route);
|
110: |
|
111: |
|
112: | $key = 'model_' . str_replace('/', '_', $route);
|
113: |
|
114: | if (!$this->registry->has($key)) {
|
115: |
|
116: | $class = 'Opencart\\' . $this->config->get('application') . '\Model\\' . str_replace(['_', '/'], ['', '\\'], ucwords($route, '_/'));
|
117: |
|
118: | if (class_exists($class)) {
|
119: | $proxy = new \Opencart\System\Engine\Proxy();
|
120: |
|
121: | foreach (get_class_methods($class) as $method) {
|
122: | $reflection = new \ReflectionMethod($class, $method);
|
123: |
|
124: | if ((substr($method, 0, 2) != '__') && $reflection->isPublic()) {
|
125: |
|
126: | $proxy->{$method} = function(&...$args) use ($route, $method) {
|
127: | $route = $route . '/' . $method;
|
128: |
|
129: | $trigger = $route;
|
130: |
|
131: |
|
132: | $this->event->trigger('model/' . $trigger . '/before', [&$route, &$args]);
|
133: |
|
134: |
|
135: | $pos = strrpos($route, '/');
|
136: |
|
137: | $class = substr($route, 0, $pos);
|
138: | $method = substr($route, $pos + 1);
|
139: |
|
140: |
|
141: | $key = 'callback_' . str_replace('/', '_', $class);
|
142: |
|
143: | if (!$this->registry->has($key)) {
|
144: |
|
145: | $model = $this->factory->model($class);
|
146: |
|
147: |
|
148: | $this->registry->set($key, $model);
|
149: | } else {
|
150: | $model = $this->registry->get($key);
|
151: | }
|
152: |
|
153: | $callable = [$model, $method];
|
154: |
|
155: | if (is_callable($callable)) {
|
156: | $output = $callable(...$args);
|
157: | } else {
|
158: | throw new \Exception('Error: Could not call model/' . $route . '!');
|
159: | }
|
160: |
|
161: |
|
162: | $this->event->trigger('model/' . $trigger . '/after', [&$route, &$args, &$output]);
|
163: |
|
164: | return $output;
|
165: | };
|
166: | }
|
167: | }
|
168: |
|
169: | $this->registry->set($key, $proxy);
|
170: | } else {
|
171: | throw new \Exception('Error: Could not load model ' . $class . '!');
|
172: | }
|
173: | }
|
174: | }
|
175: |
|
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: |
|
187: | public function view(string $route, array $data = [], string $code = ''): string {
|
188: |
|
189: | $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route);
|
190: |
|
191: | $trigger = $route;
|
192: |
|
193: | $output = '';
|
194: |
|
195: |
|
196: | $this->event->trigger('view/' . $trigger . '/before', [&$route, &$data, &$code, &$output]);
|
197: |
|
198: | if (!$output) {
|
199: |
|
200: | $output = $this->template->render($route, $data, $code);
|
201: | }
|
202: |
|
203: |
|
204: | $this->event->trigger('view/' . $trigger . '/after', [&$route, &$data, &$output]);
|
205: |
|
206: | return $output;
|
207: | }
|
208: |
|
209: | |
210: | |
211: | |
212: | |
213: | |
214: | |
215: | |
216: | |
217: |
|
218: | public function language(string $route, string $prefix = '', string $code = ''): array {
|
219: |
|
220: | $route = preg_replace('/[^a-zA-Z0-9_\-\/]/', '', $route);
|
221: |
|
222: | $trigger = $route;
|
223: |
|
224: |
|
225: | $this->event->trigger('language/' . $trigger . '/before', [&$route, &$prefix, &$code]);
|
226: |
|
227: | $output = $this->language->load($route, $prefix, $code);
|
228: |
|
229: |
|
230: | $this->event->trigger('language/' . $trigger . '/after', [&$route, &$prefix, &$code, &$output]);
|
231: |
|
232: | return $output;
|
233: | }
|
234: |
|
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: |
|
243: | public function library(string $route, &...$args): object {
|
244: |
|
245: | $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route);
|
246: |
|
247: |
|
248: | $key = 'library_' . str_replace('/', '_', $route);
|
249: |
|
250: | if (!$this->registry->has($key)) {
|
251: |
|
252: | $library = $this->factory->library($route, $args);
|
253: |
|
254: |
|
255: | $this->registry->set($key, $library);
|
256: | } else {
|
257: | $library = $this->registry->get($key);
|
258: | }
|
259: |
|
260: | return $library;
|
261: | }
|
262: |
|
263: | |
264: | |
265: | |
266: | |
267: | |
268: | |
269: |
|
270: | public function config(string $route): array {
|
271: |
|
272: | $route = preg_replace('/[^a-zA-Z0-9_\-\/]/', '', $route);
|
273: |
|
274: | $trigger = $route;
|
275: |
|
276: |
|
277: | $this->event->trigger('config/' . $trigger . '/before', [&$route]);
|
278: |
|
279: | $output = $this->config->load($route);
|
280: |
|
281: |
|
282: | $this->event->trigger('config/' . $trigger . '/after', [&$route, &$output]);
|
283: |
|
284: | return $output;
|
285: | }
|
286: |
|
287: | |
288: | |
289: | |
290: | |
291: | |
292: | |
293: |
|
294: | public function helper(string $route): void {
|
295: | $route = preg_replace('/[^a-zA-Z0-9_\/]/', '', $route);
|
296: |
|
297: | if (!str_starts_with($route, 'extension/')) {
|
298: | $file = DIR_SYSTEM . 'helper/' . $route . '.php';
|
299: | } else {
|
300: | $parts = explode('/', substr($route, 10));
|
301: |
|
302: | $code = array_shift($parts);
|
303: |
|
304: | $file = DIR_EXTENSION . $code . '/system/helper/' . implode('/', $parts) . '.php';
|
305: | }
|
306: |
|
307: | if (is_file($file)) {
|
308: | include_once($file);
|
309: | } else {
|
310: | throw new \Exception('Error: Could not load helper ' . $route . '!');
|
311: | }
|
312: | }
|
313: | }
|
314: | |