11

I want to run a method when every single console command is run, How do I create a listener for all console commands in lumen?

I tried by creating a listener for ConsoleCommandEvent, this event is fired in \Symfony\Component\Console\Application::doRunCommand, but event dispatcher has not been attached (read the comment above the method: If an event dispatcher has been attached to the application, events are also dispatched during the life-cycle of the command.)

Update Using this event: Illuminate\Console\Events\ArtisanStarting might help to do the job, but it is a different event. By doing so any time that you run php artisan your code will be executed whether you run an actual command or not.

Mohammad Mehdi Habibi
  • 1,601
  • 1
  • 14
  • 30

2 Answers2

1

In Laravel you can listen to the CommandStarting event which is also available in illuminate/console.

Olivenbaum
  • 971
  • 1
  • 6
  • 17
0

If you only need to execute some method when each command were executed, you can listen this class.

Illuminate\Console\Events\ArtisanStarting

But i don't know how to get name of every command inside listener.

mcklayin
  • 1,330
  • 10
  • 17
  • I already found this event which works well. But the problem is that as it says, it is fired when artisan is starting! so any time that you run `php artisan` it fires the event, even if you just want to see `php artisan --help`. So, I will try to find a way to attach dispatcher. btw, a lot better than your last solution :) – Mohammad Mehdi Habibi Sep 26 '16 at 14:53
  • @Mohammad Mehdi Habibi write here, if you found smth. I think you need to check if parameter sent to `php artisan` and don't check options like `--help` – mcklayin Sep 26 '16 at 14:58
  • Obviously you can get the parameters and blah blah to check if it's an actual command or not and solve the problem, but it's not that much nice, unless we have to. – Mohammad Mehdi Habibi Sep 26 '16 at 15:02