Yii2: Add silent mode for console command

Created on 18 Nov 2016  路  21Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

I want to run console command or use it class (as migration) without output information in console.

When I use the command in the console it is not critical. But if I use the command class in my project and don't want it to be displayed messages to the console. I can't do it.

An example: yii migrate --silent

under discussion enhancement

Most helpful comment

Use

yii migrate --interactive=0

All 21 comments

Might be implemented. As a workaround, you can run yii migrate > /dev/null

Use

yii migrate --interactive=0

yii migrate --interactive=0 > /dev/null

Yes that is correct.

@klimov-paul, @SilverFire Sorry, I poorly explained what I want.
If I want use:

$migrateController = new MigrateController('migrate', $app);
$migrateController->migrationPath = $migrationPath;
$migrateController->interactive = false;
//$migrateController->silent = true;  <-- I suggest it, I think this is good improvement
$migrateController->runAction($command);

Haw can I redirect output?

@klimov-paul thanks you

I found to decide it is stream_filter_register function

The more generic way to implement this would be to allow redirection of output by overriding console\Controller::stdout() and stderr() methods. We could add configuration params to Controller to allow setting the streams to use for that. To pipe all output to nowhere you can set it to use fopen('php://memory').

@cebe if need I can implement it? Because I decided my problem, but maybe it will be interesting to someone else.

Relates #12862

@klimov-paul #12862 it issue only Migration, but my issue about all console commands.

if need I can implement it?

sure, if you want to work on it, pull request is welcome :)

12862 it issue only Migration, but my issue about all console commands.

it is only related, not duplicate :)

I can see it a use case. When i run the command myself i wanna see the output. But for cronjob i dont want it

@TerraSkye for a cronjob you can just pipe the output to /dev/null, so no change is needed for that use case.

The original problem here is the fact executing a migration pushes many people to instantiate the console controller.
Ideally, the migrations should be runnable via some migration service / component that is used by the controller.
That way if you want to run migrations from the web (I think that's what you're doing) you can just talk to the component / service instead of instantiating the controller and running an action on it.

@SamMousa the problem you've mentioned is valid but it's a separate problem which serves as an example. It could be any command, not necessary migrate.

Yes, but logic shouldn't be in the controller layer right?

Ideally yes.

So that part we could fix for migrations (since it is part of the core).
I don't think the argument for the silent option should be to use it via another (web) controller.
On the other hand if the argument is use cases like cron which send mail if there's output; then that makes sense 馃憤

Won't be implemented in 2.0.

Was this page helpful?
0 / 5 - 0 ratings