event.php (2083B)
1 <?php 2 /** 3 * @package OpenCart 4 * @author Daniel Kerr 5 * @copyright Copyright (c) 2005 - 2017, OpenCart, Ltd. (https://www.opencart.com/) 6 * @license https://opensource.org/licenses/GPL-3.0 7 * @link https://www.opencart.com 8 */ 9 10 /** 11 * Event class 12 * 13 * Event System Userguide 14 * 15 * https://github.com/opencart/opencart/wiki/Events-(script-notifications)-2.2.x.x 16 */ 17 class Event { 18 protected $registry; 19 protected $data = array(); 20 21 /** 22 * Constructor 23 * 24 * @param object $route 25 */ 26 public function __construct($registry) { 27 $this->registry = $registry; 28 } 29 30 /** 31 * 32 * 33 * @param string $trigger 34 * @param object $action 35 * @param int $priority 36 */ 37 public function register($trigger, Action $action, $priority = 0) { 38 $this->data[] = array( 39 'trigger' => $trigger, 40 'action' => $action, 41 'priority' => $priority 42 ); 43 44 $sort_order = array(); 45 46 foreach ($this->data as $key => $value) { 47 $sort_order[$key] = $value['priority']; 48 } 49 50 array_multisort($sort_order, SORT_ASC, $this->data); 51 } 52 53 /** 54 * 55 * 56 * @param string $event 57 * @param array $args 58 */ 59 public function trigger($event, array $args = array()) { 60 foreach ($this->data as $value) { 61 if (preg_match('/^' . str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($value['trigger'], '/')) . '/', $event)) { 62 $result = $value['action']->execute($this->registry, $args); 63 64 if (!is_null($result) && !($result instanceof Exception)) { 65 return $result; 66 } 67 } 68 } 69 } 70 71 /** 72 * 73 * 74 * @param string $trigger 75 * @param string $route 76 */ 77 public function unregister($trigger, $route) { 78 foreach ($this->data as $key => $value) { 79 if ($trigger == $value['trigger'] && $value['action']->getId() == $route) { 80 unset($this->data[$key]); 81 } 82 } 83 } 84 85 /** 86 * 87 * 88 * @param string $trigger 89 */ 90 public function clear($trigger) { 91 foreach ($this->data as $key => $value) { 92 if ($trigger == $value['trigger']) { 93 unset($this->data[$key]); 94 } 95 } 96 } 97 }