Dispatch.php
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* 调度方法
* User: ziy
* Date: 14-7-22
* Time: 下午8:36
*/
class YHMCart_Cart_Dispatch
{
protected $_uid = 0;
/**
* @var YHMCart_Hook_Orders
*/
protected $hookOrders = null;
/**
* @return YHMCart_Hook_Orders
*/
protected function hookOrders()
{
if ($this->hookOrders == null) {
$this->hookOrders = new YHMCart_Hook_Orders();
}
return $this->hookOrders;
}
/**
* 添加到购物车时执行
* @param $productSku
* @param $buyNumber
*/
protected function addPreDispatch(YHMCart_Hook_Orders $cart)
{
$this->_dispatch($cart, 'add_preDispatch');
}
/**
* 添加到购物车后执行
* @param $productSku
* @param $buyNumber
*/
protected function addEndDispatch(YHMCart_Hook_Orders $cart)
{
$this->_dispatch($cart, 'add_endDispatch');
}
/**
* 处理钩子
* @param array $package
* @param $dispatchName
*/
protected function _dispatch(YHMCart_Hook_Orders $package, $dispatchName)
{
$defaultDispatch = array();
$channelDispatch = array();
if (!empty(YHMCart_Hook_CartConfigs::$defaultDispatch[$dispatchName])) {
$defaultDispatch = YHMCart_Hook_CartConfigs::$defaultDispatch[$dispatchName];
}
if (!empty($this->_dispatchConfigs[$dispatchName])) {
$channelDispatch = $this->_dispatchConfigs[$dispatchName];
}
$_fun = $channelDispatch + $defaultDispatch;
ksort($_fun);
if (!empty($_fun)) {
foreach ($_fun as $fun) {
if (method_exists($fun, 'run')) {
try {
$fun::run($package);
} catch (Exception $e) {
if ($fun::catchException() == true) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
}
}
}
}
}