Completed
Push — fix-tests ( f5b038 )
by
unknown
11:21
created

func.php ➔ layoutFunc()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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