1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Selim; |
4
|
|
|
|
5
|
|
|
class Util |
6
|
|
|
{ |
7
|
|
|
public static function stripPhpComments($str) |
8
|
|
|
{ |
9
|
|
|
$regex_multiline = "/\\/\\*[^\\Z]*\\*\\//m"; |
10
|
|
|
$regex_singleline = "/\\/\\/.*/m"; |
11
|
|
|
$result = preg_filter($regex_multiline, "", $str); |
12
|
|
|
if ($result) { |
13
|
|
|
$str = $result; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
return preg_filter($regex_singleline, "", $str); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function boolToString($bool) |
20
|
|
|
{ |
21
|
|
|
return $bool ? 'true' : 'false'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function filterSitesByName(array $sites, $filterRegex) |
25
|
|
|
{ |
26
|
|
|
$arr = array(); |
27
|
|
|
foreach ($sites as $sc) { |
28
|
|
|
if (!$sc instanceof SiteConfig) { |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
if (preg_match("/$filterRegex/", $sc->name)) { |
32
|
|
|
array_push($arr, $sc); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $arr; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function findInArrayWithRegex(array $arr, $regex) |
40
|
|
|
{ |
41
|
|
|
foreach ($arr as $a) { |
42
|
|
|
if (preg_match($regex, $a)) { |
43
|
|
|
return $a; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $sspages should contain only objects of class SilverstripePage |
52
|
|
|
* @param string $filterRegex |
53
|
|
|
* @return array all SilverstripePages that match the regex |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public static function filterPagesByModules(array $sspages, $filterRegex) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$arr = array(); |
59
|
|
|
foreach ($sspages as $sspage) { |
60
|
|
|
if (!$sspage instanceof SilverstripePage) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
if ($sspage->hasModule("/$filterRegex/")) { |
64
|
|
|
array_push($arr, $sspage); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $arr; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $sspages |
74
|
|
|
* @param string $env environment type dev OR live OR test |
75
|
|
|
* @return array all SilverstripePages that have environment_type $env |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public static function filterPagesByEnvironmentType($sspages, $env) { |
|
|
|
|
78
|
|
|
$arr = array(); |
79
|
|
|
if(preg_match("/^(dev|test|live)$/",$env) === 1){ |
80
|
|
|
foreach ($sspages as $sspage) { |
81
|
|
|
if (!$sspage instanceof SilverstripePage) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
if ($env === $sspage->getEnvironmentType()) { |
85
|
|
|
array_push($arr, $sspage); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return $arr; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $sspages |
94
|
|
|
* @param string $filterRegex environment type dev OR live OR test |
95
|
|
|
* |
96
|
|
|
* @return array all SilverstripePages where the version string matches $filterRegex |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public static function filterPagesByVersion($sspages, $filterRegex) { |
|
|
|
|
99
|
|
|
$arr = array(); |
100
|
|
|
foreach ($sspages as $sspage) { |
101
|
|
|
if (!$sspage instanceof SilverstripePage) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
if (preg_match("/$filterRegex/",$sspage->getVersion())) { |
105
|
|
|
array_push($arr, $sspage); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $arr; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param SilverstripePage[] $sspages |
114
|
|
|
* @param bool $da_val |
115
|
|
|
* @return SilverstripePage[] |
116
|
|
|
*/ |
117
|
|
|
public static function filterPagesByDefaultAdmin($sspages, $da_val = true) { |
118
|
|
|
$arr = array(); |
119
|
|
|
$da_val = $da_val === true ? true : false; |
120
|
|
|
foreach ($sspages as $sspage) { |
121
|
|
|
if (!$sspage instanceof SilverstripePage) { |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
if ($sspage->hasDefaultAdmin() === $da_val) { |
125
|
|
|
array_push($arr, $sspage); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $arr; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $version "1.1.1" "2.1.4" "2.0" |
133
|
|
|
* |
134
|
|
|
* @return array [1,1,1] [2,1,4] [2,0,0] |
135
|
|
|
*/ |
136
|
|
|
public static function VersionStringsToArray($version) |
137
|
|
|
{ |
138
|
|
|
$arr = array(); |
139
|
|
|
$single = explode(".", $version); |
140
|
|
|
|
141
|
|
|
for ($i = 0;$i<3;$i++) { |
142
|
|
|
if (!isset($single[$i])) { |
143
|
|
|
$x = 0; |
144
|
|
|
} else { |
145
|
|
|
$x = intval($single[$i]); |
146
|
|
|
} |
147
|
|
|
array_push($arr, $x ? $x : 0); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $arr; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public static function VersionStringsGreaterThenOrEqual($version1, $version2) |
154
|
|
|
{ |
155
|
|
|
$v1 = Util::VersionStringsToArray($version1); |
156
|
|
|
$v2 = Util::VersionStringsToArray($version2); |
157
|
|
|
for ($i = 0; $i<3;$i++) { |
158
|
|
|
if ($v1[$i] < $v2[$i]) { |
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* puts blanks at the end of a string until it reaches the minimum length. |
168
|
|
|
* |
169
|
|
|
* @param string $str |
170
|
|
|
* @param int $min |
171
|
|
|
*/ |
172
|
|
|
public static function forceStringMinLength(&$str, $min) |
173
|
|
|
{ |
174
|
|
|
while (strlen($str) < $min) { |
175
|
|
|
$str .= " "; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public static function reportError($string,$die = true) { |
180
|
|
|
if($die) { |
181
|
|
|
exit($string); |
|
|
|
|
182
|
|
|
}else{ |
183
|
|
|
echo($string); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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.