|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Codervio\Envmanager\Resolver; |
|
4
|
|
|
|
|
5
|
|
|
use Codervio\Envmanager\Resolver\AbstractResolver; |
|
6
|
|
|
use Codervio\Envmanager\Resolver\ValueResolverInterface; |
|
7
|
|
|
use Codervio\Envmanager\Exceptions\ValueException; |
|
8
|
|
|
|
|
9
|
|
|
class ValueResolver extends AbstractResolver implements ValueResolverInterface |
|
10
|
|
|
{ |
|
11
|
|
|
public function execute($value, $strict) |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Trim comments |
|
15
|
|
|
*/ |
|
16
|
|
|
$value = $this->trimComments($value); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Throw if failed one of ' or " sign |
|
20
|
|
|
*/ |
|
21
|
|
|
$value = $this->checkSurroundingStripes($value); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Trim on both side quotes and marks |
|
25
|
|
|
*/ |
|
26
|
|
|
$value = $this->trimSlashes($value); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Trim spaces |
|
30
|
|
|
*/ |
|
31
|
|
|
$value = trim($value); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Trim empty '' or "" |
|
35
|
|
|
*/ |
|
36
|
|
|
$value = $this->trimEmpty($value); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Remove "" and '' |
|
40
|
|
|
* Fix it on TK3 crashed |
|
41
|
|
|
*/ |
|
42
|
|
|
$value = $this->trimClosers($value); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Remove new lines |
|
46
|
|
|
*/ |
|
47
|
|
|
$value = $this->trimNewLines($value); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Trim backslashes "\" sign |
|
51
|
|
|
*/ |
|
52
|
|
|
$value = $this->trimBackslashes($value); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Parsing values |
|
56
|
|
|
*/ |
|
57
|
|
|
$value = $this->valueparser->parse($value, $strict); |
|
58
|
|
|
|
|
59
|
|
|
return $value; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function resolveComment($value) |
|
63
|
|
|
{ |
|
64
|
|
|
preg_match_all(self::GET_COMMENT, $value, $matches, PREG_SET_ORDER, 0); |
|
65
|
|
|
|
|
66
|
|
|
if (isset($matches[0][2])) { |
|
67
|
|
|
return trim($matches[0][2]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function trimBackslashes($result) |
|
74
|
|
|
{ |
|
75
|
|
|
$result = preg_replace('/\\\\/', '\\', $result); |
|
76
|
|
|
|
|
77
|
|
|
return stripslashes($result); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function trimSlashes($input) |
|
81
|
|
|
{ |
|
82
|
|
|
if (substr($input, 0, 1) === '"') { |
|
83
|
|
|
$input = ltrim($input, '"'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (substr($input, -1) === '"') { |
|
87
|
|
|
$input = rtrim($input, '"'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (substr($input, 0, 1) === '\'') { |
|
91
|
|
|
$input = ltrim($input, '\''); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (substr($input, -1) === '\'') { |
|
95
|
|
|
$input = rtrim($input, '\''); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $input; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function trimEmpty($result) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($result === '""' or $result === '\'\'') { |
|
104
|
|
|
return ''; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $result; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function trimNewLines($result) |
|
111
|
|
|
{ |
|
112
|
|
|
return str_replace(array(self::UNIX_NL, self::DOS_NL, self::MAC_NL), '', $result); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function trimClosers($input) |
|
116
|
|
|
{ |
|
117
|
|
|
$input = preg_replace('/^\'|\'$/', '', $input); |
|
118
|
|
|
$input = preg_replace('/^\"|\"$/', '', $input); |
|
119
|
|
|
|
|
120
|
|
|
return $input; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function trimComments($input) |
|
124
|
|
|
{ |
|
125
|
|
|
preg_match_all(self::EXPLODE_COMMENTS, $input, $matches, PREG_SET_ORDER, 0); |
|
126
|
|
|
|
|
127
|
|
|
if (isset($matches[0][3])) { |
|
128
|
|
|
return $matches[0][3]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (isset($matches[0][1])) { |
|
132
|
|
|
return $matches[0][1]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $input; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function checkSurroundingStripes($input) |
|
139
|
|
|
{ |
|
140
|
|
|
if (preg_match('/(.*)\s+(.*)/i', $input)) { |
|
141
|
|
|
$rep = '/(^["]+(.*)+["])|(^[\']+(.*)+[\'])/i'; |
|
142
|
|
|
if (!preg_match_all($rep, $input, $v)) { |
|
143
|
|
|
throw new ValueException(sprintf("A value with spaces should be closed with \" closure. Error on string: '%s' sign", $input)); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $input; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|