From 3306b8e6de5d9296572a7dab65df1b40cbeb4681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 9 Aug 2019 18:36:46 +0200 Subject: [PATCH] Add support for arguments and options --- src/Commands/Run.php | 25 +++++++++++++++++++++---- tests/CommandsTest.php | 5 +++-- tests/Etc/ExampleCommand.php | 5 +++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/Commands/Run.php b/src/Commands/Run.php index 68ae8604..35fb38a1 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -19,7 +19,10 @@ class Run extends Command * * @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. @@ -36,10 +39,24 @@ class Run extends Command $this->line("Tenant: {$tenant['uuid']} ({$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 - Artisan::call($this->argument('commandname'), [ - 'args' => $this->argument('args') // todo find a better way to pass args - ]); + $this->call($this->argument('commandname'), array_merge($arguments, $options)); + tenancy()->end(); }); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d63a5247..f372b084 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -108,8 +108,9 @@ class CommandsTest extends TestCase 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('a;b'); + ->expectsOutput('foo') + ->expectsOutput('xyz'); } } diff --git a/tests/Etc/ExampleCommand.php b/tests/Etc/ExampleCommand.php index faf83750..76932163 100644 --- a/tests/Etc/ExampleCommand.php +++ b/tests/Etc/ExampleCommand.php @@ -11,7 +11,7 @@ class ExampleCommand extends Command * * @var string */ - protected $signature = 'foo {args*}'; + protected $signature = 'foo {a} {--b=} {--c=}'; /** * Execute the console command. @@ -28,7 +28,8 @@ class ExampleCommand extends Command ]); $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')); } }