I have read the documentation and tried several examples, but can't seem to get the symfony process component to allow me to answer multiple questions as they arise. I know the answers before hand and would love to just feed them in as a string.
The command I'm wanting to run in a process is something like this:
$ introduce
$ > What is your name?
$ Michael
$ > How old are you?
$ 36
$ > Are you having a good day?
$ Yes
$ Doing lots of stuff now.
$ Info recorded. Thank you!
A stupidly simple example. The command asks three questions and expects an answer to each question. Then (my real command) begins a long process.
I have tried variations of
$process = new Process('introduce', MY_DIR);
$process->setTimeout(null);
$inputs = "Michael
36
Yes
";
$process->setInput($inputs);
$process->enableOutput();
$process->start();
and
$process = new Process('introduce', MY_DIR);
$process->setTimeout(null);
$inputs = new InputStream();
$inputs->write("Michael");
$inputs->write("36");
$inputs->write("Yes");
$process->setInput($inputs);
$process->enableOutput();
$process->start();
$inputs->close()
and
$process = new Process('introduce', MY_DIR);
$process->setTimeout(null);
$inputs = new InputStream();
$process->setInput($inputs);
$process->enableOutput();
$process->start();
$inputs->write("Michael");
$inputs->write("36");
$inputs->write("Yes");
$inputs->close();
And then, to wait for it to finish:
$running = true;
while($running) {
if (!$process->isRunning()) {
if (!$process->isSuccessful()) {
throw new Exception($process->getErrorOutput());
}
$running = false;
}
I know I can use ->wait()
, but in my real application, I'm starting multiple instances in parallel and can't let one block before the others finish. So, I add processes to an array and unset them as they finish. When the array is empty, I break out of the loops and move on with the program.
So, what am I doing wrong? How can I have a predetermined input sent to a process so it can answer successive questions and then let the process continue?