Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Symfony\Console for an interactive php cli application

I want to develop an interactive command based php application, so I did some research and bumped into the Symfony\Console component. This is great, because it lets met run

php script.php command --option

However, the script then runs the command and closes, and I want to be able to run another command. So basically, something like

php script.php

Which then listens on php://stdin for commands, structured as

command --option

then runs the command and starts listening for new commands. When a specific "exit" command is run, the script should terminate.

I'm relatively new to Symfony and the Console component, so any thoughts on how to implement this, using Symfony\Console? Because I really like the way how Symfony\Console abstracts all the command-stuff away.

like image 768
Seba Avatar asked Oct 02 '22 12:10

Seba


1 Answers

Well, you have to enclose your script.php in a cycle like that:

while true {
   <reading stdin>
   <executing command via Symfony2 component>
}

You will be able to terminate the cycle by pressing Ctrl-C

like image 171
Vladislav Rastrusny Avatar answered Oct 13 '22 10:10

Vladislav Rastrusny