Dispatch.php 2.09 KB
<?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());
                        }
                    }
                }
            }
        }
    }
}