1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Traits\EnvVarsArrayTrait |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Johann Zelger <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/server |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Server\Traits; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Server\Exceptions\ServerException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Trait EnvVarsArrayTrait |
27
|
|
|
* |
28
|
|
|
* @author Johann Zelger <[email protected]> |
29
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/appserver-io/server |
32
|
|
|
* @link http://www.appserver.io |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
trait EnvVarsArrayTrait |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Sets a value to specific env var |
38
|
|
|
* |
39
|
|
|
* @param string $envVar The env var to set |
40
|
|
|
* @param string $value The value to env var |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function setEnvVar($envVar, $value) |
45
|
|
|
{ |
46
|
|
|
if (!is_null($value)) { |
47
|
|
|
$this->envVars[$envVar] = $value; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Unsets a specific env var |
53
|
|
|
* |
54
|
|
|
* @param string $envVar The env var to unset |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function unsetEnvVar($envVar) |
59
|
|
|
{ |
60
|
|
|
if (isset($this->envVars[$envVar])) { |
61
|
|
|
unset($this->envVars[$envVar]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a value for specific env var |
67
|
|
|
* |
68
|
|
|
* @param string $envVar The env var to get value for |
69
|
|
|
* |
70
|
|
|
* @throws \AppserverIo\Server\Exceptions\ServerException |
71
|
|
|
* |
72
|
|
|
* @return mixed The value to given env var |
73
|
|
|
*/ |
74
|
|
|
public function getEnvVar($envVar) |
75
|
|
|
{ |
76
|
|
|
// check if var is set |
77
|
|
|
if (isset($this->envVars[$envVar])) { |
78
|
|
|
// return vars value |
79
|
|
|
return $this->envVars[$envVar]; |
80
|
|
|
} |
81
|
|
|
// throw exception |
82
|
|
|
throw new ServerException("Env var '$envVar'' does not exist.", 500); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns all the env vars as array key value pair format |
87
|
|
|
* |
88
|
|
|
* @return array The env vars as array |
89
|
|
|
*/ |
90
|
|
|
public function getEnvVars() |
91
|
|
|
{ |
92
|
|
|
return $this->envVars; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Checks if value exists for given env var |
97
|
|
|
* |
98
|
|
|
* @param string $envVar The env var to check |
99
|
|
|
* |
100
|
|
|
* @return boolean Weather it has envVar (true) or not (false) |
101
|
|
|
*/ |
102
|
|
|
public function hasEnvVar($envVar) |
103
|
|
|
{ |
104
|
|
|
// check if var is set |
105
|
|
|
if (!isset($this->envVars[$envVar])) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Clears the env vars storage |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function clearEnvVars() |
118
|
|
|
{ |
119
|
|
|
foreach ($this->envVars as $key => $value) { |
120
|
|
|
unset($this->envVars[$key]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.