Completed
Push — v4.0-dev ( b14a8f...3b7e85 )
by
unknown
19:33 queued 05:42
created

include.php ➔ phpInclude()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 1
nop 0
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Template;
4
5
use League\Plates;
6
7
function phpInclude() {
8
    /**
9
     * @param string    the template path
10
     * @param array     the template vars
11
     */
12
    $inc = function() {
13
        ob_start();
14
        extract(func_get_arg(1));
0 ignored issues
show
Bug introduced by
func_get_arg(1) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
15
        include func_get_arg(0);
16
        return ob_get_clean();
17
    };
18
19
    return function(...$args) use ($inc) {
20
        $cur_level = ob_get_level();
21
        try {
22
            return $inc(...$args);
23
        } catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
24
          catch (\Throwable $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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
35
function validatePathInclude($include, $file_exists = 'file_exists') {
36
    return function($path, array $vars) use ($include, $file_exists) {
37
        if (!$file_exists($path)) {
38
            throw new Plates\Exception\PlatesException('Template path ' . $path . ' does not exist.');
39
        }
40
41
        return $include($path, $vars);
42
    };
43
}
44
/** provieds a map of path -> includer */
45
function mockInclude($mocks) {
46
    return function($path, array $vars) use ($mocks) {
47
        if (!isset($mocks[$path])) {
48
            throw new Plates\Exception\PlatesException('Mock include does not exist for path: ' . $path);
49
        }
50
51
        return $mocks[$path]($path, $vars);
52
    };
53
}
54