Completed
Push — 183-default-layout-ext ( a97107 )
by
unknown
10:37
created

layout-sections.php ➔ startFunc()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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