-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
116 lines (100 loc) · 2.61 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
<?php
declare(strict_types=1);
\error_reporting(-1);
function array_merge_recursive_adv(array ...$arrays): array
{
$master = [];
foreach ($arrays as $array) {
foreach ($array as $key => $values) {
if (!isset($master[$key])) {
// If it doesn't already exist, add it and keep the incoming contents
$master[$key] = $values;
} else {
// If it DOES exist...
if (is_scalar($values)) {
// Replace the contents
$master[$key] = $values;
} elseif (is_array($values)) {
$master[$key] = array_merge_recursive_adv($master[$key], $values);
}
}
}
}
return $master;
}
/**
* Perform a case-insensitive, natural key sort on the associative array.
*
* @param array &$array The array to sort. Updates the array in-place.
*/
function natkeysort(&$array): void
{
uksort($array, function($a, $b): int {
return strcasecmp($a, $b);
});
}
/**
* Perform a custom sort, intended for the "requires" (or "requires-dev") key.
*
* @param array &$array The array to sort. Updates the array in-place.
*/
function composersort(&$array): void
{
// Groups
$master = [];
$php = null;
$ext = [];
$lib = [];
$pack = [];
// Sift the entries
foreach ($array as $key => $value) {
if ('php' === $key) {
$php = $value;
unset($array[$key]);
} elseif ('ext-' === substr($key, 0, 4)) {
$ext[$key] = $value;
unset($array[$key]);
} elseif ('lib-' === substr($key, 0, 4)) {
$lib[$key] = $value;
unset($array[$key]);
}
}
// Sort the entries
natkeysort($ext);
natkeysort($lib);
natkeysort($array);
// Re-merge the entries
$master['php'] = $php;
foreach ($ext as $key => $value) {
$master[$key] = $value;
}
foreach ($lib as $key => $value) {
$master[$key] = $value;
}
foreach ($array as $key => $value) {
$master[$key] = $value;
}
// Replace the old array with the new one
$array = $master;
}
/**
* Strips the `.tmpl` from the template filename.
*
* @param string $s The template filename.
*/
function strip_tmpl($s): string
{
return str_replace('.tmpl', '', $s);
}
/**
* Gets the template ID for the filename.
*
* @param string $s The template filename.
*/
function get_tmpl($s): string
{
$a = explode('.', $s);
$value = array_shift($a);
$value = preg_replace('/^_/', '', $value);
return $value;
}