Dispatch.php
2.09 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
<?php
/**
* Created by PhpStorm.
* User: ziy
* Date: 14-7-22
* Time: 下午8:36
*/
class YHMCart_Orders_Dispatch
{
/**
* 添加到订单时执行
* @param $package
* @param string $dispatchType
*/
protected function createPreDispatch(YHMCart_Hook_Orders $package, $dispatchType = 'ordinary')
{
$this->_dispatch($package, 'create_preDispatch', $dispatchType);
}
/**
* 添加到订单后执行
* @param $package
* @param string $dispatchType
*/
protected function createEndDispatch(YHMCart_Hook_Orders $package, $dispatchType = 'ordinary')
{
$this->_dispatch($package, 'create_endDispatch', $dispatchType);
}
/**
* 处理钩子
* @param array $package
* @param $dispatchName
* @param string $dispatchType
* @throws Exception
*/
protected function _dispatch(YHMCart_Hook_Orders $package, $dispatchName, $dispatchType = 'ordinary')
{
#默认类型订单调度
$defaultDispatch = array();
if (!empty(YHMCart_Hook_OrdersConfigs::$defaultDispatch[$dispatchName])) {
$defaultDispatch = YHMCart_Hook_OrdersConfigs::$defaultDispatch[$dispatchName];
}
#其他类型订单调度
$dispatchTypeArray = array();
$dispatchTypeFun = $dispatchType . 'Dispatch';
if (!empty(YHMCart_Hook_OrdersConfigs::$$dispatchTypeFun)) {
$dispatchTypeProperty = YHMCart_Hook_OrdersConfigs::$$dispatchTypeFun;
$dispatchTypeArray = $dispatchTypeProperty[$dispatchName];
}
$_fun = $dispatchTypeArray + $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());
}
}
}
}
}
}
}