TroodoN-Mike
TroodoN-Mike

Reputation: 16175

symfony2 get command line executed

I am creating a console command in symfony2. I need to log an executed line i run. How to get that line?

So if i run:

php app/console my-command fileName.txt --myoption=100

I want to get value "php app/console my-command fileName.txt --myoption=100"

Thanks for any help

Upvotes: 1

Views: 2600

Answers (4)

Florent Roques
Florent Roques

Reputation: 2662

You can use Symfony Request object.

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();
foreach ($request->server->get('argv') as $arg) {
 echo $arg;
}

Upvotes: 0

Gabb1995
Gabb1995

Reputation: 1037

Better solution would be to use $input->__toString()

protected function execute(InputInterface $input, OutputInterface $output)
{
    $this->info(\sprintf('Executing %s', $input->__toString()));
}

Upvotes: 3

Cyprian
Cyprian

Reputation: 11374

If you look at ArgvInput class you might notice that argv values are holded in private property without any getter. Basically it means you can't access to this information. Of course you may use $_SERVER['argv'] directly but this is not very pretty solution.

So, it seems that there is no "clean" or "easy" way to achieve exactly what you want.

However you have access to every information that you need.

$this->getName(); // gets name of command (eg. "my-comand")
$input->getArguments(); // gets all arguments (eg. "fileName.txt")
$input->getOptions(); // get all options (eg. --myoption => 100)

You can put it together to one string. But this is after validation, so if you need log bad commands as well (I mean with wrong parameters and so on) this doesn't pass exam.

Upvotes: 1

Thomas Kelley
Thomas Kelley

Reputation: 10302

I'm interpreting the question as: Within the Command code itself, you want to determine what was written on the command line in order to end up executing that Symfony Command?

If that's correct, then I don't think it's possible to get it exactly. However, you should be able to get [almost?] the same effect by doing this:

implode(" ", $_SERVER['argv'])

Example:

class SomeCommand extends ContainerAwareCommand
{
    protected function execute(InputInterface $input, OutputInterface $output)
    {       
        $output->writeln( implode(" ", $_SERVER['argv']) );
    }
}

Upvotes: 4

Related Questions