-
Notifications
You must be signed in to change notification settings - Fork 452
/
EnvironmentDetector.php
141 lines (128 loc) · 4.07 KB
/
EnvironmentDetector.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php namespace Concrete\Core\Foundation;
use Closure;
/**
* Class EnvironmentDetector
*
* This is a straight copy from Laravel.
* https://github.com/laravel/framework/blob/7172f42496f3f6f1f7aa3e0dc87065a582ffd0a7/src/Illuminate/Foundation/EnvironmentDetector.php
*
* @package Concrete\Core\Foundation
*/
class EnvironmentDetector {
/**
* Detect the application's current environment.
*
* @param array|string $environments
* @param array|null $consoleArgs
* @return string
*/
public function detect($environments, $consoleArgs = null)
{
if ($consoleArgs && $env = $this->detectConsoleEnvironment($environments, $consoleArgs)) {
return $env;
} elseif ($env = $this->detectClosureEnvironment($environments)) {
return $env;
} elseif ($env = $this->detectVariableEnvironment($environments)) {
return $env;
}
return $this->detectWebEnvironment($environments);
}
/**
* Set the application environment for a web request.
*
* @param array|string $environments
* @return string
*/
protected function detectWebEnvironment($environments)
{
foreach ($environments as $environment => $hosts)
{
// To determine the current environment, we'll simply iterate through the possible
// environments and look for the host that matches the host for this request we
// are currently processing here, then return back these environment's names.
foreach ((array) $hosts as $host)
{
if ($this->isMachine($host)) return $environment;
}
}
return 'production';
}
/**
* Set the application environment from command-line arguments.
*
* @param mixed $environments
* @param array $args
* @return string
*/
protected function detectConsoleEnvironment($environments, 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 head(array_slice(explode('=', $value), 1));
}
}
/**
* Set the application environment from environment variable.
*
* @param mixed $environments
* @return string
*/
protected function detectVariableEnvironment($environments)
{
if (($env = $this->getEnvironmentFromVariable()) !== false) {
return $env;
}
}
/**
* Set the application environment from the passed closure in case a closure
* was passed.
*
* @param mixed $environments
*
* @return string|null
*/
protected function detectClosureEnvironment($environments)
{
// If the given environment is just a Closure, we will defer the environment check
// to the Closure the developer has provided, which allows them to totally swap
// the webs environment detection logic with their own custom Closure's code.
if ($environments instanceof Closure) {
return call_user_func($environments);
}
}
/**
* Get the environment argument from the console.
*
* @param array $args
* @return string|null
*/
protected function getEnvironmentArgument(array $args)
{
return array_first($args, function($k, $v)
{
return starts_with($v, '--env');
});
}
/**
* Gets the environment from the CONCRETE5_ENV environment variable.
*
* @return string|bool
*/
protected function getEnvironmentFromVariable()
{
return getenv('CONCRETE5_ENV');
}
/**
* Determine if the name matches the machine name.
*
* @param string $name
* @return bool
*/
public function isMachine($name)
{
return str_is($name, gethostname());
}
}