1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates\Util; |
4
|
|
|
|
5
|
|
|
use League\Plates\Exception\PlatesException; |
6
|
|
|
use League\Plates\Exception\StackException; |
7
|
|
|
|
8
|
|
|
function id() { |
9
|
|
|
return function($arg) { |
10
|
|
|
return $arg; |
11
|
|
|
}; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** simple utility that wraps php echo which allows for stubbing out the |
15
|
|
|
echo func for testing */ |
16
|
|
|
function phpEcho() { |
17
|
|
|
return function($v) { |
18
|
|
|
echo $v; |
19
|
|
|
}; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** stack a set of functions into each other and returns the stacked func */ |
23
|
|
|
function stack(array $funcs) { |
24
|
|
|
return array_reduce(array_reverse($funcs), function($next, $func) { |
25
|
|
|
return function(...$args) use ($next, $func) { |
26
|
|
|
$args[] = $next; |
27
|
|
|
return $func(...$args); |
28
|
|
|
}; |
29
|
|
|
}, function() { |
30
|
|
|
throw new StackException('No handler was able to return a result.'); |
31
|
|
|
}); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** takes a structured array and sorts them by priority. This allows for prioritized stacks. |
35
|
|
|
The structure is just an associative array where the indexes are numeric and the values |
36
|
|
|
are array of stack handlers. The array is sorted by key and then all inner arrays are merged |
37
|
|
|
together */ |
38
|
|
|
function sortStacks($stacks) { |
39
|
|
|
ksort($stacks); |
40
|
|
|
return array_merge(...$stacks); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function stackGroup(array $funcs) { |
44
|
|
|
$end_next = null; |
45
|
|
|
$funcs[] = function(...$args) use (&$end_next) { |
46
|
|
|
return $end_next(...array_slice($args, 0, -1)); |
47
|
|
|
}; |
48
|
|
|
$next = stack($funcs); |
49
|
|
|
return function(...$args) use ($next, &$end_next) { |
50
|
|
|
$end_next = end($args); |
51
|
|
|
return $next(...array_slice($args, 0, -1)); |
52
|
|
|
}; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function compose(...$funcs) { |
56
|
|
|
return function($arg) use ($funcs) { |
57
|
|
|
return array_reduce($funcs, function($acc, $func) { |
58
|
|
|
return $func($acc); |
59
|
|
|
}, $arg); |
60
|
|
|
}; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) { |
64
|
|
|
return array_reduce(array_filter($parts), function($acc, $part) use ($sep) { |
65
|
|
|
if ($acc === null) { |
66
|
|
|
return rtrim($part, $sep); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $acc . $sep . ltrim($part, $sep); |
70
|
|
|
}, null); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
function isAbsolutePath($path) { |
74
|
|
|
return strpos($path, '/') === 0; |
75
|
|
|
} |
76
|
|
|
function isRelativePath($path) { |
77
|
|
|
return strpos($path, './') === 0 || strpos($path, '../') === 0; |
78
|
|
|
} |
79
|
|
|
function isResourcePath($path) { |
80
|
|
|
return strpos($path, '://') !== false; |
81
|
|
|
} |
82
|
|
|
function isPath($path) { |
83
|
|
|
return isAbsolutePath($path) || isRelativePath($path) || isResourcePath($path); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** returns the debug type of an object as string for exception printing */ |
87
|
|
|
function debugType($v) { |
88
|
|
|
if (is_object($v)) { |
89
|
|
|
return 'object ' . get_class($v); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return gettype($v); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function spliceArrayAtKey(array $array, $key, array $values, $after = true) { |
96
|
|
|
$new_array = []; |
97
|
|
|
$spliced = false; |
98
|
|
|
foreach ($array as $array_key => $val) { |
99
|
|
|
if ($array_key == $key) { |
100
|
|
|
$spliced = true; |
101
|
|
|
if ($after) { |
102
|
|
|
$new_array[$array_key] = $val; |
103
|
|
|
$new_array = array_merge($new_array, $values); |
104
|
|
|
} else { |
105
|
|
|
$new_array = array_merge($new_array, $values); |
106
|
|
|
$new_array[$array_key] = $val; |
107
|
|
|
} |
108
|
|
|
} else { |
109
|
|
|
$new_array[$array_key] = $val; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (!$spliced) { |
114
|
|
|
throw new PlatesException('Could not find key ' . $key . ' in array.'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $new_array; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function cachedFileExists(Psr\SimpleCache\CacheInterface $cache, $ttl = 3600, $file_exists = 'file_exists') { |
121
|
|
|
return function($path) use ($cache, $ttl, $file_exists) { |
122
|
|
|
$key = 'League.Plates.file_exists.' . $path; |
123
|
|
|
$res = $cache->get($key); |
124
|
|
|
if (!$res) { |
125
|
|
|
$res = $file_exists($path); |
126
|
|
|
$cache->set($key, $res, $ttl); |
127
|
|
|
} |
128
|
|
|
return $res; |
129
|
|
|
}; |
130
|
|
|
} |
131
|
|
|
|