Completed
Push — master ( b3978b...8d3a7c )
by
unknown
26:09 queued 12:30
created

src/Engine.php (1 issue)

Severity

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;
4
5
/** API for the Plates system */
6
final class Engine
7
{
8
    private $container;
9
10 16
    public function __construct($config = []) {
11 16
        $this->container = new Util\Container();
12
13 16
        $this->container->add('engine_methods', []);
14 16
        $this->container->add('config', [
15 16
            'render_context_var_name' => 'v',
16
            'ext' => 'phtml',
17
            'base_dir' => null,
18
            'escape_encoding' => null,
19
            'escape_flags' => null,
20
            'validate_paths' => true,
21
            'php_extensions' => ['php', 'phtml'],
22
            'image_extensions' => ['png', 'jpg'],
23
        ]);
24
        $this->container->addComposed('compose', function() { return []; });
25 16
        $this->container->add('fileExists', function($c) {
0 ignored issues
show
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...
26 16
            return 'file_exists';
27 16
        });
28 16
        $this->container->add('renderTemplate', function($c) {
29 16
            $rt = new RenderTemplate\FileSystemRenderTemplate([
30
                [
31 16
                    Template\matchExtensions($c->get('config')['php_extensions']),
32 16
                    new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind'))
33
                ],
34
                [
35 16
                    Template\matchExtensions($c->get('config')['image_extensions']),
36 16
                    RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate())
37
                ],
38
                [
39 16
                    Template\matchStub(true),
40 16
                    new RenderTemplate\StaticFileRenderTemplate(),
41
                ]
42
            ]);
43 16
            if ($c->get('config')['validate_paths']) {
44 16
                $rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists'));
45
            }
46 16
            $rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) {
47 16
                return $create($rt);
48 16
            }, $rt);
49 16
            $rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose'));
50 16
            return $rt;
51 16
        });
52 16
        $this->container->add('renderTemplate.bind', function() {
53 16
            return Util\id();
54 16
        });
55 16
        $this->container->add('renderTemplate.factories', function() {
56 16
            return [];
57 16
        });
58
59 16
        $this->addMethods([
60
            'pushComposers' => function(Engine $e, $def_composer) {
61 16
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
62 16
                    return array_merge($composed, $def_composer($c));
63 16
                });
64 16
            },
65
            'unshiftComposers' => function(Engine $e, $def_composer) {
66
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
67
                    return array_merge($def_composer($c), $composed);
68
                });
69 16
            },
70
        ]);
71
72 16
        $this->register(new Extension\Data\DataExtension());
73 16
        $this->register(new Extension\Path\PathExtension());
74 16
        $this->register(new Extension\RenderContext\RenderContextExtension());
75 16
        $this->register(new Extension\LayoutSections\LayoutSectionsExtension());
76 16
        $this->register(new Extension\Folders\FoldersExtension());
77
78 16
        $this->addConfig($config);
79 16
    }
80
81
    /** @return string */
82 16
    public function render($template_name, array $data = [], array $attributes = []) {
83 16
        return $this->container->get('renderTemplate')->renderTemplate(new Template(
84 16
            $template_name,
85 16
            $data,
86 16
            $attributes
87
        ));
88
    }
89
90 16
    public function __call($method, array $args) {
91 16
        $methods = $this->container->get('engine_methods');
92 16
        if (isset($methods[$method])) {
93 16
            return $methods[$method]($this, ...$args);
94
        }
95
96
        throw new \BadMethodCallException("No method {$method} found for engine.");
97
    }
98
99 16
    public function register(Extension $extension) {
100 16
        $extension->register($this);
101 16
    }
102 16
    public function addMethods(array $methods) {
103 16
        $this->container->merge('engine_methods', $methods);
104 16
    }
105 16
    public function addConfig(array $config) {
106 16
        $this->container->merge('config', $config);
107 16
    }
108
109 16
    public function getContainer() {
110 16
        return $this->container;
111
    }
112
}
113