1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI; |
6
|
|
|
|
7
|
|
|
use GraphQLAPI\GraphQLAPI\Config\PluginConfigurationHelpers; |
8
|
|
|
|
9
|
|
|
class PluginEnvironment |
10
|
|
|
{ |
11
|
|
|
public const PLUGIN_ENVIRONMENT = 'PLUGIN_ENVIRONMENT'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The plugins runs in PROD |
15
|
|
|
*/ |
16
|
|
|
public const PLUGIN_ENVIRONMENT_PROD = 'production'; |
17
|
|
|
/** |
18
|
|
|
* The plugins runs in DEV |
19
|
|
|
*/ |
20
|
|
|
public const PLUGIN_ENVIRONMENT_DEV = 'development'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Return a value for a variable, checking if it is defined in the environment |
24
|
|
|
* first, and in the wp-config.php second |
25
|
|
|
* |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
protected static function getValueFromEnvironmentOrWPConfig(string $envVariable) |
29
|
|
|
{ |
30
|
|
|
if (getenv($envVariable) !== false) { |
31
|
|
|
return getenv($envVariable); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (PluginConfigurationHelpers::isWPConfigConstantDefined($envVariable)) { |
35
|
|
|
return PluginConfigurationHelpers::getWPConfigConstantValue($envVariable); |
36
|
|
|
}; |
37
|
|
|
|
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The label to show when the value is empty |
43
|
|
|
*/ |
44
|
|
|
public static function getPluginEnvironment(): string |
45
|
|
|
{ |
46
|
|
|
$environments = [ |
47
|
|
|
self::PLUGIN_ENVIRONMENT_PROD, |
48
|
|
|
self::PLUGIN_ENVIRONMENT_DEV, |
49
|
|
|
]; |
50
|
|
|
$value = self::getValueFromEnvironmentOrWPConfig(self::PLUGIN_ENVIRONMENT); |
51
|
|
|
if (!is_null($value) && in_array($value, $environments)) { |
52
|
|
|
return $value; |
53
|
|
|
} |
54
|
|
|
// Default value |
55
|
|
|
return self::PLUGIN_ENVIRONMENT_PROD; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function isPluginEnvironmentProd(): bool |
59
|
|
|
{ |
60
|
|
|
return self::getPluginEnvironment() == self::PLUGIN_ENVIRONMENT_PROD; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function isPluginEnvironmentDev(): bool |
64
|
|
|
{ |
65
|
|
|
return self::getPluginEnvironment() == self::PLUGIN_ENVIRONMENT_DEV; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|