These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | 28 | return $arg; |
|
11 | 28 | }; |
|
12 | } |
||
13 | |||
14 | /** wraps a closure in output buffering and returns the buffered |
||
15 | content. */ |
||
16 | function obWrap(callable $wrap) { |
||
17 | $cur_level = ob_get_level(); |
||
18 | 12 | ||
19 | 20 | try { |
|
20 | ob_start(); |
||
21 | $wrap(); |
||
22 | return ob_get_clean(); |
||
23 | } catch (\Exception $e) {} |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
24 | catch (\Throwable $e) {} |
||
25 | |||
26 | 52 | // clean the ob stack |
|
27 | 52 | while (ob_get_level() > $cur_level) { |
|
28 | 52 | ob_end_clean(); |
|
29 | } |
||
30 | 4 | ||
31 | 56 | throw $e; |
|
32 | } |
||
33 | |||
34 | /** simple utility that wraps php echo which allows for stubbing out the |
||
35 | 20 | echo func for testing */ |
|
36 | function phpEcho() { |
||
37 | 4 | return function($v) { |
|
38 | 20 | echo $v; |
|
39 | 20 | }; |
|
40 | } |
||
41 | 16 | ||
42 | 16 | /** stack a set of functions into each other and returns the stacked func */ |
|
43 | 20 | function stack(array $funcs) { |
|
44 | return array_reduce($funcs, function($next, $func) { |
||
45 | return function(...$args) use ($next, $func) { |
||
46 | $args[] = $next; |
||
47 | return $func(...$args); |
||
48 | 16 | }; |
|
49 | }, function() { |
||
50 | throw new StackException('No handler was able to return a result.'); |
||
51 | }); |
||
52 | } |
||
53 | |||
54 | function stackGroup(array $funcs) { |
||
55 | 16 | $end_next = null; |
|
56 | 16 | array_unshift($funcs, function(...$args) use (&$end_next) { |
|
57 | 16 | return $end_next(...array_slice($args, 0, -1)); |
|
58 | }); |
||
59 | $next = stack($funcs); |
||
60 | return function(...$args) use ($next, &$end_next) { |
||
61 | $end_next = end($args); |
||
62 | 28 | return $next(...array_slice($args, 0, -1)); |
|
63 | 28 | }; |
|
64 | } |
||
65 | |||
66 | 28 | /** compose(f, g)(x) = f(g(x)) */ |
|
67 | 28 | function compose(...$funcs) { |
|
68 | return pipe(...array_reverse($funcs)); |
||
69 | } |
||
70 | |||
71 | 32 | /** pipe(f, g)(x) = g(f(x)) */ |
|
72 | function pipe(...$funcs) { |
||
73 | return function($arg) use ($funcs) { |
||
74 | 28 | return array_reduce($funcs, function($acc, $func) { |
|
75 | return $func($acc); |
||
76 | }, $arg); |
||
77 | 16 | }; |
|
78 | } |
||
79 | |||
80 | 24 | function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) { |
|
81 | return array_reduce(array_filter($parts), function($acc, $part) use ($sep) { |
||
82 | if ($acc === null) { |
||
83 | return rtrim($part, $sep); |
||
84 | } |
||
85 | 8 | ||
86 | 4 | return $acc . $sep . ltrim($part, $sep); |
|
87 | }, null); |
||
88 | } |
||
89 | 4 | ||
90 | function isAbsolutePath($path) { |
||
91 | return strpos($path, '/') === 0; |
||
92 | } |
||
93 | function isRelativePath($path) { |
||
94 | return strpos($path, './') === 0 || strpos($path, '../') === 0; |
||
95 | } |
||
96 | function isResourcePath($path) { |
||
97 | return strpos($path, '://') !== false; |
||
98 | } |
||
99 | function isPath($path) { |
||
100 | return isAbsolutePath($path) || isRelativePath($path) || isResourcePath($path); |
||
101 | } |
||
102 | |||
103 | /** returns the debug type of an object as string for exception printing */ |
||
104 | function debugType($v) { |
||
105 | if (is_object($v)) { |
||
106 | return 'object ' . get_class($v); |
||
107 | } |
||
108 | |||
109 | return gettype($v); |
||
110 | } |
||
111 | |||
112 | function spliceArrayAtKey(array $array, $key, array $values, $after = true) { |
||
113 | $new_array = []; |
||
114 | $spliced = false; |
||
115 | foreach ($array as $array_key => $val) { |
||
116 | if ($array_key == $key) { |
||
117 | $spliced = true; |
||
118 | if ($after) { |
||
119 | $new_array[$array_key] = $val; |
||
120 | $new_array = array_merge($new_array, $values); |
||
121 | } else { |
||
122 | $new_array = array_merge($new_array, $values); |
||
123 | $new_array[$array_key] = $val; |
||
124 | } |
||
125 | } else { |
||
126 | $new_array[$array_key] = $val; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | if (!$spliced) { |
||
131 | throw new PlatesException('Could not find key ' . $key . ' in array.'); |
||
132 | } |
||
133 | |||
134 | return $new_array; |
||
135 | } |
||
136 | |||
137 | function cachedFileExists(Psr\SimpleCache\CacheInterface $cache, $ttl = 3600, $file_exists = 'file_exists') { |
||
138 | return function($path) use ($cache, $ttl, $file_exists) { |
||
139 | $key = 'League.Plates.file_exists.' . $path; |
||
140 | $res = $cache->get($key); |
||
141 | if (!$res) { |
||
142 | $res = $file_exists($path); |
||
143 | $cache->set($key, $res, $ttl); |
||
144 | } |
||
145 | return $res; |
||
146 | }; |
||
147 | } |
||
148 | |||
149 | /** Invokes the callable if the predicate returns true. Returns the value otherwise. */ |
||
150 | function when(callable $predicate, callable $fn) { |
||
151 | return function($arg) use ($predicate, $fn) { |
||
152 | return $predicate($arg) ? $fn($arg) : $arg; |
||
153 | }; |
||
154 | } |
||
155 |