如何在命令之外获得laravel命令参数,例如在服务提供商?

时间:2017-01-24 14:00:39

标签: php laravel

我可以使用以下代码获取命令参数:

$this->argument();

但如何在外面争论?

如果我查看argument()函数的来源,我看到:

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

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

我想检测命令" php artisan何时迁移:refresh --seed"正在运行,因为我希望模型中的某些部分代码在localhost环境中运行,但在播种期间不在localhost环境中运行...

2 个答案:

答案 0 :(得分:2)

laravel如何获取命令参数的机制非常复杂。我可以通过\ App :: runningInConsole()检测app是否在控制台中运行,但是没有可以获取参数的函数,例如:

if(\App::runningInConsole()){
    $args = \App::getConsoleArguments();
}

但$ _SERVER [' argv']可以在这里使用,当" php artisan migrate:refresh --seed"在$ _SERVER中运行[' argv']是这个数组:

Array
(
    [0] => artisan
    [1] => migrate:refresh
    [2] => --seed
)

所以我可以使用这段代码:

if( ! empty($_SERVER['argv'][2] )  &&  $_SERVER['argv'][2] == '--seed'){
    //
}

答案 1 :(得分:0)

我在AWS上构建一个Laravel SAAS应用程序存在同样的问题,基于this project我为此修改了我的ServiceProvider:

<?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;
}

在这个类中,有一个数组,其中包含验证和使用多租户配置所需的命令。