Skip to content

Commit 9abaa61

Browse files
committed
Allow passthrough options with single dash; test passthrough option extraction and removal
1 parent 86e557e commit 9abaa61

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

app/Commands/EnableCommand.php

+47-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Services;
77
use App\Shell\Environment;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\App;
910
use Illuminate\Support\Str;
1011
use LaravelZero\Framework\Commands\Command;
1112

@@ -26,16 +27,11 @@ public function handle(Environment $environment, Services $services): void
2627
$this->services = $services;
2728
$this->initializeCommand();
2829

29-
$services = $this->removePassthroughOptions($this->argument('serviceNames'));
30+
$services = $this->removeOptions($this->serverArguments());
31+
$passthroughOptions = $this->extractPassthroughOptions($this->serverArguments());
3032

3133
$useDefaults = $this->option('default');
3234

33-
// Extract passthrough options, if provided, passed after "-- "
34-
$passthroughOptions = [];
35-
if (in_array('--', $_SERVER['argv'])) {
36-
$passthroughOptions = array_slice($_SERVER['argv'], array_search('--', $_SERVER['argv']) + 1);
37-
}
38-
3935
if (filled($services)) {
4036
foreach ($services as $service) {
4137
$this->enable($service, $useDefaults, $passthroughOptions);
@@ -53,17 +49,55 @@ public function handle(Environment $environment, Services $services): void
5349
$this->enable($option, $useDefaults, $passthroughOptions);
5450
}
5551

52+
public function serverArguments(): array
53+
{
54+
if (App::environment() === 'testing') {
55+
$string = array_merge(['takeout', 'enable'], $this->argument('serviceNames'));
56+
57+
if ($this->option('default')) {
58+
$string[] = '--default';
59+
}
60+
61+
return $string;
62+
}
63+
64+
return $_SERVER['argv'];
65+
}
66+
67+
/**
68+
* Extract and return any passthrough options from the parameters list
69+
*
70+
* @param array $arguments
71+
* @return array
72+
*/
73+
public function extractPassthroughOptions(array $arguments): array
74+
{
75+
if (! in_array('--', $arguments)) {
76+
return [];
77+
}
78+
79+
return array_slice($arguments, array_search('--', $arguments) + 1);
80+
}
81+
5682
/**
57-
* Remove any passthrough options from the parameters list
83+
* Remove any options or passthrough options from the parameters list
5884
*
59-
* @param array $serviceNames
85+
* @param array $arguments
6086
* @return array
6187
*/
62-
protected function removePassthroughOptions(array $serviceNames): array
88+
public function removeOptions(array $arguments): array
6389
{
64-
return collect($serviceNames)->reject(function ($item) {
65-
return str_starts_with($item, '--');
66-
})->all();
90+
$arguments = collect($arguments)->reject(fn ($argument) => str_starts_with($argument, '--') && strlen($argument) > 2)->toArray();
91+
92+
$start = array_search('enable', $arguments) + 1;
93+
94+
if (in_array('--', $arguments)) {
95+
$length = array_search('--', $arguments) - $start - 1;
96+
97+
return array_slice($arguments, $start, $length);
98+
}
99+
100+
return array_slice($arguments, $start);
67101
}
68102

69103
private function selectService(): ?string

config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
|
4141
*/
4242

43-
'env' => 'development',
43+
'env' => env('APP_ENV', 'development'),
4444

4545
/*
4646
|--------------------------------------------------------------------------

phpunit.xml.dist

+3
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
<directory suffix="Test.php">./tests/Unit</directory>
2424
</testsuite>
2525
</testsuites>
26+
<php>
27+
<server name="APP_ENV" value="testing"/>
28+
</php>
2629
</phpunit>

tests/Feature/EnableCommandTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature;
44

5+
use App\Commands\EnableCommand;
56
use App\Exceptions\InvalidServiceShortnameException;
67
use App\Services;
78
use App\Services\MeiliSearch;
@@ -172,4 +173,24 @@ function it_displays_error_if_invalid_shortname_passed()
172173
$this->artisan('enable asdfasdfadsfasdfadsf')
173174
->assertExitCode(0);
174175
}
176+
177+
/** @test */
178+
function it_removes_options()
179+
{
180+
$cli = explode(' ', "./takeout enable meilisearch postgresql mysql --default -- -e 'abc' --other-flag");
181+
182+
$command = new EnableCommand;
183+
184+
$this->assertEquals(['meilisearch', 'postgresql', 'mysql'], $command->removeOptions($cli));
185+
}
186+
187+
/** @test */
188+
function it_extracts_passthrough_options()
189+
{
190+
$cli = explode(' ', "./takeout enable meilisearch postgresql mysql --default -- -e 'abc' --other-flag");
191+
192+
$command = new EnableCommand;
193+
194+
$this->assertEquals(['-e', "'abc'", '--other-flag'], $command->extractPassthroughOptions($cli));
195+
}
175196
}

0 commit comments

Comments
 (0)