Skip to content

Commit

Permalink
Merge pull request #28869 from ThibaudDauce/console-environment-separ…
Browse files Browse the repository at this point in the history
…ated-with-space

[5.8] Allow console environment argument to be separated with a space
  • Loading branch information
taylorotwell authored Jun 17, 2019
2 parents 0ab884f + ac0c9b2 commit 85137e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
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);
}
}

0 comments on commit 85137e0

Please sign in to comment.