BladeCompiler::initializeOutputBuffering()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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