| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: |
|
| 11: | namespace Opencart\System\Engine;
|
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: |
|
| 17: | class Event {
|
| 18: | |
| 19: | |
| 20: |
|
| 21: | protected \Opencart\System\Engine\Registry $registry;
|
| 22: | |
| 23: | |
| 24: |
|
| 25: | protected array $data = [];
|
| 26: |
|
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: |
|
| 32: | public function __construct(\Opencart\System\Engine\Registry $registry) {
|
| 33: | $this->registry = $registry;
|
| 34: | }
|
| 35: |
|
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: |
|
| 45: | public function register(string $trigger, \Opencart\System\Engine\Action $action, int $priority = 0): void {
|
| 46: | $this->data[] = [
|
| 47: | 'trigger' => $trigger,
|
| 48: | 'action' => $action,
|
| 49: | 'priority' => $priority
|
| 50: | ];
|
| 51: |
|
| 52: | $sort_order = [];
|
| 53: |
|
| 54: | foreach ($this->data as $key => $value) {
|
| 55: | $sort_order[$key] = $value['priority'];
|
| 56: | }
|
| 57: |
|
| 58: | array_multisort($sort_order, SORT_ASC, $this->data);
|
| 59: | }
|
| 60: |
|
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: |
|
| 69: | public function trigger(string $event, array $args = []) {
|
| 70: | foreach ($this->data as $value) {
|
| 71: | if (preg_match('/^' . str_replace(['\*', '\?'], ['.*', '.'], preg_quote($value['trigger'], '/')) . '/', $event)) {
|
| 72: | $value['action']->execute($this->registry, $args);
|
| 73: | }
|
| 74: | }
|
| 75: |
|
| 76: | return '';
|
| 77: | }
|
| 78: |
|
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: |
|
| 87: | public function unregister(string $trigger, string $route): void {
|
| 88: | foreach ($this->data as $key => $value) {
|
| 89: | if ($trigger == $value['trigger'] && $value['action']->getId() == $route) {
|
| 90: | unset($this->data[$key]);
|
| 91: | }
|
| 92: | }
|
| 93: | }
|
| 94: |
|
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: |
|
| 102: | public function clear(string $trigger): void {
|
| 103: | foreach ($this->data as $key => $value) {
|
| 104: | if ($trigger == $value['trigger']) {
|
| 105: | unset($this->data[$key]);
|
| 106: | }
|
| 107: | }
|
| 108: | }
|
| 109: | }
|
| 110: | |