BladeCompiler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 65
ccs 14
cts 22
cp 0.6364
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 13 3
A render() 0 5 1
A initializeOutputBuffering() 0 7 1
A getRendered() 0 3 1
A absorbApplicationEnvironment() 0 5 1
1
<?php
2
3
namespace A17\Twill\Services\Blocks;
4
5
use Throwable;
6
use Illuminate\View\Factory;
7
use Illuminate\Support\Facades\Blade;
8
9
class BladeCompiler
10
{
11
    /**
12
     * @param $data
13
     * @return mixed
14
     */
15
    protected static function absorbApplicationEnvironment($data)
16
    {
17 5
        $data['__env'] = app(Factory::class);
18
19 5
        return $data;
20
    }
21 5
22
    /**
23
     * @param string $php
24
     * @param array $data
25
     * @throws \Throwable
26
     */
27
    protected static function compile(string $php, array $data)
28
    {
29 5
        $obLevel = self::initializeOutputBuffering();
30
31 5
        try {
32
            extract(self::absorbApplicationEnvironment($data), EXTR_SKIP);
33
34 5
            eval('?' . '>' . $php);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
35
        } catch (Throwable $e) {
36 5
            while (ob_get_level() > $obLevel) {
37
                ob_end_clean();
38
            }
39
            throw $e;
40
        }
41
    }
42
43
    /**
44
     * @return false|string
45
     */
46
    protected static function getRendered()
47
    {
48 5
        return ob_get_clean();
49
    }
50
51
    /**
52
     * @return int
53 5
     */
54
    protected static function initializeOutputBuffering()
55 5
    {
56
        $obLevel = ob_get_level();
57
58
        ob_start();
59
60
        return $obLevel;
61 5
    }
62
63 5
    /**
64
     * @param $string
65 5
     * @param $data
66
     * @return false|string
67 5
     * @throws \Throwable
68
     */
69
    public static function render($string, $data)
70
    {
71
        self::compile(Blade::compileString($string), $data);
72
73
        return self::getRendered();
74
    }
75
}
76