1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JiraRestApi\Configuration; |
4
|
|
|
|
5
|
|
|
use JiraRestApi\JiraException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DotEnvConfiguration. |
9
|
|
|
*/ |
10
|
|
|
class DotEnvConfiguration extends AbstractConfiguration |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $path |
14
|
|
|
* |
15
|
|
|
* @throws JiraException |
16
|
|
|
*/ |
17
|
|
|
public function __construct($path = '.') |
18
|
|
|
{ |
19
|
|
|
$this->loadDotEnv($path); |
20
|
|
|
|
21
|
|
|
$this->jiraHost = $this->env('JIRA_HOST'); |
|
|
|
|
22
|
|
|
$this->jiraUser = $this->env('JIRA_USER'); |
|
|
|
|
23
|
|
|
$this->jiraPassword = $this->env('JIRA_PASS'); |
|
|
|
|
24
|
|
|
$this->oauthAccessToken = $this->env('OAUTH_ACCESS_TOKEN'); |
|
|
|
|
25
|
|
|
$this->cookieAuthEnabled = $this->env('COOKIE_AUTH_ENABLED', false); |
|
|
|
|
26
|
|
|
$this->cookieFile = $this->env('COOKIE_FILE', 'jira-cookie.txt'); |
|
|
|
|
27
|
|
|
$this->jiraLogFile = $this->env('JIRA_LOG_FILE', 'jira-rest-client.log'); |
|
|
|
|
28
|
|
|
$this->jiraLogLevel = $this->env('JIRA_LOG_LEVEL', 'WARNING'); |
|
|
|
|
29
|
|
|
$this->curlOptSslVerifyHost = $this->env('CURLOPT_SSL_VERIFYHOST', false); |
|
|
|
|
30
|
|
|
$this->curlOptSslVerifyPeer = $this->env('CURLOPT_SSL_VERIFYPEER', false); |
|
|
|
|
31
|
|
|
$this->curlOptUserAgent = $this->env('CURLOPT_USERAGENT', $this->getDefaultUserAgentString()); |
|
|
|
|
32
|
|
|
$this->curlOptVerbose = $this->env('CURLOPT_VERBOSE', false); |
|
|
|
|
33
|
|
|
$this->proxyServer = $this->env('PROXY_SERVER'); |
|
|
|
|
34
|
|
|
$this->proxyPort = $this->env('PROXY_PORT'); |
|
|
|
|
35
|
|
|
$this->proxyUser = $this->env('PROXY_USER'); |
|
|
|
|
36
|
|
|
$this->proxyPassword = $this->env('PROXY_PASSWORD'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Gets the value of an environment variable. Supports boolean, empty and null. |
41
|
|
|
* |
42
|
|
|
* @param string $key |
43
|
|
|
* @param mixed $default |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
private function env($key, $default = null) |
48
|
|
|
{ |
49
|
|
|
$value = getenv($key); |
50
|
|
|
|
51
|
|
|
if ($value === false) { |
52
|
|
|
return $default; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
switch (strtolower($value)) { |
56
|
|
|
case 'true': |
57
|
|
|
case '(true)': |
58
|
|
|
return true; |
59
|
|
|
|
60
|
|
|
case 'false': |
61
|
|
|
case '(false)': |
62
|
|
|
return false; |
63
|
|
|
|
64
|
|
|
case 'empty': |
65
|
|
|
case '(empty)': |
66
|
|
|
return ''; |
67
|
|
|
|
68
|
|
|
case 'null': |
69
|
|
|
case '(null)': |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->startsWith($value, '"') && $this->endsWith($value, '"')) { |
74
|
|
|
return substr($value, 1, -1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Determine if a given string starts with a given substring. |
82
|
|
|
* |
83
|
|
|
* @param string $haystack |
84
|
|
|
* @param string|array $needles |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function startsWith($haystack, $needles) |
89
|
|
|
{ |
90
|
|
|
foreach ((array) $needles as $needle) { |
91
|
|
|
if ($needle != '' && strpos($haystack, $needle) === 0) { |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Determine if a given string ends with a given substring. |
101
|
|
|
* |
102
|
|
|
* @param string $haystack |
103
|
|
|
* @param string|array $needles |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function endsWith($haystack, $needles) |
108
|
|
|
{ |
109
|
|
|
foreach ((array) $needles as $needle) { |
110
|
|
|
if ((string) $needle === substr($haystack, -strlen($needle))) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* load dotenv |
120
|
|
|
* |
121
|
|
|
* @param $path |
122
|
|
|
* @throws JiraException |
123
|
|
|
*/ |
124
|
|
|
private function loadDotEnv($path) |
125
|
|
|
{ |
126
|
|
|
$requireParam = [ |
127
|
|
|
'JIRA_HOST', 'JIRA_USER', 'JIRA_PASS', |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
// support for dotenv 1.x and 2.x. see also https://github.com/lesstif/php-jira-rest-client/issues/102 |
131
|
|
|
if (class_exists('\Dotenv\Dotenv')) { |
132
|
|
|
|
133
|
|
|
// dirty solution for check whether dotenv v3 or v2 |
134
|
|
|
try { |
135
|
|
|
$method = new \ReflectionMethod('\Dotenv\Dotenv', 'create'); |
|
|
|
|
136
|
|
|
|
137
|
|
|
$dotenv = \Dotenv\Dotenv::create($path); |
138
|
|
|
|
139
|
|
|
$dotenv->safeLoad(); |
140
|
|
|
$dotenv->required($requireParam); |
141
|
|
|
} catch (\ReflectionException $re) { |
142
|
|
|
// dotenv v2 doesn't have create method. |
143
|
|
|
$dotenv = new \Dotenv\Dotenv($path); |
144
|
|
|
|
145
|
|
|
$dotenv->load(); |
146
|
|
|
|
147
|
|
|
$dotenv->required($requireParam); |
148
|
|
|
} |
149
|
|
|
} elseif (class_exists('\Dotenv')) { // DotEnv v1 |
150
|
|
|
\Dotenv::load($path); |
151
|
|
|
\Dotenv::required($requireParam); |
152
|
|
|
} else { |
153
|
|
|
throw new JiraException('can not load PHP dotenv class.!'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.