Completed
Push — master ( 22bc57...1ce92a )
by
unknown
29:24 queued 15:51
created

Extension/RenderContext/RenderContextExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 16
    public function register(Plates\Engine $plates) {
11 16
        $c = $plates->getContainer();
12 16
        $c->addStack('renderContext.func', function($c) {
13
            return [
14 16
                'notFound' => notFoundFunc(),
15 16
                'plates' => Plates\Util\stackGroup([
16 16
                    splitByNameFunc($c->get('renderContext.func.funcs')),
17 16
                    aliasNameFunc($c->get('renderContext.func.aliases')),
18
                ]),
19
            ];
20 16
        });
21 16
        $c->add('renderContext.func.aliases', [
22 16
            'e' => 'escape',
23
            '__invoke' => 'escape',
24
            'stop' => 'end',
25
        ]);
26 16
        $c->add('renderContext.func.funcs', function($c) {
27 16
            $template_args = assertTemplateArgsFunc();
28 16
            $one_arg = assertArgsFunc(1);
29 16
            $config = $c->get('config');
30
31
            return [
32 16
                'insert' => [insertFunc(), $template_args],
33
                'escape' => [
34 16
                    isset($config['escape_flags'], $config['escape_encoding'])
35
                        ? escapeFunc($config['escape_flags'], $config['escape_encoding'])
36 16
                        : escapeFunc(),
37 16
                    $one_arg,
38
                ],
39 16
                'data' => [templateDataFunc(), assertArgsFunc(0, 1)],
40 16
                'name' => [accessTemplatePropFunc('name')],
41 16
                'context' => [accessTemplatePropFunc('context')],
42 16
                'component' => [componentFunc(), $template_args],
43 16
                'slot' => [slotFunc(), $one_arg],
44 16
                'end' => [endFunc()]
45
            ];
46 16
        });
47 16
        $c->add('include.bind', function($c) {
48
            return renderContextBind($c->get('config')['render_context_var_name']);
49 16
        });
50 16
        $c->add('renderContext.factory', function($c) {
51 16
            return RenderContext::factory(
52
                function() use ($c) { return $c->get('renderTemplate'); },
53
                $c->get('renderContext.func')
54 16
            );
55 16
        });
56
57 16
        $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 16
            'render_context_var_name' => 'v',
59 16
            'escape_encoding' => null,
60
            'escape_flags' => null,
61 16
        ]);
62
        $plates->pushComposers(function($c) {
63 16
            return [
64
                'renderContext.renderContext' => renderContextCompose(
65 16
                    $c->get('renderContext.factory'),
66 16
                    $c->get('config')['render_context_var_name']
67
                )
68
            ];
69
        });
70
71
        $plates->addMethods([
72
            'registerFunction' => function(Plates\Engine $e, $name, callable $func, callable $assert_args = null, $simple = true) {
73
                $c = $e->getContainer();
74 16
                $func = $simple ? wrapSimpleFunc($func) : $func;
75
76 16
                $c->wrap('renderContext.func.funcs', function($funcs, $c) use ($name, $func, $assert_args) {
77 16
                    $funcs[$name] = $assert_args ? [$assert_args, $func] : [$func];
78
                    return $funcs;
79 16
                });
80 16
            },
81 16
            'addFuncs' => function(Plates\Engine $e, callable $add_funcs, $simple = false) {
82 16
                $e->getContainer()->wrap('renderContext.func.funcs', function($funcs, $c) use ($add_funcs, $simple) {
83 16
                    $new_funcs = $simple
84
                        ? array_map(wrapSimpleFunc::class, $add_funcs($c))
85 16
                        : $add_funcs($c);
86
                    return array_merge($funcs, $new_funcs);
87 16
                });
88
            },
89
            'wrapFuncs' => function(Plates\Engine $e, callable $wrap_funcs) {
90
                $e->getContainer()->wrap('renderContext.func.funcs', $wrap_funcs);
91
            }
92
        ]);
93
    }
94
}
95