Completed
Push — extensions ( 84a6ae )
by
unknown
22:00 queued 07:01
created

RenderContextExtension   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 56 2
1
<?php
2
3
namespace League\Plates\Extension\RenderContext;
4
5
use League\Plates;
6
7
/** The render context extension provides a RenderContext object and functions to be used within the render context object */
8
final class RenderContextExtension implements Plates\Extension
9
{
10
    public function register(Plates\Engine $plates) {
11
        $c = $plates->getContainer();
12
        $c->add('renderContext.func', function($c) {
13
            return Plates\Util\stack($c->get('renderContext.func.stack'));
14
        });
15
        $c->add('renderContext.func.stack', function($c) {
16
            return [
17
                'plates' => Plates\Util\stackGroup([
18
                    aliasNameFunc($c->get('renderContext.func.aliases')),
19
                    splitByNameFunc($c->get('renderContext.func.funcs'))
20
                ])
21
            ];
22
        });
23
        $c->add('renderContext.func.aliases', [
24
            'e' => 'escape',
25
            '__invoke' => 'escape',
26
            'stop' => 'end',
27
        ]);
28
        $c->add('renderContext.func.funcs', function($c) {
29
            $template_args = assertTemplateArgsFunc();
30
            $one_arg = assertArgsFunc(1);
31
            $config = $c->get('config');
32
33
            return [
34
                'insert' => [$template_args, insertFunc()],
35
                'escape' => [
36
                    $one_arg,
37
                    isset($config['escape_flags'], $config['escape_encoding'])
38
                        ? escapeFunc($config['escape_flags'], $config['escape_encoding'])
39
                        : escapeFunc()
40
                ],
41
                'data' => [accessTemplatePropFunc('data')],
42
                'name' => [accessTemplatePropFunc('name')],
43
                'context' => [accessTemplatePropFunc('context')],
44
                'component' => [$template_args, componentFunc()],
45
                'slot' => [$one_arg, slotFunc()],
46
                'end' => [endFunc()]
47
            ];
48
        });
49
50
        $c->wrap('compose', function($compose, $c) {
51
            return Plates\Util\compose($compose, renderContextCompose(
52
                $c->get('renderContext.factory'),
53
                $c->get('config')['render_context_var_name']
54
            ));
55
        });
56
        $c->add('include.bind', function($c) {
57
            return renderContextBind($c->get('config')['render_context_var_name']);
58
        });
59
        $c->add('renderContext.factory', function($c) {
60
            return RenderContext::factory(
61
                function() use ($c) { return $c->get('renderTemplate'); },
62
                $c->get('renderContext.func')
63
            );
64
        });
65
    }
66
}
67