'How to get laravel command arguments outside command, e.g. in service provider?

I can get arguments in command with this code:

$this->argument();

But how to get arguments outside ?

If I look at source of argument() function I see :

public function argument($key = null)
{
    if (is_null($key)) {
        return $this->input->getArguments();
    }

    return $this->input->getArgument($key);
}

I want to detect when command "php artisan migrate:refresh --seed" is running because I want some part of code in models run at localhost enviroment but not in localhost enviroment during seeding...



Solution 1:[1]

I has the same problem building a Laravel SAAS app on AWS, in base to this project I modify my ServiceProvider for this:

<?php

namespace App\Providers;

use Illuminate\Console\Events\ArtisanStarting;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventDispatcher;

class MultitenantServiceProvider extends ServiceProvider{

protected $consoleDispatcher = false;
protected $commands_with_tenant = [
    'migrate', 'migrate:refresh', 'migrate:install', 'migrate:reset', 'migrate:rollback', 
    'migrate:status', 'passport:client', 'passport:install', 'passport:keys'
];

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot(){
    if( $this->app->runningInConsole() ){
        $this->registerTenantOption();
        $this->verifyTenantOption();
    }
    // Multitenant re-configure in case of HTTP request
}

/**
 * Register any application services.
 *
 * @return void
 */
public function register(){
    $this->app->singleton('multitenant', function ($app){
        // Register your Multitenant
    });
}

protected function registerTenantOption(){
    $this->app['events']->listen(ArtisanStarting::class, function($event){
        $definition = $event->artisan->getDefinition();
        $definition->addOption(
            new InputOption('--tenant', null, InputOption::VALUE_OPTIONAL, 'The tenant subdomain the command should be run for. Use * or all for every tenant.')
        );
        $event->artisan->setDefinition($definition);
        $event->artisan->setDispatcher($this->getConsoleDispatcher());
    });
}

protected function verifyTenantOption(){
    $this->getConsoleDispatcher()->addListener(ConsoleEvents::COMMAND, function(ConsoleCommandEvent $event){
        if( in_array($event->getCommand()->getName() , $this->commands_with_tenant) ){
            $tenant = $event->getInput()->getParameterOption('--tenant', null);
            if (!is_null($tenant)){
                if ($tenant == 'all'){
                    // Do something with 'all'
                }
                else{
                    // Do something with $tenant
                }
            }
            else{
                $event->getOutput('<error>This command need that specified a tenant client</error>');
                $event->disableCommand();
            }
        }
    });
}

protected function getConsoleDispatcher(){
    if (!$this->consoleDispatcher){
        $this->consoleDispatcher = app(EventDispatcher::class);
    }
    return $this->consoleDispatcher;
}

In this class, there is an array with the commands that are needed to verify and use a multitenant config.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sᴀᴍ Onᴇᴌᴀ