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