5

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.

6
  • Why can't you "access" the OutputInterface? Commented Aug 14, 2013 at 16:43
  • Because I only can manipulate a Class that is called in the Command. Unfortunately I don't get passed the OutputInterface. Commented Aug 14, 2013 at 16:47
  • 1
    If no OutputInterface were given, PHP would throw an error. Are you using Symfony\Component\Console\Application to run them ? Or is it a custom caller ? Commented Aug 14, 2013 at 16:49
  • @Touki You misunderstand me. I use a given Command. Actually I want to print some Debug message in my Fixtures github.com/doctrine/DoctrineFixturesBundle. I am only able to create the Fixtures Class and there is no OutputInterface available. Commented Aug 14, 2013 at 16:52
  • What's wrong with echo while debugging ? If you want to run the command and output console messages you can use $command = new MyCommand; $command->run(new ArrayInput($parameters), new ConsoleOutput()); Commented Aug 14, 2013 at 16:55

2 Answers 2

8

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 OutputInterfaces

  • NullOutput - Will result in no output at all and keep the command quiet
  • ConsoleOutput - Will result in console messages
  • StreamOutput - Will result in printing messages into a given stream

Debugging to a file

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);

Debugging in the console

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 debugging

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);
Sign up to request clarification or add additional context in comments.

Comments

1

Look at JMSAopBundle https://github.com/schmittjoh/JMSAopBundle and check out this great article http://php-and-symfony.matthiasnoback.nl/2013/07/symfony2-rich-console-command-output-using-aop/

1 Comment

This seems pretty promising as well. Maybe I will try it in the next days. Thank you for sharing this ressource. I didn't know about Aop at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.