/
FilenameArray.php
189 lines (173 loc) · 3.6 KB
/
FilenameArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php namespace ProcessWire;
/**
* ProcessWire FilenameArray
*
* Manages array of filenames or file URLs, like for $config->scripts and $config->styles.
*
* ProcessWire 3.x, Copyright 2023 by Ryan Cramer
* https://processwire.com
*
*/
class FilenameArray extends Wire implements \IteratorAggregate, \Countable {
/**
* Array of filenames indexed by MD5 hash of filename
*
* @var array
*
*/
protected $data = array();
/**
* Add a file
*
* @param string $filename
* @return $this
*
*/
public function add($filename) {
$key = $this->getKey($filename);
$this->data[$key] = $filename;
return $this;
}
/**
* Get key for $filename that excludes query strings
*
* @param string $filename
* @return string
*
*/
protected function getKey($filename) {
$filename = (string) $filename;
$pos = strpos($filename, '?');
$key = $pos ? substr($filename, 0, $pos) : $filename;
return md5($key);
}
/**
* Prepend $filename to the beginning
*
* @param string $filename
* @return $this
*
*/
public function prepend($filename) {
$key = $this->getKey($filename);
$data = array($key => $filename);
foreach($this->data as $k => $v) {
if($k == $key) continue;
$data[$k] = $v;
}
$this->data = $data;
return $this;
}
/**
* Append $filename to the end
*
* @param string $filename
* @return FilenameArray
*
*/
public function append($filename) {
return $this->add($filename);
}
/**
* Make iterable
*
* @return \ArrayObject
*
*/
#[\ReturnTypeWillChange]
public function getIterator() {
return new \ArrayObject($this->data);
}
/**
* Get cache-busting URLs for this FilenameArray
*
* This is the same as iterating this FilenameArray except that it appends cache-busting
* query strings to the URLs that resolve to physical files.
*
* @param bool|null|string $useVersion See Config::versionUrls() for arument details
* @return array
* @throws WireException
* @see Config::versionUrls()
* @since 3.0.227
*
*/
public function urls($useVersion = null) {
return $this->wire()->config->versionUrls($this, $useVersion);
}
/**
* Make FilenameArray unique (deprecated)
*
* @deprecated no longer necessary since the add() function ensures uniqueness
* @return FilenameArray
*
*/
public function unique() {
// no longer necessary since the add() function ensures uniqueness
// $this->data = array_unique($this->data);
return $this;
}
/**
* Remove filename
*
* @param string $filename
* @return $this
*
*/
public function remove($filename) {
$key = $this->getKey($filename);
unset($this->data[$key]);
return $this;
}
/**
* Remove all filenames
*
* @return $this
*
*/
public function removeAll() {
$this->data = array();
return $this;
}
/**
* Replace one file with another
*
* @param string $oldFile
* @param string $newFile
* @return $this
* @since 3.0.215
*
*/
public function replace($oldFile, $newFile) {
$key = $this->getKey($oldFile);
if(isset($this->data[$key])) {
$this->data[$key] = $newFile;
} else {
$key = array_search($oldFile, $this->data);
if($key !== false) {
$this->data[$key] = $newFile;
} else {
$this->add($newFile);
}
}
return $this;
}
/**
* String value containing print_r() dump of all filenames
*
* @return string
*
*/
public function __toString() {
return print_r($this->data, true);
}
/**
* Return count of items in this FilenameArray
*
* @return int
*
*/
#[\ReturnTypeWillChange]
public function count() {
return count($this->data);
}
}