1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\LaravelTracy; |
4
|
|
|
|
5
|
|
|
class Template |
6
|
|
|
{ |
7
|
|
|
protected $attributes = []; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* setAttributes. |
11
|
|
|
* |
12
|
|
|
* @param array $attributes |
13
|
|
|
*/ |
14
|
2 |
|
public function setAttributes($attributes) |
15
|
|
|
{ |
16
|
2 |
|
$this->attributes = $attributes; |
17
|
2 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* render. |
21
|
|
|
* |
22
|
|
|
* @param string $view |
23
|
|
|
* |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
2 |
|
public function render($view) |
27
|
|
|
{ |
28
|
2 |
|
extract($this->attributes); |
29
|
|
|
|
30
|
2 |
|
ob_start(); |
31
|
2 |
|
require $view; |
32
|
|
|
|
33
|
2 |
|
return $this->minify(ob_get_clean()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* minify. |
38
|
|
|
* |
39
|
|
|
* @param string $buffer |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
2 |
|
protected function minify($buffer) |
44
|
|
|
{ |
45
|
|
|
$search = [ |
46
|
2 |
|
'/\>[^\S ]+/s', // strip whitespaces after tags, except space |
47
|
|
|
'/[^\S ]+\</s', // strip whitespaces before tags, except space |
48
|
|
|
'/(\s)+/s', // shorten multiple whitespace sequences |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$replace = [ |
52
|
2 |
|
'>', |
53
|
|
|
'<', |
54
|
|
|
'\\1', |
55
|
|
|
]; |
56
|
|
|
|
57
|
2 |
|
$buffer = preg_replace($search, $replace, $buffer); |
58
|
|
|
|
59
|
|
|
//replaces |
60
|
2 |
|
$buffer = str_replace(' type="text/javascript"', '', $buffer); |
61
|
2 |
|
$buffer = str_replace(' type="text/css"', '', $buffer); |
62
|
|
|
|
63
|
|
|
//replace style elements |
64
|
2 |
|
$buffer = preg_replace_callback("/<style>([\s\S]*?)<\/style>/", function ($matches) { |
65
|
|
|
//minify the css |
66
|
|
|
$css = $matches[1]; |
67
|
|
|
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css); |
68
|
|
|
|
69
|
|
|
$css = str_replace(["\r\n", "\r", "\n", "\t", ' ', ' ', ' '], '', $css); |
70
|
|
|
|
71
|
|
|
$css = preg_replace(['(( )+{)', '({( )+)'], '{', $css); |
72
|
|
|
$css = preg_replace(['(( )+})', '(}( )+)', '(;( )*})'], '}', $css); |
73
|
|
|
$css = preg_replace(['(;( )+)', '(( )+;)'], ';', $css); |
74
|
|
|
|
75
|
|
|
return '<style>'.$css.'</style>'; |
76
|
2 |
|
}, $buffer); |
77
|
|
|
|
78
|
|
|
/* |
|
|
|
|
79
|
|
|
//replace script elements |
80
|
|
|
$buffer = preg_replace_callback("/<script>([\s\S]*?)<\/script>/", function ($matches) { |
81
|
|
|
$minifiedCode = JSMin::minify($matches[1]); |
82
|
|
|
|
83
|
|
|
return '<script>'.$minifiedCode.'</script>'; |
84
|
|
|
}, $buffer); |
85
|
|
|
*/ |
86
|
|
|
|
87
|
2 |
|
return $buffer; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.