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
|
28 |
|
public function __construct($items = []) |
22
|
|
|
{ |
23
|
28 |
|
$replacementHashLimit = 32; |
24
|
28 |
|
$this->replacementHash = str_limit(md5(time()), $replacementHashLimit); |
25
|
28 |
|
parent::__construct($items); |
26
|
28 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $contents |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
4 |
|
public function restorePlaceholders($contents) |
34
|
|
|
{ |
35
|
4 |
|
foreach ($this->all() as $placeholder => $original) { |
36
|
1 |
|
$contents = str_replace($placeholder, $original, $contents); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
return $contents; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Replace ```$value``` with a placeholder and store it in the container. |
44
|
|
|
* |
45
|
|
|
* @param string $value |
46
|
|
|
* @param string $content |
47
|
|
|
* |
48
|
|
|
* @return string $contents |
49
|
|
|
*/ |
50
|
3 |
|
public function addPlaceholder($value, $content) |
51
|
|
|
{ |
52
|
3 |
|
$placeholder = $this->createPlaceholder($value); |
53
|
|
|
|
54
|
3 |
|
return str_replace($value, $placeholder, $content); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a unique placeholder for the given contents. |
59
|
|
|
* |
60
|
|
|
* @param string $value |
61
|
|
|
* |
62
|
|
|
* @return string $placeholder |
|
|
|
|
63
|
|
|
*/ |
64
|
5 |
|
public function createPlaceholder($value) |
65
|
|
|
{ |
66
|
5 |
|
if (($key = array_search($value, $this->all()))) { |
67
|
|
|
$placeholder = $key; |
68
|
|
|
} else { |
69
|
5 |
|
$placeholder = $this->createUniquePlaceholder(); |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
$value = $this->removeNestedPlaceholders($value); |
73
|
5 |
|
$this->items[$placeholder] = $value; |
74
|
|
|
|
75
|
5 |
|
return $placeholder; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Calculate the byte size of the placeholders. |
80
|
|
|
* |
81
|
|
|
* @param string $contentsWithPlaceholders |
82
|
|
|
* @return int |
|
|
|
|
83
|
|
|
*/ |
84
|
4 |
|
public function getContentSize($contentsWithPlaceholders) |
85
|
|
|
{ |
86
|
|
|
$placeholderSize = $this->map(function ($value, $key) use (&$contentsWithPlaceholders){ |
87
|
1 |
|
$count = substr_count($contentsWithPlaceholders, $key); |
88
|
|
|
|
89
|
1 |
|
return strlen($key) * $count - strlen($value) * $count; |
90
|
4 |
|
})->sum(); |
91
|
|
|
|
92
|
4 |
|
return strlen($contentsWithPlaceholders) - $placeholderSize; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create an unique placeholder. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
5 |
|
protected function createUniquePlaceholder() |
101
|
|
|
{ |
102
|
5 |
|
return '[['.$this->replacementHash.$this->count().']]'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove nested placeholders so no nested placholders remain in the original contents. |
107
|
|
|
* |
108
|
|
|
* @param string $originalContent |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function removeNestedPlaceholders($originalContent) |
113
|
|
|
{ |
114
|
5 |
|
return preg_replace_callback('/'.Constants::PLACEHOLDER_PATTERN.'/', function ($match) { |
115
|
1 |
|
return $this->pull($match[0]); |
116
|
5 |
|
}, $originalContent); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.