1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Traits\EnvVarsObjectTrait |
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
|
|
|
/** |
24
|
|
|
* Trait EnvVarsObjectTrait |
25
|
|
|
* |
26
|
|
|
* @author Johann Zelger <[email protected]> |
27
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
29
|
|
|
* @link https://github.com/appserver-io/server |
30
|
|
|
* @link http://www.appserver.io |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
trait EnvVarsObjectTrait |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Sets a value to specific env var |
36
|
|
|
* |
37
|
|
|
* @param string $envVar The env var to set |
38
|
|
|
* @param string $value The value to env var |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function setEnvVar($envVar, $value) |
43
|
|
|
{ |
44
|
|
|
$this->envVars->add($envVar, $value); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Unsets a specific env var |
49
|
|
|
* |
50
|
|
|
* @param string $envVar The env var to unset |
51
|
|
|
* |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
public function unsetEnvVar($envVar) |
55
|
|
|
{ |
56
|
|
|
$this->envVars->remove($envVar); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns a value for specific env var |
61
|
|
|
* |
62
|
|
|
* @param string $envVar The env var to get value for |
63
|
|
|
* |
64
|
|
|
* @throws \AppserverIo\Server\Exceptions\ServerException |
65
|
|
|
* |
66
|
|
|
* @return mixed The value to given env var |
67
|
|
|
*/ |
68
|
|
|
public function getEnvVar($envVar) |
69
|
|
|
{ |
70
|
|
|
// get from hash map |
71
|
|
|
return $this->envVars->get($envVar); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns all the env vars as array key value pair format |
76
|
|
|
* |
77
|
|
|
* @return array The env vars as array |
78
|
|
|
*/ |
79
|
|
|
public function getEnvVars() |
80
|
|
|
{ |
81
|
|
|
return $this->envVars->toIndexedArray(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Checks if value exists for given env var |
86
|
|
|
* |
87
|
|
|
* @param string $envVar The env var to check |
88
|
|
|
* |
89
|
|
|
* @return boolean Weather it has envVar (true) or not (false) |
90
|
|
|
*/ |
91
|
|
|
public function hasEnvVar($envVar) |
92
|
|
|
{ |
93
|
|
|
// check if var is set |
94
|
|
|
return $this->envVars->exists($envVar); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Clears the env vars storage |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function clearEnvVars() |
103
|
|
|
{ |
104
|
|
|
$this->envVars->clear(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.