RenderContextExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 50
cts 60
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 84 5
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. This RenderContext object is injected into the template data to allow usefulness in the templates. */
8
final class RenderContextExtension implements Plates\Extension
9
{
10 24
    public function register(Plates\Engine $plates) {
11 24
        $c = $plates->getContainer();
12 24
        $c->addStack('renderContext.func', function($c) {
13
            return [
14 24
                'notFound' => notFoundFunc(),
15 24
                'plates' => Plates\Util\stackGroup([
16 24
                    splitByNameFunc($c->get('renderContext.func.funcs')),
17 24
                    aliasNameFunc($c->get('renderContext.func.aliases')),
18
                ]),
19
            ];
20 24
        });
21 24
        $c->add('renderContext.func.aliases', [
22 24
            'e' => 'escape',
23
            '__invoke' => 'escape',
24
            'stop' => 'end',
25
        ]);
26 24
        $c->add('renderContext.func.funcs', function($c) {
27 24
            $template_args = assertTemplateArgsFunc();
28 24
            $one_arg = assertArgsFunc(1);
29 24
            $config = $c->get('config');
30
31
            return [
32 24
                'insert' => [insertFunc(), $template_args],
33
                'escape' => [
34 24
                    isset($config['escape_flags'], $config['escape_encoding'])
35
                        ? escapeFunc($config['escape_flags'], $config['escape_encoding'])
36 24
                        : escapeFunc(),
37 24
                    $one_arg,
38
                ],
39 24
                'data' => [templateDataFunc(), assertArgsFunc(0, 1)],
40 24
                'name' => [accessTemplatePropFunc('name')],
41 24
                'context' => [accessTemplatePropFunc('context')],
42 24
                'component' => [componentFunc(), $template_args],
43 24
                'slot' => [slotFunc(), $one_arg],
44 24
                'end' => [endFunc()]
45
            ];
46 24
        });
47 24
        $c->add('include.bind', function($c) {
48
            return renderContextBind($c->get('config')['render_context_var_name']);
49 24
        });
50 24
        $c->add('renderContext.factory', function($c) {
51 24
            return RenderContext::factory(
52
                function() use ($c) { return $c->get('renderTemplate'); },
53 24
                $c->get('renderContext.func')
54
            );
55 24
        });
56
57 24
        $plates->defineConfig([
0 ignored issues
show
Documentation Bug introduced by
The method defineConfig does not exist on object<League\Plates\Engine>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58 24
            'render_context_var_name' => 'v',
59
            'escape_encoding' => null,
60
            'escape_flags' => null,
61
        ]);
62 24
        $plates->pushComposers(function($c) {
0 ignored issues
show
Documentation Bug introduced by
The method pushComposers does not exist on object<League\Plates\Engine>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
63
            return [
64 24
                'renderContext.renderContext' => renderContextCompose(
65 24
                    $c->get('renderContext.factory'),
66 24
                    $c->get('config')['render_context_var_name']
67
                )
68
            ];
69 24
        });
70
71 24
        $plates->addMethods([
72 24
            'registerFunction' => function(Plates\Engine $e, $name, callable $func, callable $assert_args = null, $simple = true) {
73
                $c = $e->getContainer();
74
                $func = $simple ? wrapSimpleFunc($func) : $func;
75
76
                $c->wrap('renderContext.func.funcs', function($funcs, $c) use ($name, $func, $assert_args) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
                    $funcs[$name] = $assert_args ? [$assert_args, $func] : [$func];
78
                    return $funcs;
79
                });
80 24
            },
81
            'addFuncs' => function(Plates\Engine $e, callable $add_funcs, $simple = false) {
82 24
                $e->getContainer()->wrap('renderContext.func.funcs', function($funcs, $c) use ($add_funcs, $simple) {
83 24
                    $new_funcs = $simple
84
                        ? array_map(wrapSimpleFunc::class, $add_funcs($c))
85 24
                        : $add_funcs($c);
86 24
                    return array_merge($funcs, $new_funcs);
87 24
                });
88 24
            },
89 24
            'wrapFuncs' => function(Plates\Engine $e, callable $wrap_funcs) {
90
                $e->getContainer()->wrap('renderContext.func.funcs', $wrap_funcs);
91 24
            }
92
        ]);
93 24
    }
94
}
95