Jobs and the console

Permalink
Hi,

really happy with the console, and running jobs from the console. Cool stuff

I would like to add a bit more information in my jobs when i run them in the console, so using the verbose methods provided by the symphony console would sound logically.

i can easily get access to an output handle by using:
$output = new \Symfony\Component\Console\Output\ConsoleOutput\ConsoleOutput();

this allows me to do things like: $output->writeln("<info>starting</info>") to the console! Great stuff. But what i would like to do is:

if ($output->isQuiet()) { // ...}
if ($output->isVerbose()) { // ...}
if ($output->isVeryVerbose()) { // ...}
if ($output->isDebug()) { // ...}

just using ConsoleOutput() does not give me the context of the actual console.....

Anyone any idea?

Thanks

\m/

meshen
 
meshen replied on at Permalink Best Answer Reply
meshen
Hi,

created a solution for the myself:

<?php
namespace Concrete\Package\ImageGallery\Src\Helper;
use Symfony\Component\Console\Output\OutputInterface;
class Console
{
   static function Verbosity()
   {
      $args = $_SERVER['argv'];
      $result = OutputInterface::VERBOSITY_NORMAL;
      if (in_array("-q", $args, true))    $result = OutputInterface::VERBOSITY_QUIET;
      if (in_array("-quit", $args, true)) $result = OutputInterface::VERBOSITY_QUIET;      
      if (in_array("-v", $args, true))    $result = OutputInterface::VERBOSITY_VERBOSE;
      if (in_array("-vv", $args, true))   $result = OutputInterface::VERBOSITY_VERY_VERBOSE;
      if (in_array("-vvv", $args, true))  $result = OutputInterface::VERBOSITY_DEBUG;      
      return $result;


this will check for the console arguments in the args array and returns the verbosity level.

Now when you want to create a console output, do the following:

$output = new ConsoleOutput(Console::Verbosity());


This will give you all the nice things the console has to offer:

if ($output->isQuiet()) {
    // ...
}
if ($output->isVerbose()) {
    // ...
}
if ($output->isVeryVerbose()) {
    // ...
}
if ($output->isDebug()) {
    // ...
}