如何在Symfony2命令中设置退出状态代码?

Cho*_*per 30 command-line symfony

在Symfony2命令中设置退出状态代码的正确方法是什么?

在普通的PHP中,你可以这样做exit(123).但我猜Symfony2有一个OOP方式.是对的吗?虽然我在文档上找不到任何东西.

我需要这个主要是因为我希望能够在Linux中做这样的事情: app/console my:command || { echo "Something went wrong, I'm gonna call handle_disaster now"; handle_disaster; }

Emi*_*aos 52

在基Command类中:

    if ($this->code) {
        $statusCode = call_user_func($this->code, $input, $output);
    } else {
        $statusCode = $this->execute($input, $output);
    }

    return is_numeric($statusCode) ? (int) $statusCode : 0;
Run Code Online (Sandbox Code Playgroud)

所以只需从execute()函数中返回退出代码即可.您的控制台命令将使用此代码退出,只要它是一个数值.