1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArjanSchouten\HtmlMinifier; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class PlaceholderContainer extends Collection |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Hash used in placeholders. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $replacementHash; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a new placeholdercontainer. |
18
|
|
|
* |
19
|
|
|
* @param array $items |
20
|
|
|
*/ |
21
|
24 |
|
public function __construct($items = []) |
22
|
|
|
{ |
23
|
24 |
|
$replacementHashLimit = 32; |
24
|
24 |
|
$this->replacementHash = str_limit(md5(time()), $replacementHashLimit); |
25
|
24 |
|
parent::__construct($items); |
26
|
24 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $contents |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
3 |
|
public function restorePlaceholders($contents) |
34
|
|
|
{ |
35
|
3 |
|
foreach ($this->all() as $placeholder => $original) { |
36
|
|
|
$contents = str_replace($placeholder, $original, $contents); |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
return $contents; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Store a placeholder in the container. |
44
|
|
|
* |
45
|
|
|
* @param string $originalContent |
46
|
|
|
* |
47
|
|
|
* @return string $placeholder |
|
|
|
|
48
|
|
|
*/ |
49
|
1 |
|
public function addPlaceholder($originalContent) |
50
|
|
|
{ |
51
|
1 |
|
if (($key = array_search($originalContent, $this->all()))) { |
52
|
|
|
$placeholder = $key; |
53
|
|
|
} else { |
54
|
1 |
|
$placeholder = $this->createUniquePlaceholder(); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$originalContent = $this->removeNestedPlaceholders($originalContent); |
58
|
1 |
|
$this->items[$placeholder] = $originalContent; |
59
|
|
|
|
60
|
1 |
|
return $placeholder; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create an unique placeholder. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
1 |
|
protected function createUniquePlaceholder() |
69
|
|
|
{ |
70
|
1 |
|
return '[['.$this->replacementHash.$this->count().']]'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove nested placeholders so no nested placholders remain in the original contents. |
75
|
|
|
* |
76
|
|
|
* @param string $originalContent |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
protected function removeNestedPlaceholders($originalContent) |
81
|
|
|
{ |
82
|
|
|
return preg_replace_callback('/'.Constants::PLACEHOLDER_PATTERN.'/', function ($match) { |
83
|
|
|
return $this->get($match[0]); |
84
|
1 |
|
}, $originalContent); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
public function getPlaceholderSize() |
88
|
|
|
{ |
89
|
|
|
return $this->keys()->sum(function ($key) { |
90
|
|
|
return mb_strlen($key, '8bit'); |
91
|
3 |
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getOriginalSize() |
95
|
|
|
{ |
96
|
3 |
|
return $this->values()->sum(function ($value) { |
97
|
|
|
return mb_strlen($value, '8bit'); |
98
|
3 |
|
}); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.