-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-filecombiner.php
299 lines (250 loc) · 7.62 KB
/
class-filecombiner.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* FileCombiner Class for PhP
*
* Combines files and makes them into a single file.
* Checks by means of modification date in source
* directory whether a file has changed and renews
* the target file.
*
* @version 1.0.0
* @author Pixelbart <[email protected]>
* @link https://github.com/pixelbart/filecombiner/
* @license MIT License (https://opensource.org/licenses/MIT)
*/
class FileCombiner
{
/**
* @var string
*/
private $_source;
/**
* @var string
*/
private $_target;
/**
* @var string
*/
private $_targetDir;
/**
* @var string
*/
private $_extension = '.js';
/**
* @var array
*/
public $extensions = ['.js', '.css', '.scss', '.less'];
/**
* @var string
*/
private $_targetCallback;
/**
* @param string $source The folder where all the files are.
* @param string $target The destination file where everything should be combined.
* @param string $extension The file extension to be respected.
*/
public function __construct(string $source, string $target, string $extension = '.js')
{
if (is_dir($source)) {
$this->_source = rtrim($source, '/');
}
if (!file_exists($target)) {
$this->createFiles($target, '');
}
$this->_target = $target;
$this->_targetDir = rtrim(dirname($target), '/');
if (in_array($extension, $this->extensions)) {
$this->_extension = $extension;
}
}
/**
* A function, or method, to be applied to the contents of the target file at the end.
* Is useful if you want to minify the file.
*
* @param string $callback The function/method to be executed.
*
* @return void
*/
public function setTargetCallback(string $callback)
{
$this->_targetCallback = $callback;
}
/**
* Checks the source directory and triggers processing if a file has changed.
*
* @return bool
*/
public function watch()
{
if (!$this->_source || !$this->_target || !$this->_targetDir) {
return false;
}
if (!is_dir($this->_source)) {
return false;
}
$lastCheckFile = sprintf('%s/_timestamp.txt', $this->_targetDir);
if (!file_exists($lastCheckFile)) {
$this->createFiles($lastCheckFile, '');
}
$lastcheck = $this->readFiles($lastCheckFile, '');
if (!is_numeric($lastcheck)) {
$lastcheck = 0;
}
$source = sprintf('%s/*%s', $this->_source, $this->_extension);
foreach (glob($source) as $filepath) {
if (filemtime($filepath) > $lastcheck) {
$this->createFiles($lastCheckFile, time());
return $this->handleSource();
}
}
return false;
}
/**
* Performs an update of the files.
*
* @return bool
*/
public function forceHandleSource()
{
$lastCheckFile = sprintf('%s/_timestamp.txt', $this->_targetDir);
if (!file_exists($lastCheckFile)) {
$this->createFiles($lastCheckFile, '');
}
$lastcheck = $this->readFiles($lastCheckFile, '');
if (!is_numeric($lastcheck)) {
$lastcheck = 0;
}
$this->createFiles($lastCheckFile, time());
return $this->handleSource();
}
/**
* Starts the process and combines all files.
*
* @return bool
*/
public function handleSource()
{
if (!$this->_source || !$this->_target || !$this->_targetDir) {
return false;
}
$content = '';
if (is_dir($this->_source)) {
foreach (glob($this->_source . '/*' . $this->_extension) as $filename) {
if (file_exists($filename)) {
$files[] = $filename;
}
}
$content = $this->readFiles($files, "\n\n");
}
if ($this->_targetCallback) {
$content = call_user_func($this->_targetCallback, $content);
}
$this->createFiles($this->_target, $content);
return true;
}
/**
* Adds the target file to the WordPress enqueue.
*
* @param string $name Enqueue name.
* @param array $supports Name of other enqueues to be loaded before.
* @param string $version The version of the file. With '' a uniqid() is generated and the file is not cached by the browser.
*
* @return void
*/
public function enqueue(string $name = 'combined', array $supports = [], string $version = '')
{
if (trim($version) === '') {
$version = uniqid();
}
if ($this->_extension === '.js') {
wp_enqueue_script($name, $this->toUrl($this->_target), $supports, $version, true);
}
if (in_array($this->_extension, ['.css', '.less', '.scss'])) {
wp_enqueue_style($name, $this->toUrl($this->_target), $supports, $version, false);
}
}
/**
* Reads one or more files.
*
* @param string|string[] $path The files to be read.
* @param string $sep Specifies how the individual contents are separated from each other.
*
* @return string
*/
public function readFiles($path, string $sep = "\n\n")
{
$results = [];
if (is_array($path)) {
foreach ($path as $p) {
if (file_exists($p)) {
$results[] = file_get_contents($p);
}
}
return implode($sep, $results);
}
if (file_exists($path)) {
$results[] = file_get_contents($path);
}
return implode($sep, $results);
}
/**
* Creates one or more files with content.
*
* @param string|string[] $path The files to be created.
* @param string $content The content of the files.
*
* @return void
*/
public function createFiles($path, string $content = '')
{
if (is_array($path)) {
foreach ($path as $p) {
file_put_contents($p, $content);
}
return;
}
file_put_contents($path, $content);
return;
}
/**
* Deletes one or more files.
*
* @param string|string[] $path The files to be deleted.
*
* @return void
*/
public function deleteFiles($path)
{
$results = [];
if (is_array($path)) {
foreach ($path as $p) {
if (file_exists($p)) {
$results[] = [
'file' => $p,
'deleted' => unlink($p),
];
}
}
return $results;
}
if (file_exists($path)) {
$results[] = [
'file' => $p,
'deleted' => unlink($path),
];
}
return $results;
}
/**
* Converts a path into a URL.
*
* @param string $file The path of the file.
* @param string $protocol The protocol of the URL.
*
* @return string
*/
public function toUrl(string $file, string $protocol = 'https://')
{
return esc_url($protocol . $_SERVER['HTTP_HOST'] . str_replace($_SERVER['DOCUMENT_ROOT'], '/', $file));
}
}