Skip to content

[10.x] Add runningConsoleCommand(...$commands) method - #48751

Merged
taylorotwell merged 1 commit into
laravel:10.xfrom
trevorgehman:trevor/add-command
Oct 18, 2023
Merged

taylorotwell merged 1 commit into
laravel:10.xfrom
trevorgehman:trevor/add-command

Conversation

@trevorgehman

Copy link
Copy Markdown
Contributor

What

This PR adds a method runningConsoleCommand(...$commands) which returns true if the application is running a given artisan console command.

Note: This is the same as #48731 except I decided not to update the interface so it can be released in 10.x.

How to Use

The method can be used like so:

app()->runningConsoleCommand('db:seed'); // True if running db:seed
app()->runningConsoleCommand('db:seed', 'migrate:fresh'); // True if running db:seed OR migrate:fresh
app()->runningConsoleCommand(['db:seed', 'migrate:fresh']); // Also accepts arrays

Why

In our local environment we enable things like PreventLazyLoading and PreventSilentlyDiscardingAttributes. We ran into an issue where our Seeders were throwing these exceptions, and wanted a way to disable them when seeding the database. But there wasn't an easy way to determine if the db:seed or the migrate:fresh commands were being run.

Methods with similar purposes are app()->runningInConsole() and app()->runningUnitTests().

Inspiration

This code is based on a similar method already present in laravel/telescope:

    /**
     * Determine if the application is running an approved command.
     *
     * @param  \Illuminate\Foundation\Application  $app
     * @return bool
     */
    protected static function runningApprovedArtisanCommand($app)
    {
        return $app->runningInConsole() && ! in_array(
            $_SERVER['argv'][1] ?? null,
            array_merge([
                // 'migrate',
                'migrate:rollback',
                'migrate:fresh',
                // 'migrate:refresh',
                'migrate:reset',
                'migrate:install',
                'package:discover',
                'queue:listen',
                'queue:work',
                'horizon',
                'horizon:work',
                'horizon:supervisor',
            ], config('telescope.ignoreCommands', []), config('telescope.ignore_commands', []))
        );
    }

@crynobone

crynobone commented Oct 17, 2023

Copy link
Copy Markdown
Member

This would only work if the command is called directly via cli and not via Artisan::call() or Artisan::queue().

You should be able to disable using the following:

use Illuminate\Console\Events\CommandStarting;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Support\Facades\Event;

Event::listen(function (CommandStarting $event) {
    if (in_array($event->command, ['db:seed', 'migrate'])) {
        Model::preventLazyLoading(false);
    }
});

Event::listen(function (CommandFinished $event) {
    Model::preventLazyLoading($default);
});

@woodspire

woodspire commented Oct 17, 2023

Copy link
Copy Markdown

The method name should be: areConsoleCommandsRunning

When I first read the current name (runningConsoleCommand), I taught this method would be used to run commands.

@trevorgehman

Copy link
Copy Markdown
Contributor Author

This would only work if the command is called directly via cli and not via Artisan::call() or Artisan::queue().

Yes that's true. It's a limitation. IMO it's still useful.

The method name should be: areConsoleCommandsRunning

When I first read the current name (runningConsoleCommand), I taught this method would be used to run commands.

I tried to follow the existing naming convention for similar methods:

  • runningInConsole()
  • runningUnitTests()

@taylorotwell
taylorotwell merged commit 2a4c30c into laravel:10.x Oct 18, 2023
@trevorgehman
trevorgehman deleted the trevor/add-command branch October 18, 2023 19:49
timacdonald pushed a commit to timacdonald/framework that referenced this pull request Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants