thephpleague /
plates
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
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 | 36 | return $arg; |
|
| 11 | 36 | }; |
|
| 12 | } |
||
| 13 | |||
| 14 | /** wraps a closure in output buffering and returns the buffered |
||
| 15 | content. */ |
||
| 16 | function obWrap(callable $wrap) { |
||
| 17 | 28 | $cur_level = ob_get_level(); |
|
| 18 | |||
| 19 | try { |
||
| 20 | 28 | ob_start(); |
|
| 21 | 28 | $wrap(); |
|
| 22 | 28 | return ob_get_clean(); |
|
| 23 | } catch (\Exception $e) {} |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
| 24 | catch (\Throwable $e) {} |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||
| 25 | |||
| 26 | // clean the ob stack |
||
| 27 | while (ob_get_level() > $cur_level) { |
||
| 28 | ob_end_clean(); |
||
| 29 | } |
||
| 30 | |||
| 31 | throw $e; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** simple utility that wraps php echo which allows for stubbing out the |
||
| 35 | echo func for testing */ |
||
| 36 | function phpEcho() { |
||
| 37 | return function($v) { |
||
| 38 | 16 | echo $v; |
|
| 39 | 28 | }; |
|
| 40 | } |
||
| 41 | |||
| 42 | /** stack a set of functions into each other and returns the stacked func */ |
||
| 43 | function stack(array $funcs) { |
||
| 44 | return array_reduce($funcs, function($next, $func) { |
||
| 45 | return function(...$args) use ($next, $func) { |
||
| 46 | 60 | $args[] = $next; |
|
| 47 | 60 | return $func(...$args); |
|
| 48 | 60 | }; |
|
| 49 | }, function() { |
||
| 50 | 4 | throw new StackException('No handler was able to return a result.'); |
|
| 51 | 64 | }); |
|
| 52 | } |
||
| 53 | |||
| 54 | function stackGroup(array $funcs) { |
||
| 55 | 28 | $end_next = null; |
|
| 56 | array_unshift($funcs, function(...$args) use (&$end_next) { |
||
| 57 | 4 | return $end_next(...array_slice($args, 0, -1)); |
|
| 58 | 28 | }); |
|
| 59 | 28 | $next = stack($funcs); |
|
| 60 | return function(...$args) use ($next, &$end_next) { |
||
| 61 | 20 | $end_next = end($args); |
|
| 62 | 20 | return $next(...array_slice($args, 0, -1)); |
|
| 63 | 28 | }; |
|
| 64 | } |
||
| 65 | |||
| 66 | /** compose(f, g)(x) = f(g(x)) */ |
||
| 67 | function compose(...$funcs) { |
||
| 68 | 24 | return pipe(...array_reverse($funcs)); |
|
| 69 | } |
||
| 70 | |||
| 71 | /** pipe(f, g)(x) = g(f(x)) */ |
||
| 72 | function pipe(...$funcs) { |
||
| 73 | return function($arg) use ($funcs) { |
||
| 74 | return array_reduce($funcs, function($acc, $func) { |
||
| 75 | 24 | return $func($acc); |
|
| 76 | 24 | }, $arg); |
|
| 77 | 24 | }; |
|
| 78 | } |
||
| 79 | |||
| 80 | function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) { |
||
| 81 | return array_reduce(array_filter($parts), function($acc, $part) use ($sep) { |
||
| 82 | 32 | if ($acc === null) { |
|
| 83 | 32 | return rtrim($part, $sep); |
|
| 84 | } |
||
| 85 | |||
| 86 | 32 | return $acc . $sep . ltrim($part, $sep); |
|
| 87 | 32 | }, null); |
|
| 88 | } |
||
| 89 | |||
| 90 | function isAbsolutePath($path) { |
||
| 91 | 40 | return strpos($path, '/') === 0; |
|
| 92 | } |
||
| 93 | function isRelativePath($path) { |
||
| 94 | 36 | return strpos($path, './') === 0 || strpos($path, '../') === 0; |
|
| 95 | } |
||
| 96 | function isResourcePath($path) { |
||
| 97 | 20 | return strpos($path, '://') !== false; |
|
| 98 | } |
||
| 99 | function isPath($path) { |
||
| 100 | 32 | 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 | 8 | if (is_object($v)) { |
|
| 106 | 4 | return 'object ' . get_class($v); |
|
| 107 | } |
||
| 108 | |||
| 109 | 4 | 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 | 8 | return $predicate($arg) ? $fn($arg) : $arg; |
|
| 153 | 8 | }; |
|
| 154 | } |
||
| 155 |