Completed
Push — master ( e6a217...ab75c5 )
by recca
03:02
created

Template::minify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
1
<?php
2
3
namespace Recca0120\LaravelTracy;
4
5
class Template
6
{
7
    /**
8
     * $attributes.
9
     *
10
     * @var array
11
     */
12
    protected $attributes = [];
13
14
    /**
15
     * $minify.
16
     *
17
     * @var bool
18
     */
19
    protected $minify = true;
20
21
    /**
22
     * setAttributes.
23
     *
24
     * @param array $attributes
25
     */
26 2
    public function setAttributes($attributes)
27
    {
28 2
        $this->attributes = $attributes;
29 2
    }
30
31
    /**
32
     * minify.
33
     *
34
     * @param bool $minify
35
     * @return $this
36
     */
37
    public function minify($minify)
38
    {
39
        $this->minify = $minify;
40
41
        return $this;
42
    }
43
44
    /**
45
     * render.
46
     *
47
     * @param string $view
48
     * @return string
49
     */
50 2
    public function render($view)
51
    {
52 2
        extract($this->attributes);
53
54 2
        ob_start();
55 2
        require $view;
56
57 2
        return $this->minify === true
58 2
            ? $this->min(ob_get_clean())
59 2
            : ob_get_clean();
60
    }
61
62
    /**
63
     * min.
64
     *
65
     * if need min style and script, refrence
66
     * https://gist.github.com/recca0120/5930842de4e0a43a48b8bf027ab058f9
67
     *
68
     * @param string $buffer
69
     * @return string
70
     */
71 2
    protected function min($buffer)
72
    {
73 2
        return preg_replace(
74 2
            ['/<!--(.*)-->/Uis', '/[[:blank:]]+/'],
75 2
            ['', ' '],
76 2
            str_replace(["\n", "\r", "\t"], '', $buffer)
77 2
        );
78
    }
79
}
80