Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 18, 2023
Merged

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

merged 1 commit into from Oct 18, 2023

Conversation

trevorgehman
Copy link
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
Copy link
Member

crynobone commented Oct 17, 2023

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
Copy link

woodspire commented Oct 17, 2023

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
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
21 checks passed
@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.

None yet

4 participants