Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Allow console environment argument to be separated with a space #28869

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Illuminate/Foundation/EnvironmentDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Illuminate\Foundation;

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class EnvironmentDetector
Expand Down Expand Up @@ -48,7 +47,7 @@ protected function detectConsoleEnvironment(Closure $callback, array $args)
// and if it was that automatically overrides as the environment. Otherwise, we
// will check the environment as a "web" request like a typical HTTP request.
if (! is_null($value = $this->getEnvironmentArgument($args))) {
return head(array_slice(explode('=', $value), 1));
return $value;
}

return $this->detectWebEnvironment($callback);
Expand All @@ -62,8 +61,14 @@ protected function detectConsoleEnvironment(Closure $callback, array $args)
*/
protected function getEnvironmentArgument(array $args)
{
return Arr::first($args, function ($value) {
return Str::startsWith($value, '--env');
});
foreach ($args as $i => $value) {
if ($value === '--env') {
return $args[$i + 1] ?? null;
}

if (Str::startsWith($value, '--env')) {
return head(array_slice(explode('=', $value), 1));
}
}
}
}
20 changes: 20 additions & 0 deletions tests/Foundation/FoundationEnvironmentDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,24 @@ public function testConsoleEnvironmentDetection()
}, ['--env=local']);
$this->assertEquals('local', $result);
}

public function testConsoleEnvironmentDetectionSeparatedWithSpace()
{
$env = new EnvironmentDetector;

$result = $env->detect(function () {
return 'foobar';
}, ['--env', 'local']);
$this->assertEquals('local', $result);
}

public function testConsoleEnvironmentDetectionWithNoValue()
{
$env = new EnvironmentDetector;

$result = $env->detect(function () {
return 'foobar';
}, ['--env']);
$this->assertEquals('foobar', $result);
}
}