Skip to content

Commit addd888

Browse files
committed
reset
1 parent 27c999a commit addd888

19 files changed

+90
-124
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ composer.lock
44
phpunit.xml
55
vendor
66
.phpunit.result.cache
7+
.DS_Store
8+
.php-cs-fixer.*

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"phpunit/phpunit": "^8.5 || ^9.0",
2424
"orchestra/testbench": "^4.0 || ^5.0 || ^6.0 || ^7.0",
2525
"squizlabs/php_codesniffer": "^3.6",
26-
"mockery/mockery": "^1.4"
26+
"mockery/mockery": "^1.4",
27+
"friendsofphp/php-cs-fixer": "^3.49"
2728
},
2829
"autoload": {
2930
"psr-4": {

config/laravel-page-speed.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,6 @@
5454
'*.iso',
5555
'*.flv',
5656
'*.m4v',
57-
'*.torrent'
58-
],
59-
60-
/*
61-
|--------------------------------------------------------------------------
62-
| Skip Custom Domain
63-
|--------------------------------------------------------------------------
64-
|
65-
| Skip Custom Domain to exclude.
66-
|
67-
*/
68-
69-
'domain' => [
70-
// for eg: str_replace(['https://', 'http://'], ['', ''], env('FILAMENT_DOMAIN'))
57+
'*.torrent',
7158
],
7259
];

src/Middleware/CollapseWhitespace.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@
22

33
namespace RenatoMarinho\LaravelPageSpeed\Middleware;
44

