I am trying to print some Information to the Console in a Symfony Console Command. Regularly you would do something like this:
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
if ($name) {
$text = 'Hello '.$name;
} else {
$text = 'Hello';
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
For the full Code of the Example - Symfony Documentation
Unfortunately I can't access the OutputInterface
. Is it possible to print a Message to the Console?
Unfortunately I can't pass the OutputInterface
to the Class where I want to print some Output.
Understanding the matter of ponctual debugging, you can always print debug messages with echo
or var_dump
If you plan to use a command without Symfony's application with global debug messages, here's a way to do this.
Symfony offers 3 different OutputInterface
s
Doing such, whenever you call $output->writeln()
in your command, it will write a new line in /path/to/debug/file.log
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Acme\FooBundle\Command\MyCommand;
$params = array();
$input = new ArrayInput($params);
$file = '/path/to/debug/file.log';
$handle = fopen($file, 'w+');
$output = new StreamOutput($handle);
$command = new MyCommand;
$command->run($input, $output);
fclose($handle);
It is quietly the same process, except that you use ConsoleOutput
instead
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Acme\FooBundle\Command\MyCommand;
$params = array();
$input = new ArrayInput($params);
$output = new ConsoleOutput();
$command = new MyCommand;
$command->run($input, $output);
No message will be printed
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Acme\FooBundle\Command\MyCommand;
$params = array();
$input = new ArrayInput($params);
$output = new NullOutput();
$command = new MyCommand;
$command->run($input, $output);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With