1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 22:54:04 +00:00

Add support for arguments and options

This commit is contained in:
Samuel Štancl 2019-08-09 18:36:46 +02:00
parent cfaa6463ee
commit 3306b8e6de
3 changed files with 27 additions and 8 deletions

View file

@ -19,7 +19,10 @@ class Run extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'tenants:run {commandname} {--tenants=} {args*}'; protected $signature = "tenants:run {commandname : The command's name.}
{--tenants= : The tenant(s) to run the command for. Default: all}
{--argument=* : The arguments to pass to the command. Default: none}
{--option=* : The options to pass to the command. Default: none}";
/** /**
* Execute the console command. * Execute the console command.
@ -36,10 +39,24 @@ class Run extends Command
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})"); $this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
tenancy()->init($tenant['domain']); tenancy()->init($tenant['domain']);
$callback = function ($prefix = '') {
return function ($arguments, $argument) use ($prefix) {
[$key, $value] = explode('=', $argument);
$arguments[$prefix . $key] = $value;
return $arguments;
};
};
// Turns ['foo=bar', 'abc=xyz'] into ['foo' => 'bar', 'abc' => 'xyz']
$arguments = array_reduce($this->option('argument'), $callback(), []);
// Turns ['foo=bar', 'abc=xyz'] into ['--foo' => 'bar', '--abc' => 'xyz']
$options = array_reduce($this->option('option'), $callback('--'), []);
// Run command // Run command
Artisan::call($this->argument('commandname'), [ $this->call($this->argument('commandname'), array_merge($arguments, $options));
'args' => $this->argument('args') // todo find a better way to pass args
]);
tenancy()->end(); tenancy()->end();
}); });

View file

@ -108,8 +108,9 @@ class CommandsTest extends TestCase
Artisan::call('tenants:migrate', ['--tenants' => $uuid]); Artisan::call('tenants:migrate', ['--tenants' => $uuid]);
$this->artisan("tenants:run foo --tenants=$uuid a b") $this->artisan("tenants:run foo --tenants=$uuid --argument='a=foo' --option='b=bar' --option='c=xyz'")
->expectsOutput("User's name is Test command") ->expectsOutput("User's name is Test command")
->expectsOutput('a;b'); ->expectsOutput('foo')
->expectsOutput('xyz');
} }
} }

View file

@ -11,7 +11,7 @@ class ExampleCommand extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'foo {args*}'; protected $signature = 'foo {a} {--b=} {--c=}';
/** /**
* Execute the console command. * Execute the console command.
@ -28,7 +28,8 @@ class ExampleCommand extends Command
]); ]);
$this->line("User's name is " . User::find(999)->name); $this->line("User's name is " . User::find(999)->name);
$this->line(implode(';', $this->argument('args'))); $this->line($this->argument('a'));
$this->line($this->option('c'));
} }
} }