如果事件处理程序在yii2中返回false,如何停止运行代码

时间:2017-08-27 09:19:40

标签: php oop events yii2 eventhandler

我试图在yii2中创建一个模块。我希望使用事件来处理模块中的某些事件,并使我的模块独立于我的核心。

现在我想要停止模块代码流,如果事件抛出甚至处理程序返回false。

看下面的代码:

function withdraw($amount, $description = null)
{
    if (!(is_integer($amount) or is_float($amount) or is_double($amount))) {
        throw new InvalidArgumentException("Amount should be in double.");
    }
    try {
        $event = new TransactionEvent();
        $event->setTransaction($transactionModel);
        $event->setType($event::TYPE_WITHDRAW);
        $this->trigger(\aminkt\userAccounting\UserAccounting::EVENT_WITHDRAW, $event);

        $this->totalWithdraw += $amount;
        if (!$this->save())
            throw new RuntimeException("Purse cant update itself in withdraw action.");

        $transaction->commit();
    } catch (\Exception $e) {
        throw $e;
    } catch (\Throwable $e) {
        throw $e;
    }
}

这是我现在的代码。我想要下面的内容知道事件处理程序正常工作,我的事务正确保存到数据库中:

if($this->trigger(\aminkt\userAccounting\UserAccounting::EVENT_WITHDRAW, event)){
   $this->totalWithdraw += $amount;
   if (!$this->save())
       throw new RuntimeException("Purse cant update itself in withdraw action.");
}else{
   throw new RuntimeException("Transaction handler not worked correctly.");
}

谢谢。

0 个答案:

没有答案