1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Katten\Purge; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Sabberworm\CSS\CSSList\Document; |
9
|
|
|
use Sabberworm\CSS\RuleSet\DeclarationBlock; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class BlockHashTable { |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
private $hashTable; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
const IS_USED = 'isUsed'; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
const BLOCK_VALUE = 'block'; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct() { |
27
|
|
|
|
28
|
|
|
$this->hashTable = []; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Build a hash table of declaration blocks from |
34
|
|
|
* a given Document object |
35
|
|
|
* |
36
|
|
|
* @param Document $css |
37
|
|
|
*/ |
38
|
|
|
public function build(Document $css) { |
39
|
|
|
|
40
|
|
|
foreach ($css->getAllDeclarationBlocks() as $block) { |
41
|
|
|
$hash = $this->hashBlock($block); |
42
|
|
|
|
43
|
|
|
$this->hashTable[$hash] = [ self::BLOCK_VALUE => $block, self::IS_USED => false ]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Serialize the contents of the Declaration block and hash the result |
51
|
|
|
* |
52
|
|
|
* @param DeclarationBlock $block |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* An md5 hash representing the DeclarationBlock object |
56
|
|
|
*/ |
57
|
|
|
public function hashBlock(DeclarationBlock $block) { |
58
|
|
|
|
59
|
|
|
return md5(serialize($block)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the used flag for the given block at a given hash index |
66
|
|
|
* |
67
|
|
|
* @param string $hash |
68
|
|
|
* A hashed representation of a DeclarationBlock |
69
|
|
|
* |
70
|
|
|
* @param bool $value |
71
|
|
|
* The boolean value to set the flag as |
72
|
|
|
*/ |
73
|
|
|
public function setUsedFlag($hash, $value) { |
74
|
|
|
|
75
|
|
|
$this->hashTable[$hash][self::IS_USED] = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the flag value for the block at a given hash index |
82
|
|
|
* |
83
|
|
|
* @param string $hash |
84
|
|
|
* A hashed representation of a DeclarationBlock |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
* The value of the flag |
88
|
|
|
*/ |
89
|
|
|
public function getUsedFlag($hash) { |
90
|
|
|
|
91
|
|
|
return $this->hashTable[$hash][self::IS_USED]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the block at a given hash index |
98
|
|
|
* |
99
|
|
|
* @param string hash |
100
|
|
|
* A hashed representation of a DeclarationBlock |
101
|
|
|
* |
102
|
|
|
* @return DeclarationBlock |
103
|
|
|
* The declaration block at index $hash |
104
|
|
|
*/ |
105
|
|
|
public function getBlock($hash) { |
106
|
|
|
|
107
|
|
|
return $this->hashTable[$hash][self::BLOCK_VALUE]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set a hashTable value |
114
|
|
|
* |
115
|
|
|
* @param DeclarationBlock $block |
116
|
|
|
* |
117
|
|
|
* @param $value |
118
|
|
|
* A boolean flag to set IS_USED with |
119
|
|
|
*/ |
120
|
|
|
public function setBlock(DeclarationBlock $block, $value = false) { |
121
|
|
|
|
122
|
|
|
$hash = $this->hashBlock($block); |
123
|
|
|
|
124
|
|
|
$this->hashTable[$hash] = [ self::BLOCK_VALUE => $block, self::IS_USED => $value ]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return |
131
|
|
|
* Return a copy of the hashTable |
132
|
|
|
*/ |
133
|
|
|
public function getBlockCollection() { |
134
|
|
|
|
135
|
|
|
return $this->hashTable; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Determine if a block is in the hash table |
142
|
|
|
* |
143
|
|
|
* @param DeclarationBlock $block |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
* True if block is found |
147
|
|
|
*/ |
148
|
|
|
public function hasBlock(DeclarationBlock $block) { |
149
|
|
|
|
150
|
|
|
$hash = $this->hashBlock($block); |
151
|
|
|
|
152
|
|
|
return isset($this->hashTable[$hash]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
} |
156
|
|
|
|