Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the exit status code in a Symfony2 command?

What's the proper way for setting the exit status code in a Symfony2 command?

In plain php you can do this with exit(123). But I guess Symfony2 has an OOP way. Is that right? I couldn't find anything on the docs though.

I need this mostly because I want to be able to do something like this in Linux: app/console my:command || { echo "Something went wrong, I'm gonna call handle_disaster now"; handle_disaster; }

like image 327
ChocoDeveloper Avatar asked Aug 26 '13 03:08

ChocoDeveloper


1 Answers

In the base Command class:

    if ($this->code) {         $statusCode = call_user_func($this->code, $input, $output);     } else {         $statusCode = $this->execute($input, $output);     }      return is_numeric($statusCode) ? (int) $statusCode : 0; 

So simply return the exit code from your execute() function. Your console command will exit with this code as long as it is a numeric value.

like image 165
Emii Khaos Avatar answered Sep 29 '22 09:09

Emii Khaos