Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roboburo's avatar

Get all console commands

Hello all()

Does anyone know how to programmatically list all custom Artisan commands of a Laravel application?

I know one can do this \Artisan::all() or this app(Kernel::class)->all(). And the list can be found in the $commands property of \App\Console\Kernel, but that property is protected and I would like to have it outside that class.

I could probably use Reflection, but I'm hoping there is another way?

0 likes
8 replies
Tray2's avatar

Why would you want to do that? To me it sounds dangerous since you can do a lot of stuff with it,

roboburo's avatar

Hi, thanks @tray2, I'm not sure why it would be more dangerous than doing \Artisan::all()?

To put it differently, I would like to have a way to have the same result as \Artisan::all() but without the default Artisan commands. That is, without migrate, migrate:*, key:generate and all the other ones which come with a default Laravel installation.

In fact, even the command signatures would be enough (the keys from \Artisan::all()).

ajpw's avatar

If all you want is the keys from Artisan::all then why not do

foreach(\Artisan::all() as $key=>$value)
{
	var_dump($key);
}

and just filter out the ones that you don't want

roboburo's avatar

Hi @ajpw, yes, I was looking for something more future-proof - not having to update the filtering whenever Laravel adds or removes a default command. (It is for a composer package.)

Tray2's avatar

The question is still why would you need to have a list of artisan commands in your app?

roboburo's avatar

Ok, I found a solution - as often, hidden in plain sight. \Artisan::all() returns the instantiated commands as values, so I can verify if they are in the App\Console\Commands namespace!

And @tray2, I'm writing this little health-check package, which logs the execution time of every application console command. Here is how:

protected function registerEventListeners(): void
{
    $this->app
        ->make('events')
        ->listen(CommandFinished::class, function(CommandFinished $event) {
            if ($event->command && $this->isApplicationCommand($event->command)) {
                $this->app->make(HealthService::class)->setTime($event->command);
            }
        });
}

protected function isApplicationCommand(string $signature): bool
{
    $commands = $this->app->make(Kernel::class)->all();
    return !empty($commands[$signature])
        && strpos(get_class($commands[$signature]), 'App\Console\Commands') === 0;
}
roboburo's avatar

Thanks for the hint @tray2. I'm in fact merely recording last execution timestamp of custom made commands (not the default ones).

Please or to participate in this conversation.