Completed
Push — v4.0-dev ( 2cae77...599661 )
by
unknown
13s
created

func.php ➔ endFunc()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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