1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines; |
6
|
|
|
|
7
|
|
|
class Lib |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param mixed $v |
11
|
|
|
* @param mixed $d |
12
|
|
|
* |
13
|
|
|
* @return mixed |
14
|
|
|
*/ |
15
|
2 |
|
public static function r(&$v, $d) |
16
|
|
|
{ |
17
|
2 |
|
if (isset($v)) { |
18
|
1 |
|
return $v; |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
return $d; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param mixed $v variable reference |
26
|
|
|
* @param mixed $d [optional] default value (null) |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
2 |
|
public static function v(&$v, $d = null) |
31
|
|
|
{ |
32
|
2 |
|
if (!isset($v)) { |
33
|
1 |
|
$v = $d; |
34
|
|
|
} |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string UUID version 4 |
39
|
|
|
*/ |
40
|
1 |
|
public static function generateUuid() |
41
|
|
|
{ |
42
|
1 |
|
return sprintf( |
43
|
1 |
|
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
44
|
|
|
|
45
|
|
|
// 32 bits for "time_low" |
46
|
1 |
|
mt_rand(0, 0xffff), |
47
|
1 |
|
mt_rand(0, 0xffff), |
48
|
|
|
|
49
|
|
|
// 16 bits for "time_mid" |
50
|
1 |
|
mt_rand(0, 0xffff), |
51
|
|
|
|
52
|
|
|
// 16 bits for "time_hi_and_version", |
53
|
|
|
// four most significant bits holds version number 4 |
54
|
1 |
|
mt_rand(0, 0x0fff) | 0x4000, |
55
|
|
|
|
56
|
|
|
// 16 bits, 8 bits for "clk_seq_hi_res", |
57
|
|
|
// 8 bits for "clk_seq_low", |
58
|
|
|
// two most significant bits holds zero and one for variant DCE1.1 |
59
|
1 |
|
mt_rand(0, 0x3fff) | 0x8000, |
60
|
|
|
|
61
|
|
|
// 48 bits for "node" |
62
|
1 |
|
mt_rand(0, 0xffff), |
63
|
1 |
|
mt_rand(0, 0xffff), |
64
|
1 |
|
mt_rand(0, 0xffff) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $command |
70
|
|
|
* @param array|string[] $arguments |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
2 |
|
public static function cmd($command, array $arguments) |
75
|
|
|
{ |
76
|
2 |
|
$buffer = $command; |
77
|
|
|
|
78
|
2 |
|
$arguments = call_user_func_array('self::merge', $arguments); |
79
|
|
|
|
80
|
2 |
|
foreach ($arguments as $argument) { |
81
|
2 |
|
$buffer .= ' ' . self::quoteArg($argument); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return $buffer; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* quote an argument to preserve its value verbatim when used as |
89
|
|
|
* a utility argument in shell |
90
|
|
|
* |
91
|
|
|
* @param string $argument |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
8 |
|
public static function quoteArg($argument) |
96
|
|
|
{ |
97
|
8 |
|
$parts = explode("'", $argument); |
98
|
|
|
|
99
|
8 |
|
$buffer = ''; |
100
|
8 |
|
foreach ($parts as $index => $part) { |
101
|
8 |
|
$index && $buffer .= "\\'"; |
102
|
8 |
|
$safe = preg_match('~^[a-zA-Z0-9,._+@%/-]*$~', $part); |
103
|
8 |
|
$buffer .= $safe ? $part : "'${part}'"; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ('' === $buffer) { |
107
|
|
|
$buffer = "''"; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $buffer; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Turn multi-line string into an array of lines. |
115
|
|
|
* |
116
|
|
|
* Handles (no) newline at the end of buffer |
117
|
|
|
* |
118
|
|
|
* @param string $buffer |
119
|
|
|
* |
120
|
|
|
* @return array|string[] |
121
|
|
|
*/ |
122
|
|
|
public static function lines($buffer) |
123
|
|
|
{ |
124
|
1 |
|
$lines = explode("\n", $buffer); |
125
|
1 |
|
$c = count($lines); |
126
|
1 |
|
if ($c && '' === $lines[$c - 1]) { |
127
|
1 |
|
array_pop($lines); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $lines; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* merge n parameters, if a scalar, turned into array, otherwise must be an array |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public static function merge() |
139
|
|
|
{ |
140
|
4 |
|
if (!$arrays = func_get_args()) { |
141
|
1 |
|
return $arrays; |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
foreach ($arrays as $key => $value) { |
145
|
3 |
|
if (!is_array($value)) { |
146
|
3 |
|
$arrays[$key] = (array)$value; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
3 |
|
return call_user_func_array('array_merge', $arrays); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Chunk an array of strings based on maximum string length per chunk |
155
|
|
|
* |
156
|
|
|
* @param array|string[] $array |
157
|
|
|
* @param int $maxLength |
158
|
|
|
* @param int $overHeadPerEntry |
159
|
|
|
* |
160
|
|
|
* @return array|array[] |
161
|
|
|
*/ |
162
|
|
|
public static function arrayChunkByStringLength(array $array, $maxLength, $overHeadPerEntry = 0) |
163
|
|
|
{ |
164
|
2 |
|
$chunks = array(); |
165
|
2 |
|
$chunkStringLength = 0; |
166
|
2 |
|
$chunk = array(); |
167
|
|
|
|
168
|
2 |
|
foreach ($array as $key => $value) { |
169
|
2 |
|
$entryLen = strlen($value) + $overHeadPerEntry; |
170
|
2 |
|
if ($chunkStringLength + $entryLen > $maxLength) { |
171
|
2 |
|
if (empty($chunk)) { |
172
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
173
|
1 |
|
'maximum length of %d is too little to chunk the array at %s %s (%d chunk(s) so far)', |
174
|
|
|
$maxLength, |
175
|
1 |
|
is_string($key) ? 'key' : 'index', |
176
|
1 |
|
is_string($key) ? var_export($key, true) : (int)$key, |
177
|
1 |
|
count($chunks) |
178
|
|
|
)); |
179
|
|
|
} |
180
|
1 |
|
$chunks[] = $chunk; |
181
|
1 |
|
$chunk = array(); |
182
|
1 |
|
$chunkStringLength = 0; |
183
|
|
|
} |
184
|
1 |
|
$chunk[] = $value; |
185
|
1 |
|
$chunkStringLength += $entryLen; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
if (!empty($chunk)) { |
189
|
1 |
|
$chunks[] = $chunk; |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
return $chunks; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get shell environment variables only from $_SERVER in PHP CLI |
197
|
|
|
* |
198
|
|
|
* Filter an array like $_SERVER in PHP CLI shell for environment |
199
|
|
|
* variables only. |
200
|
|
|
* |
201
|
|
|
* @param array $server |
202
|
|
|
* |
203
|
|
|
* @return array|string[] |
204
|
|
|
*/ |
205
|
|
|
public static function env(array $server) |
206
|
|
|
{ |
207
|
1 |
|
return array_filter( |
208
|
|
|
// Pipelines must not support environment variable names with '=' in them |
209
|
1 |
|
array_flip(preg_grep('~=~', array_keys($server))) |
210
|
|
|
// What PHP might have added |
211
|
|
|
+ array( |
212
|
1 |
|
'PHP_SELF' => null, |
213
|
|
|
'SCRIPT_NAME' => null, |
214
|
|
|
'SCRIPT_FILENAME' => null, |
215
|
|
|
'PATH_TRANSLATED' => null, |
216
|
|
|
'DOCUMENT_ROOT' => null, |
217
|
|
|
'REQUEST_TIME_FLOAT' => null, |
218
|
|
|
'REQUEST_TIME' => null, |
219
|
|
|
'argv' => null, |
220
|
|
|
'argc' => null, |
221
|
|
|
) |
222
|
1 |
|
+ $server, |
223
|
1 |
|
'is_string' |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* fallback for the php 5.3 version which does not have PHP_BINARY. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public static function phpBinary() |
233
|
|
|
{ |
234
|
1 |
|
return defined('PHP_BINARY') ? constant('PHP_BINARY') : PHP_BINDIR . '/php'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Empty "coalesce" function |
239
|
|
|
* |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
|
|
public static function emptyCoalesce() |
243
|
|
|
{ |
244
|
1 |
|
foreach (func_get_args() as $arg) { |
245
|
1 |
|
if (empty($arg)) { |
246
|
1 |
|
continue; |
247
|
|
|
} |
248
|
|
|
|
249
|
1 |
|
return $arg; |
250
|
|
|
} |
251
|
|
|
|
252
|
1 |
|
return null; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|