1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: keanor
5
+ * Date: 17.08.15
6
+ * Time: 22:01
7
+ */
8
+ namespace JiraRestApi \Configuration ;
9
+
10
+ use Dotenv ;
11
+
12
+ /**
13
+ * Class DotEnvConfiguration
14
+ *
15
+ * @package JiraRestApi\Configuration
16
+ */
17
+ class DotEnvConfiguration extends AbstractConfiguration
18
+ {
19
+ /**
20
+ * @param string $path
21
+ */
22
+ public function __construct ($ path = '. ' )
23
+ {
24
+ Dotenv::load ($ path );
25
+
26
+ // not available in dotenv 1.1
27
+ // $dotenv->required(['JIRA_HOST', 'JIRA_USER', 'JIRA_PASS']);
28
+
29
+ $ this ->jiraHost = $ this ->env ('JIRA_HOST ' );
30
+ $ this ->jiraUser = $ this ->env ('JIRA_USER ' );
31
+ $ this ->jiraPassword = $ this ->env ('JIRA_PASS ' );
32
+ $ this ->jiraLogFile = $ this ->env ('JIRA_LOG_FILE ' , 'jira-rest-client.log ' );
33
+ $ this ->jiraLogLevel = $ this ->env ('JIRA_LOG_LEVEL ' , 'WARNING ' );
34
+ $ this ->curlOptSslVerifyHost = $ this ->env ('CURLOPT_SSL_VERIFYHOST ' , false );
35
+ $ this ->curlOptSslVerifyPeer = $ this ->env ('CURLOPT_SSL_VERIFYPEER ' , false );
36
+ $ this ->curlOptVerbose = $ this ->env ('CURLOPT_VERBOSE ' , false );
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 null ;
71
+ }
72
+
73
+ if ($ this ->startsWith ($ value , '" ' ) && 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
+ }
0 commit comments