layout-sections.php ➔ startFunc()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 3
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\LayoutSections;
4
5
use League\Plates;
6
use League\Plates\Template;
7
8
use League\Plates\Extension\RenderContext\FuncArgs;
9
use function League\Plates\Extension\RenderContext\startBufferFunc;
10
11
function sectionsCompose() {
12
    return function(Template $template) {
13 24
        return $template->with('sections', $template->parent ? $template->parent()->get('sections') : new Sections());
14 24
    };
15
}
16
17
function layoutFunc() {
18
    return function(FuncArgs $args) {
19 8
        list($name, $data) = $args->args;
20
21 8
        $layout = $args->template()->fork($name, $data ?: []);
22 8
        $args->template()->with('layout', $layout->reference);
23
24 8
        return $layout;
25 24
    };
26
}
27
28
function sectionFunc() {
29
    return function(FuncArgs $args) {
30 28
        list($name, $else) = $args->args;
31
32 28
        $res = $args->template()->get('sections')->get($name);
33 28
        if ($res || !$else) {
34 20
            return $res;
35
        }
36
37 12
        return is_callable($else)
38 8
            ? Plates\Util\obWrap($else)
39 12
            : (string) $else;
40 40
    };
41
}
42
43
const START_APPEND = 0;
44
const START_PREPEND = 1;
45
const START_REPLACE = 2;
46
47
/** Starts the output buffering for a section, update of 0 = replace, 1 = append, 2 = prepend */
48
function startFunc($update = START_REPLACE) {
49
    return startBufferFunc(function(FuncArgs $args) use ($update) {
50
        return function($contents) use ($update, $args) {
51 16
            $name = $args->args[0];
52 16
            $sections = $args->template()->get('sections');
53
54 16
            if ($update === START_APPEND) {
55 8
                $sections->append($name, $contents);
56 12
            } else if ($update === START_PREPEND) {
57 8
                $sections->prepend($name, $contents);
58
            } else {
59 4
                $sections->add($name, $contents);
60
            }
61 16
        };
62 36
    });
63
}
64