2013-08-26 57 views
20

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

在普通的PHP,你可以用exit(123)做到这一点。但我猜Symfony2有一个面向对象的方式。是对的吗?尽管我找不到文档上的任何内容。

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

回答

39

在基础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; 

所以,简单地从你的​​函数返回的退出代码。只要它是一个数字值,您的控制台命令将退出该代码。

+0

完美,谢谢! – ChocoDeveloper

相关问题