5-
use Exception;
6-
75
class CollapseWhitespace extends PageSpeed
86
{
97
public function apply($buffer)
108
{
11-
12-
if (str_contains($buffer, 'Error:')) {
13-
return $buffer;
14-
}
15-
16-
if (in_array(request()?->host(), config('laravel-page-speed.domain'), true)) {
17-
return $this->replace([], $this->removeComments($buffer));
18-
}
19-
209
$replace = [
2110
"/\n([\S])/" => '$1',
22-
"/\r/" => '',
23-
"/\n/" => '',
24-
"/\t/" => '',
25-
'/ >/' => '>',
26-
'/ +/' => ' ',
27-
'/> +</' => '><',
11+
"/\r/" => '',
12+
"/\n/" => '',
13+
"/\t/" => '',
14+
'/ +/' => ' ',
15+
'/> +</' => '><',
2816
];
2917

3018
return $this->replace($replace, $this->removeComments($buffer));

src/Middleware/ElideAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ElideAttributes extends PageSpeed
77
public function apply($buffer)
88
{
99
$replace = [
10-
'/ method=("get"|get)/' => '',
10+
'/ method=("get"|get)/' => '',
1111
'/ disabled=[^ >]*(.*?)/' => ' disabled',
1212
'/ selected=[^ >]*(.*?)/' => ' selected',
1313
];

src/Middleware/InlineCss.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
class InlineCss extends PageSpeed
66
{
7-
private $html = '';
8-
private $class = [];
9-
private $style = [];
7+
private $html = '';
8+
private $class = [];
9+
private $style = [];
1010
private $inline = [];
1111

1212
public function apply($buffer)
@@ -21,8 +21,7 @@ public function apply($buffer)
2121
);
2222

2323
$this->class = collect($matches[1])->mapWithKeys(function ($item) {
24-
25-
return [ 'page_speed_'.rand() => $item[0] ];
24+
return ['page_speed_' . rand() => $item[0]];
2625
})->unique();
2726

2827
return $this->injectStyle()->injectClass()->fixHTML()->html;
@@ -31,18 +30,17 @@ public function apply($buffer)
3130
private function injectStyle()
3231
{
3332
collect($this->class)->each(function ($attributes, $class) {
34-
3533
$this->inline[] = ".{$class}{ {$attributes} }";
3634

3735
$this->style[] = [
38-
'class' => $class,
36+
'class' => $class,
3937
'attributes' => preg_quote($attributes, '/')];
4038
});
4139

4240
$injectStyle = implode(' ', $this->inline);
4341

4442
$replace = [
45-
'#</head>(.*?)#' => "<style>{$injectStyle}</style></head>"
43+
'#</head>(.*?)#' => "\n<style>{$injectStyle}</style>\n</head>",
4644
];
4745

4846
$this->html = $this->replace($replace, $this->html);
@@ -54,7 +52,7 @@ private function injectClass()
5452
{
5553
collect($this->style)->each(function ($item) {
5654
$replace = [
57-
'/style="'.$item['attributes'].'"/' => "class=\"{$item['class']}\"",
55+
'/style="' . $item['attributes'] . '"/' => "class=\"{$item['class']}\"",
5856
];
5957

6058
$this->html = $this->replace($replace, $this->html);
@@ -66,18 +64,18 @@ private function injectClass()
6664
private function fixHTML()
6765
{
6866
$newHTML = [];
69-
$tmp = explode('<', $this->html);
67+
$tmp = explode('<', $this->html);
7068

7169
$replaceClass = [
72-
'/class="(.*?)"/' => "",
70+
'/class="(.*?)"/' => '',
7371
];
7472

7573
foreach ($tmp as $value) {
7674
preg_match_all('/class="(.*?)"/', $value, $matches);
7775

7876
if (count($matches[1]) > 1) {
7977
$replace = [
80-
'/>/' => "class=\"".implode(' ', $matches[1])."\">",
78+
'/>/' => 'class="' . implode(' ', $matches[1]) . '">',
8179
];
8280

8381
$newHTML[] = str_replace(

src/Middleware/InsertDNSPrefetch.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,17 @@ public function apply($buffer)
1414
);
1515

1616
$dnsPrefetch = collect($matches[0])->map(function ($item) {
17-
18-
if (str_contains($item[0], 'debugbar')) {
19-
return;
20-
}
21-
2217
$domain = (new TrimUrls)->apply($item[0]);
2318
$domain = explode(
2419
'/',
2520
str_replace('//', '', $domain)
2621
);
27-
28-
29-
if (str_contains(@$domain[0], 'www.schema.org')) {
30-
$domain[0] = 'www.schema.org';
31-
}
3222

33-
return "<link rel=\"preconnect\" href=\"//{$domain[0]}\" crossorigin><link rel=\"dns-prefetch\" href=\"//{$domain[0]}\">";
34-
})->unique()->implode('');
23+
return "<link rel=\"dns-prefetch\" href=\"//{$domain[0]}\">";
24+
})->unique()->implode("\n");
3525

3626
$replace = [
37-
'#<head>(.*?)#' => "<head>{$dnsPrefetch}",
27+
'#<head>(.*?)#' => "<head>\n{$dnsPrefetch}",
3828
];
3929

4030
return $this->replace($replace, $buffer);

src/Middleware/PageSpeed.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use Closure;
66
use RenatoMarinho\LaravelPageSpeed\Entities\HtmlSpecs;
7-
use Symfony\Component\HttpFoundation\StreamedResponse;
87
use Symfony\Component\HttpFoundation\BinaryFileResponse;
8+
use Symfony\Component\HttpFoundation\StreamedResponse;
99

1010
abstract class PageSpeed
1111
{
@@ -23,7 +23,7 @@ abstract public function apply($buffer);
2323
* Handle an incoming request.
2424
*
2525
* @param \Illuminate\Http\Request $request
26-
* @param \Closure $next
26+
* @param Closure $next
2727
* @return \Illuminate\Http\Response $response
2828
*/
2929
public function handle($request, Closure $next)
@@ -34,7 +34,7 @@ public function handle($request, Closure $next)
3434
return $response;
3535
}
3636

37-
$html = $response->getContent();
37+
$html = $response->getContent();
3838
$newContent = $this->apply($html);
3939

4040
return $response->setContent($newContent);
@@ -53,7 +53,7 @@ protected function replace(array $replace, $buffer)
5353
}
5454

5555
/**
56-
* Check Laravel Page Speed is enabled or not
56+
* Check Laravel Page Speed is enabled or not.
5757
*
5858
* @return bool
5959
*/
@@ -69,7 +69,7 @@ protected function isEnable()
6969
}
7070

7171
/**
72-
* Should Process
72+
* Should Process.
7373
*
7474
* @param \Illuminate\Http\Request $request
7575
* @param \Illuminate\Http\Response $response
@@ -92,7 +92,7 @@ protected function shouldProcessPageSpeed($request, $response)
9292
$patterns = config('laravel-page-speed.skip', []);
9393

9494
foreach ($patterns as $pattern) {
95-
if ($request->is($pattern)||$request->has($pattern)) {
95+
if ($request->is($pattern)) {
9696
return false;
9797
}
9898
}
@@ -101,7 +101,7 @@ protected function shouldProcessPageSpeed($request, $response)
101101
}
102102

103103
/**
104-
* Match all occurrences of the html tags given
104+
* Match all occurrences of the html tags given.
105105
*
106106
* @param array $tags Html tags to match in the given buffer
107107
* @param string $buffer Middleware response buffer
@@ -110,7 +110,7 @@ protected function shouldProcessPageSpeed($request, $response)
110110
*/
111111
protected function matchAllHtmlTag(array $tags, string $buffer): array
112112
{
113-
$voidTags = array_intersect($tags, HtmlSpecs::voidElements());
113+
$voidTags = array_intersect($tags, HtmlSpecs::voidElements());
114114
$normalTags = array_diff($tags, $voidTags);
115115

116116
return array_merge(
@@ -133,7 +133,7 @@ protected function matchTags(array $tags, string $pattern, string $buffer): arra
133133
}
134134

135135
/**
136-
* Replace occurrences of regex pattern inside of given HTML tags
136+
* Replace occurrences of regex pattern inside of given HTML tags.
137137
*
138138
* @param array $tags Html tags to match and run regex to replace occurrences
139139
* @param string $regex Regex rule to match on the given HTML tags
@@ -148,7 +148,7 @@ protected function replaceInsideHtmlTags(array $tags, string $regex, string $rep
148148
preg_match_all($regex, $tagMatched, $contentsMatched);
149149

150150
$tagAfterReplace = str_replace($contentsMatched[0], $replace, $tagMatched);
151-
$buffer = str_replace($tagMatched, $tagAfterReplace, $buffer);
151+
$buffer = str_replace($tagMatched, $tagAfterReplace, $buffer);
152152
}
153153

154154
return $buffer;

src/Middleware/RemoveComments.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class RemoveComments extends PageSpeed
66
{
7-
const REGEX_MATCH_JS_AND_CSS_COMMENTS = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\"|[integrity])\/\/.*))/';
8-
const REGEX_MATCH_HTML_COMMENTS = '/<!--[^]><!\[](.*?)[^\]]-->/s';
7+
const REGEX_MATCH_JS_AND_CSS_COMMENTS = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/';
8+
const REGEX_MATCH_HTML_COMMENTS = '/<!--[^]><!\[](.*?)[^\]]-->/s';
99

1010
public function apply($buffer)
1111
{

src/Middleware/RemoveQuotes.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ public function apply($buffer)
1111
$buffer = $this->replaceInsideHtmlTags(HtmlSpecs::voidElements(), '/\/>/', '>', $buffer);
1212

1313
$replace = [
14-
'/ src="(.\S*?)"/' => ' src=$1',
15-
'/ width="(.\S*?)"/' => ' width=$1',
16-
'/ height="(.\S*?)"/' => ' height=$1',
17-
'/ name="(.\S*?)"/' => ' name=$1',
18-
'/ charset="(.\S*?)"/' => ' charset=$1',
19-
'/ align="(.\S*?)"/' => ' align=$1',
20-
'/ border="(.\S*?)"/' => ' border=$1',
14+
'/ src="(.\S*?)"/' => ' src=$1',
15+
'/ width="(.\S*?)"/' => ' width=$1',
16+
'/ height="(.\S*?)"/' => ' height=$1',
17+
'/ name="(.\S*?)"/' => ' name=$1',
18+
'/ charset="(.\S*?)"/' => ' charset=$1',
19+
'/ align="(.\S*?)"/' => ' align=$1',
20+
'/ border="(.\S*?)"/' => ' border=$1',
2121
'/ crossorigin="(.\S*?)"/' => ' crossorigin=$1',
22-
'/ type="(.\S*?)"/' => ' type=$1',
22+
'/ type="(.\S*?)"/' => ' type=$1',
2323
];
2424

2525
return $this->replace($replace, $buffer);

0 commit comments

Comments
 (0)