EnvironmentDetector.php
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Illuminate\Foundation;
use Closure;
use Illuminate\Support\Str;
class EnvironmentDetector
{
/**
* Detect the application's current environment.
*
* @param \Closure $callback
* @param array|null $consoleArgs
* @return string
*/
public function detect(Closure $callback, $consoleArgs = null)
{
if ($consoleArgs) {
return $this->detectConsoleEnvironment($callback, $consoleArgs);
}
return $this->detectWebEnvironment($callback);
}
/**
* Set the application environment for a web request.
*
* @param \Closure $callback
* @return string
*/
protected function detectWebEnvironment(Closure $callback)
{
return $callback();
}
/**
* Set the application environment from command-line arguments.
*
* @param \Closure $callback
* @param array $args
* @return string
*/
protected function detectConsoleEnvironment(Closure $callback, array $args)
{
// First we will check if an environment argument was passed via console arguments
// 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 $value;
}
return $this->detectWebEnvironment($callback);
}
/**
* Get the environment argument from the console.
*
* @param array $args
* @return string|null
*/
protected function getEnvironmentArgument(array $args)
{
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));
}
}
}
}