PhpEngine::type()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Charcoal\View\Php;
6
7
// From 'charcoal-view'
8
use Charcoal\View\AbstractEngine;
9
10
/**
11
 * PHP view rendering engine
12
 */
13
class PhpEngine extends AbstractEngine
14
{
15
    /**
16
     * @return string
17
     */
18
    public function type(): string
19
    {
20
        return 'php';
21
    }
22
23
    /**
24
     * @param string $templateString The template string to render.
25
     * @param mixed  $context        The rendering context.
26
     * @return string The rendered template string.
27
     */
28
    public function renderTemplate(string $templateString, $context): string
29
    {
30
        $arrayContext = json_decode(json_encode($context), true);
31
        // Prevents leaking global variable by forcing anonymous scope
32
        $render = function($templateString, array $context) {
33
            extract($context);
34
            return eval('?>'.$templateString);
35
        };
36
37
        ob_start();
38
        $render($templateString, $arrayContext);
39
        $output = ob_get_clean();
40
41
        return $output;
42
    }
43
}
44