Completed
Pull Request — v4.0-dev (#193)
by
unknown
23:34 queued 22:04
created

func.php ➔ componentFunc()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 2
nop 1
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 4
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\RenderContext;
4
5
use League\Plates;
6
use League\Plates\Exception\FuncException;
7
8
function componentFunc($insert = null) {
9 16
    $insert = $insert ?: insertFunc();
10
    return startBufferFunc(function(FuncArgs $args) use ($insert) {
11 12
        if ($args->template()->get('component_slot_data') !== null) {
12 4
            throw new FuncException('Cannot nest component func calls.');
13
        }
14
15 12
        $args->template()->with('component_slot_data', []);
16
        return function($contents) use ($insert, $args) {
17 8
            list($name, $data) = $args->args;
18
19 8
            $data = array_merge(
20 8
                $data ?: [],
21 8
                ['slot' => $contents],
22 8
                $args->template()->get('component_slot_data')
23
            );
24
25 8
            $insert($args->withArgs([$name, $data]));
26
27 8
            $args->template()->with('component_slot_data', null);
28 12
        };
29 16
    });
30
}
31
32
function slotFunc() {
33
    return startBufferFunc(function(FuncArgs $args) {
34 8
        if ($args->template()->get('component_slot_data') === null) {
35 4
            throw new FuncException('Cannot call slot func outside of component definition.');
36
        }
37
38
        return function($contents) use ($args) {
39 4
            $slot_data = $args->template()->get('component_slot_data');
40 4
            $slot_data[$args->args[0]] = $contents;
41 4
            $args->template()->with('component_slot_data', $slot_data);
42 4
        };
43 16
    });
44
}
45
46
function startBufferFunc(callable $create_callback) {
47
    return function(FuncArgs $args) use ($create_callback) {
48 44
        $buffer_stack = $args->template()->get('buffer_stack') ?: [];
49
50 44
        ob_start();
51 44
        $buffer_stack[] = [ob_get_level(), $create_callback($args)];
52
53 40
        $args->template()->with('buffer_stack', $buffer_stack);
54 48
    };
55
}
56
57
function endFunc() {
58
    return function(FuncArgs $args) {
59 36
        $buffer_stack = $args->template()->get('buffer_stack') ?: [];
60 36
        if (!count($buffer_stack)) {
61 4
            throw new FuncException('Cannot end a section definition because no section has been started.');
62
        }
63
64 32
        list($ob_level, $callback) = array_pop($buffer_stack);
65
66 32
        if ($ob_level != ob_get_level()) {
67 4
            throw new FuncException('Output buffering level does not match when section was started.');
68
        }
69
70 28
        $contents = ob_get_clean();
71
72 28
        $callback($contents);
73
74 28
        $args->template()->with('buffer_stack', $buffer_stack);
75 40
    };
76
}
77
78
function insertFunc($echo = null) {
79 12
    $echo = $echo ?: Plates\Util\phpEcho();
80
81
    return function(FuncArgs $args) use ($echo) {
82 4
        list($name, $data) = $args->args;
83 4
        $child = $args->template()->fork($name, $data ?: []);
84 4
        $echo($args->render->renderTemplate($child));
85 12
    };
86
}
87
88
function accessTemplatePropFunc($prop) {
89
    return function(FuncArgs $args) use ($prop) {
90
        return $args->template()->{$prop};
91 8
    };
92
}
93
94
function escapeFunc($flags = ENT_COMPAT | ENT_HTML401, $encoding = 'UTF-8') {
95
    return function(FuncArgs $args) use ($flags, $encoding) {
96 4
        return htmlspecialchars($args->args[0], $flags, $encoding);
97 8
    };
98
}
99
100
function assertArgsFunc($num_required, $num_default = 0) {
101
    return function(FuncArgs $args, $next) use ($num_required, $num_default) {
102 8
        if (count($args->args) < $num_required) {
103
            throw new FuncException("Func {$args->func_name} has {$num_required} argument(s).");
104
        }
105
106 8
        if (count($args->args) >= $num_required + $num_default) {
107 8
            return $next($args);
108
        }
109
110 4
        $args = $args->withArgs(array_merge($args->args, array_fill(
111 4
            0,
112 4
            $num_required + $num_default - count($args->args),
113 4
            null
114
        )));
115
116 4
        return $next($args);
117 8
    };
118
}
119
120
function assertTemplateArgsFunc() {
121 8
    return assertArgsFunc(1, 2);
122
}
123
124
/** Creates aliases for certain functions */
125
function aliasNameFunc(array $aliases) {
126
    return function(FuncArgs $args, $next) use ($aliases) {
127 8
        if (!isset($aliases[$args->func_name])) {
128 8
            return $next($args);
129
        }
130
131 4
        while (isset($aliases[$args->func_name])) {
132 4
            $args = $args->withName($aliases[$args->func_name]);
133
        }
134
135 4
        return $next($args);
136 8
    };
137
}
138
139
/** Allows splitting of the handlers from the args name */
140
function splitByNameFunc(array $handlers) {
141
    return function(FuncArgs $args, $next) use ($handlers) {
142 8
        $name = $args->func_name;
143 8
        if (isset($handlers[$name])) {
144 8
            $handler = Plates\Util\stackGroup($handlers[$name]);
145 8
            return $handler($args, $next);
146
        }
147
148
        return $next($args);
149 8
    };
150
}
151
152
function notFoundFunc() {
153
    return function(FuncArgs $args) {
154
        throw new FuncException('The function ' . $args->func_name . ' does not exist.');
155 8
    };
156
}
157