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 #48731

Closed
wants to merge 3 commits into from
Closed

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

wants to merge 3 commits into from

Conversation

trevorgehman
Copy link
Contributor

@trevorgehman trevorgehman commented Oct 14, 2023

What

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

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', []))
        );
    }

@trevorgehman trevorgehman changed the title Trevor/add running command method Add runningConsoleCommand(...$commands) method Oct 14, 2023
@trevorgehman trevorgehman changed the title Add runningConsoleCommand(...$commands) method [10.x] Add runningConsoleCommand(...$commands) method Oct 14, 2023
@trevorgehman trevorgehman changed the base branch from 10.x to master October 15, 2023 20:02
@trevorgehman trevorgehman changed the base branch from master to 10.x October 15, 2023 20:04
@trevorgehman trevorgehman closed this by deleting the head repository Oct 15, 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.

1 participant