PhpEngine   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A type() 0 4 1
A renderTemplate() 0 15 1
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