-
Notifications
You must be signed in to change notification settings - Fork 3
/
Functions.php
178 lines (167 loc) · 4.95 KB
/
Functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace CCL;
class Functions {
/**
* Assert that we are properly executing within the context of a compilation task.
*
* If this script tries to run in any other context, then you will get some
* kind of error (e.g. class not found or RuntimeException).
*/
public function assertTask() {
\Civi\CompilePlugin\Util\Script::assertTask();
}
/**
* Array-map function. Similar to array_map(), but tuned to key-value pairs.
*
* Example:
* $data = [100 => 'apple', 200 => 'banana'];
* $opposite = mapkv($data, function($k, $v){ return [-1 * $k => strtoupper($v)]; });
*
* This would return [-100 => 'APPLE', -200 => 'BANANA']
*
* By convention, mapping functions should return an 1-row array "[newKey => newValue]".
*
* Some unconventional forms are also defined:
* - Return empty array ==> Skip/omit the row
* - Return multiple items ==> Add all items to the result
* - Return an unkeyed (numeric) array ==> Discard original keys. Items are appended numerically (`$arr[] = $value`).
*
* @param array $array
* Values to iterate over
* @param callable $func
* Callback function.
* function(scalar $key, mixed $value): array
* @return array
* The filtered array.
*/
public function mapkv($array, $func) {
$r = [];
foreach ($array as $k => $v) {
foreach ($func($k, $v) as $out_k => $out_v) {
if (isset($r[$out_k])) {
$r[] = $out_v;
}
else {
$r[$out_k] = $out_v;
}
}
}
return $r;
}
/**
* Map file-names.
*
* @param string $matchPat
* Ex: 'src/*.json'
* @param string $outPat
* Ex: 'dest/#1.json'
* @param bool $flip
* The orientation of the result map.
* If false, returned as "original => filtered".
* If true, returned as "filtered => original".
* @return array
* List of files and the corresponding names.
*/
public function globMap($matchPat, $outPat, $flip = FALSE) {
$inFiles = glob($matchPat);
$regex = ';' . preg_quote($matchPat, ';') . ';';
$regex = str_replace(preg_quote('*', ';'), '(.*)', $regex);
$replacement = preg_replace(';#(\d+);', '\\' . '\\\1', $outPat);
$outFiles = preg_replace($regex, $replacement, $inFiles);
return $flip ? array_combine($outFiles, $inFiles) : array_combine($inFiles, $outFiles);
}
public function chdir($directory) {
if (!\chdir($directory)) {
throw new IOException("Failed to change directory ($directory)");
}
}
/**
* @param string|string[] $pats
* List of glob patterns.
* @param null|int $flags
* @return array
* List of matching files.
*/
public function glob($pats, $flags = NULL) {
$r = [];
$pats = (array) $pats;
foreach ($pats as $pat) {
$r = array_unique(array_merge($r, (array) \glob($pat, $flags)));
}
sort($r);
return $r;
}
/**
* Read a set of files and concatenate the results
*
* @param string|string[] $srcs
* Files to read. These may be globs.
* @param string $newLine
* Whether to ensure that joined files have a newline separator.
* Ex: 'raw' (as-is), 'auto' (add if missing)
* @return string
* The result of joining the files.
*/
public function cat($srcs, $newLine = 'auto') {
$buf = '';
foreach (glob($srcs) as $file) {
if (!is_readable($file)) {
throw new \RuntimeException("Cannot read $file");
}
$buf .= file_get_contents($file);
switch ($newLine) {
case 'auto':
if (substr($buf, -1) !== "\n") {
$buf .= "\n";
}
break;
case 'raw':
// Don't
break;
}
}
return $buf;
}
///**
// * Atomically dumps content into a file.
// *
// * @param string $filename The file to be written to
// * @param string $content The data to write into the file
// *
// * @throws IOException if the file cannot be written to
// */
//public function write($file, $content) {
// \CCL\dumpFile($file, $content);
//}
///**
// * Copy file(s) to a destination.
// *
// * This does work with files or directories. However, if you wish to reference a directory, then
// * it *must* end with a trailing slash. Ex:
// *
// * Copy "infile.txt" to "outfile.txt"
// * cp("infile.txt", "outfile.txt");
// *
// * Copy "myfile.txt" to "out-dir/myfile.txt"
// * cp("myfile.txt", "out-dir/");
// *
// * Recursively copy "in-dir/*" into "out-dir/"
// * cp("in-dir/*", "out-dir/");
// *
// * Recursively copy the whole "in-dir/" into "out-dir/deriv/"
// * cp("in-dir/", "out-dir/deriv/");
// *
// * @param string $srcs
// * @param string $dest
// */
//public function cp($srcs, $dest) {
// $destType = substr($dest, -1) === '/' ? 'D' : 'F';
//
// foreach (glob($srcs, MARK) as $src) {
// $srcType = substr($src, -1) === '/' ? 'D' : 'F';
// switch ($srcType . $destType) {
// }
// }
//
//}
}