Completed
Push — v4.0-dev ( ea258d...cf92b6 )
by
unknown
02:01
created

func.php ➔ assertTemplateArgsFunc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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