FileUtils::zip()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
rs 8.0555
cc 9
nc 7
nop 2
1
<?php
2
3
namespace Alpha\Util\File;
4
5
use Alpha\Exception\IllegalArguementException;
6
use Alpha\Exception\AlphaException;
7
use DirectoryIterator;
8
use ZipArchive;
9
use RecursiveIteratorIterator;
10
use RecursiveDirectoryIterator;
11
12
/**
13
 * A utility class for carrying out various file system tasks.
14
 *
15
 * @since 1.0
16
 *
17
 * @author John Collins <[email protected]>
18
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
19
 * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
20
 * All rights reserved.
21
 *
22
 * <pre>
23
 * Redistribution and use in source and binary forms, with or
24
 * without modification, are permitted provided that the
25
 * following conditions are met:
26
 *
27
 * * Redistributions of source code must retain the above
28
 *   copyright notice, this list of conditions and the
29
 *   following disclaimer.
30
 * * Redistributions in binary form must reproduce the above
31
 *   copyright notice, this list of conditions and the
32
 *   following disclaimer in the documentation and/or other
33
 *   materials provided with the distribution.
34
 * * Neither the name of the Alpha Framework nor the names
35
 *   of its contributors may be used to endorse or promote
36
 *   products derived from this software without specific
37
 *   prior written permission.
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
44
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
50
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
51
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52
 * </pre>
53
 */
54
class FileUtils
55
{
56
    /**
57
     * A hash array for mapping file extensions to MIME types.
58
     *
59
     * @var array
60
     *
61
     * @since 1.0
62
     */
63
    private static $extensionToMIMEMappings = array(
64
        '3dm' => 'x-world/x-3dmf',
65
        '3dmf' => 'x-world/x-3dmf',
66
        'a' => 'application/octet-stream',
67
        'aab' => 'application/x-authorware-bin',
68
        'aam' => 'application/x-authorware-map',
69
        'aas' => 'application/x-authorware-seg',
70
        'abc' => 'text/vnd.abc',
71
        'acgi' => 'text/html',
72
        'afl' => 'video/animaflex',
73
        'ai' => 'application/postscript',
74
        'aif' => 'audio/aiff',
75
        'aifc' => 'audio/aiff',
76
        'aiff' => 'audio/aiff',
77
        'aim' => 'application/x-aim',
78
        'aip' => 'text/x-audiosoft-intra',
79
        'ani' => 'application/x-navi-animation',
80
        'aos' => 'application/x-nokia-9000-communicator-add-on-software',
81
        'aps' => 'application/mime',
82
        'arc' => 'application/octet-stream',
83
        'arj' => 'application/arj',
84
        'art' => 'image/x-jg',
85
        'asf' => 'video/x-ms-asf',
86
        'asm' => 'text/x-asm',
87
        'asp' => 'text/asp',
88
        'asx' => 'application/x-mplayer2',
89
        'au' => 'audio/basic',
90
        'avi' => 'application/x-troff-msvideo',
91
        'avs' => 'video/avs-video',
92
        'bcpio' => 'application/x-bcpio',
93
        'bin' => 'application/octet-stream',
94
        'bm' => 'image/bmp',
95
        'bmp' => 'image/bmp',
96
        'boo' => 'application/book',
97
        'book' => 'application/book',
98
        'boz' => 'application/x-bzip2',
99
        'bsh' => 'application/x-bsh',
100
        'bz' => 'application/x-bzip',
101
        'bz2' => 'application/x-bzip2',
102
        'c' => 'text/plain',
103
        'c++' => 'text/plain',
104
        'cat' => 'application/vnd.ms-pki.seccat',
105
        'cc' => 'text/plain',
106
        'ccad' => 'application/clariscad',
107
        'cco' => 'application/x-cocoa',
108
        'cdf' => 'application/cdf',
109
        'cer' => 'application/pkix-cert',
110
        'cha' => 'application/x-chat',
111
        'chat' => 'application/x-chat',
112
        'class' => 'application/java',
113
        'com' => 'application/octet-stream',
114
        'conf' => 'text/plain',
115
        'cpio' => 'application/x-cpio',
116
        'cpp' => 'text/x-c',
117
        'cpt' => 'application/mac-compactpro',
118
        'crl' => 'application/pkcs-crl',
119
        'crt' => 'application/pkix-cert',
120
        'csh' => 'application/x-csh',
121
        'css' => 'application/x-pointplus',
122
        'cxx' => 'text/plain',
123
        'dcr' => 'application/x-director',
124
        'deepv' => 'application/x-deepv',
125
        'def' => 'text/plain',
126
        'der' => 'application/x-x509-ca-cert',
127
        'dif' => 'video/x-dv',
128
        'dir' => 'application/x-director',
129
        'dl' => 'video/dl',
130
        'doc' => 'application/msword',
131
        'dot' => 'application/msword',
132
        'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
133
        'dp' => 'application/commonground',
134
        'drw' => 'application/drafting',
135
        'dump' => 'application/octet-stream',
136
        'dv' => 'video/x-dv',
137
        'dvi' => 'application/x-dvi',
138
        'dwf' => 'drawing/x-dwf (old)',
139
        'dwg' => 'application/acad',
140
        'dxf' => 'application/dxf',
141
        'dxr' => 'application/x-director',
142
        'el' => 'text/x-script.elisp',
143
        'elc' => 'application/x-bytecode.elisp (compiled elisp)',
144
        'env' => 'application/x-envoy',
145
        'eps' => 'application/postscript',
146
        'es' => 'application/x-esrehber',
147
        'etx' => 'text/x-setext',
148
        'evy' => 'application/envoy',
149
        'exe' => 'application/octet-stream',
150
        'f' => 'text/plain',
151
        'f77' => 'text/x-fortran',
152
        'f90' => 'text/plain',
153
        'fdf' => 'application/vnd.fdf',
154
        'fif' => 'application/fractals',
155
        'fli' => 'video/fli',
156
        'flo' => 'image/florian',
157
        'flx' => 'text/vnd.fmi.flexstor',
158
        'fmf' => 'video/x-atomic3d-feature',
159
        'for' => 'text/plain',
160
        'fpx' => 'image/vnd.fpx',
161
        'frl' => 'application/freeloader',
162
        'funk' => 'audio/make',
163
        'g' => 'text/plain',
164
        'g3' => 'image/g3fax',
165
        'gif' => 'image/gif',
166
        'gl' => 'video/gl',
167
        'gsd' => 'audio/x-gsm',
168
        'gsm' => 'audio/x-gsm',
169
        'gsp' => 'application/x-gsp',
170
        'gss' => 'application/x-gss',
171
        'gtar' => 'application/x-gtar',
172
        'gz' => 'application/x-compressed',
173
        'gzip' => 'application/x-gzip',
174
        'h' => 'text/plain',
175
        'hdf' => 'application/x-hdf',
176
        'help' => 'application/x-helpfile',
177
        'hgl' => 'application/vnd.hp-hpgl',
178
        'hh' => 'text/plain',
179
        'hlb' => 'text/x-script',
180
        'hlp' => 'application/hlp',
181
        'hpg' => 'application/vnd.hp-hpgl',
182
        'hpgl' => 'application/vnd.hp-hpgl',
183
        'hqx' => 'application/binhex',
184
        'hta' => 'application/hta',
185
        'htc' => 'text/x-component',
186
        'htm' => 'text/html',
187
        'html' => 'text/html',
188
        'htmls' => 'text/html',
189
        'htt' => 'text/webviewhtml',
190
        'htx' => 'text/html',
191
        'ice' => 'x-conference/x-cooltalk',
192
        'ico' => 'image/x-icon',
193
        'idc' => 'text/plain',
194
        'ief' => 'image/ief',
195
        'iefs' => 'image/ief',
196
        'iges' => 'application/iges',
197
        'igs' => 'application/iges',
198
        'ima' => 'application/x-ima',
199
        'imap' => 'application/x-httpd-imap',
200
        'inf' => 'application/inf',
201
        'ins' => 'application/x-internett-signup',
202
        'ip' => 'application/x-ip2',
203
        'isu' => 'video/x-isvideo',
204
        'it' => 'audio/it',
205
        'iv' => 'application/x-inventor',
206
        'ivr' => 'i-world/i-vrml',
207
        'ivy' => 'application/x-livescreen',
208
        'jam' => 'audio/x-jam',
209
        'jav' => 'text/plain',
210
        'java' => 'text/plain',
211
        'jcm' => 'application/x-java-commerce',
212
        'jfif' => 'image/jpeg',
213
        'jfif-tbnl' => 'image/jpeg',
214
        'jpe' => 'image/jpeg',
215
        'jpeg' => 'image/jpeg',
216
        'jpg' => 'image/jpeg',
217
        'jps' => 'image/x-jps',
218
        'js' => 'application/x-javascript',
219
        'jut' => 'image/jutvision',
220
        'kar' => 'audio/midi',
221
        'ksh' => 'application/x-ksh',
222
        'la' => 'audio/nspaudio',
223
        'lam' => 'audio/x-liveaudio',
224
        'latex' => 'application/x-latex',
225
        'lha' => 'application/lha',
226
        'lhx' => 'application/octet-stream',
227
        'list' => 'text/plain',
228
        'lma' => 'audio/nspaudio',
229
        'log' => 'text/plain',
230
        'lsp' => 'application/x-lisp',
231
        'lst' => 'text/plain',
232
        'lsx' => 'text/x-la-asf',
233
        'ltx' => 'application/x-latex',
234
        'lzh' => 'application/octet-stream',
235
        'lzx' => 'application/lzx',
236
        'm' => 'text/plain',
237
        'm1v' => 'video/mpeg',
238
        'm2a' => 'audio/mpeg',
239
        'm2v' => 'video/mpeg',
240
        'm3u' => 'audio/x-mpequrl',
241
        'man' => 'application/x-troff-man',
242
        'map' => 'application/x-navimap',
243
        'mar' => 'text/plain',
244
        'mbd' => 'application/mbedlet',
245
        'mc$' => 'application/x-magic-cap-package-1.0',
246
        'mcd' => 'application/mcad',
247
        'mcf' => 'image/vasa',
248
        'mcp' => 'application/netmc',
249
        'me' => 'application/x-troff-me',
250
        'mht' => 'message/rfc822',
251
        'mhtml' => 'message/rfc822',
252
        'mid' => 'application/x-midi',
253
        'midi' => 'application/x-midi',
254
        'mif' => 'application/x-frame',
255
        'mime' => 'message/rfc822',
256
        'mjf' => 'audio/x-vnd.audioexplosion.mjuicemediafile',
257
        'mjpg' => 'video/x-motion-jpeg',
258
        'mm' => 'application/base64',
259
        'mme' => 'application/base64',
260
        'mod' => 'audio/mod',
261
        'moov' => 'video/quicktime',
262
        'mov' => 'video/quicktime',
263
        'movie' => 'video/x-sgi-movie',
264
        'mp2' => 'audio/mpeg',
265
        'mp3' => 'audio/mpeg3',
266
        'mpa' => 'audio/mpeg',
267
        'mpc' => 'application/x-project',
268
        'mpe' => 'video/mpeg',
269
        'mpeg' => 'video/mpeg',
270
        'mpg' => 'audio/mpeg',
271
        'mpga' => 'audio/mpeg',
272
        'mpp' => 'application/vnd.ms-project',
273
        'mpt' => 'application/x-project',
274
        'mpv' => 'application/x-project',
275
        'mpx' => 'application/x-project',
276
        'mrc' => 'application/marc',
277
        'ms' => 'application/x-troff-ms',
278
        'mv' => 'video/x-sgi-movie',
279
        'my' => 'audio/make',
280
        'mzz' => 'application/x-vnd.audioexplosion.mzz',
281
        'nap' => 'image/naplps',
282
        'naplps' => 'image/naplps',
283
        'nc' => 'application/x-netcdf',
284
        'ncm' => 'application/vnd.nokia.configuration-message',
285
        'nif' => 'image/x-niff',
286
        'niff' => 'image/x-niff',
287
        'nix' => 'application/x-mix-transfer',
288
        'nsc' => 'application/x-conference',
289
        'nvd' => 'application/x-navidoc',
290
        'o' => 'application/octet-stream',
291
        'oda' => 'application/oda',
292
        'omc' => 'application/x-omc',
293
        'omcd' => 'application/x-omcdatamaker',
294
        'omcr' => 'application/x-omcregerator',
295
        'p' => 'text/x-pascal',
296
        'p10' => 'application/pkcs10',
297
        'p12' => 'application/pkcs-12',
298
        'p7a' => 'application/x-pkcs7-signature',
299
        'p7c' => 'application/pkcs7-mime',
300
        'p7m' => 'application/pkcs7-mime',
301
        'p7r' => 'application/x-pkcs7-certreqresp',
302
        'p7s' => 'application/pkcs7-signature',
303
        'part' => 'application/pro_eng',
304
        'pas' => 'text/pascal',
305
        'pbm' => 'image/x-portable-bitmap',
306
        'pcl' => 'application/vnd.hp-pcl',
307
        'pct' => 'image/x-pict',
308
        'pcx' => 'image/x-pcx',
309
        'pdb' => 'chemical/x-pdb',
310
        'pdf' => 'application/pdf',
311
        'pfunk' => 'audio/make',
312
        'pgm' => 'image/x-portable-graymap',
313
        'pic' => 'image/pict',
314
        'pict' => 'image/pict',
315
        'pkg' => 'application/x-newton-compatible-pkg',
316
        'pko' => 'application/vnd.ms-pki.pko',
317
        'pl' => 'text/plain',
318
        'plx' => 'application/x-pixclscript',
319
        'pm' => 'image/x-xpixmap',
320
        'pm4' => 'application/x-pagemaker',
321
        'pm5' => 'application/x-pagemaker',
322
        'png' => 'image/png',
323
        'pnm' => 'application/x-portable-anymap',
324
        'pot' => 'application/mspowerpoint',
325
        'pov' => 'model/x-pov',
326
        'ppa' => 'application/vnd.ms-powerpoint',
327
        'ppm' => 'image/x-portable-pixmap',
328
        'pps' => 'application/mspowerpoint',
329
        'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
330
        'ppt' => 'application/mspowerpoint',
331
        'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
332
        'ppz' => 'application/mspowerpoint',
333
        'pre' => 'application/x-freelance',
334
        'prt' => 'application/pro_eng',
335
        'ps' => 'application/postscript',
336
        'psd' => 'application/octet-stream',
337
        'pvu' => 'paleovu/x-pv',
338
        'pwz' => 'application/vnd.ms-powerpoint',
339
        'py' => 'text/x-script.phyton',
340
        'pyc' => 'applicaiton/x-bytecode.python',
341
        'qcp' => 'audio/vnd.qcelp',
342
        'qd3' => 'x-world/x-3dmf',
343
        'qd3d' => 'x-world/x-3dmf',
344
        'qif' => 'image/x-quicktime',
345
        'qt' => 'video/quicktime',
346
        'qtc' => 'video/x-qtc',
347
        'qti' => 'image/x-quicktime',
348
        'qtif' => 'image/x-quicktime',
349
        'ra' => 'audio/x-pn-realaudio',
350
        'ram' => 'audio/x-pn-realaudio',
351
        'ras' => 'application/x-cmu-raster',
352
        'rast' => 'image/cmu-raster',
353
        'rexx' => 'text/x-script.rexx',
354
        'rf' => 'image/vnd.rn-realflash',
355
        'rgb' => 'image/x-rgb',
356
        'rm' => 'application/vnd.rn-realmedia',
357
        'rmi' => 'audio/mid',
358
        'rmm' => 'audio/x-pn-realaudio',
359
        'rmp' => 'audio/x-pn-realaudio',
360
        'rng' => 'application/ringing-tones',
361
        'rnx' => 'application/vnd.rn-realplayer',
362
        'roff' => 'application/x-troff',
363
        'rp' => 'image/vnd.rn-realpix',
364
        'rpm' => 'audio/x-pn-realaudio-plugin',
365
        'rt' => 'text/richtext',
366
        'rtf' => 'application/rtf',
367
        'rtx' => 'application/rtf',
368
        'rv' => 'video/vnd.rn-realvideo',
369
        's' => 'text/x-asm',
370
        's3m' => 'audio/s3m',
371
        'saveme' => 'application/octet-stream',
372
        'sbk' => 'application/x-tbook',
373
        'scm' => 'application/x-lotusscreencam',
374
        'sdml' => 'text/plain',
375
        'sdp' => 'application/sdp',
376
        'sdr' => 'application/sounder',
377
        'sea' => 'application/sea',
378
        'set' => 'application/set',
379
        'sgm' => 'text/sgml',
380
        'sgml' => 'text/sgml',
381
        'sh' => 'application/x-bsh',
382
        'shar' => 'application/x-bsh',
383
        'shtml' => 'text/html',
384
        'sid' => 'audio/x-psid',
385
        'sit' => 'application/x-sit',
386
        'skd' => 'application/x-koan',
387
        'skm' => 'application/x-koan',
388
        'skp' => 'application/x-koan',
389
        'skt' => 'application/x-koan',
390
        'sl' => 'application/x-seelogo',
391
        'smi' => 'application/smil',
392
        'smil' => 'application/smil',
393
        'snd' => 'audio/basic',
394
        'sol' => 'application/solids',
395
        'spc' => 'application/x-pkcs7-certificates',
396
        'spl' => 'application/futuresplash',
397
        'spr' => 'application/x-sprite',
398
        'sprite' => 'application/x-sprite',
399
        'src' => 'application/x-wais-source',
400
        'ssi' => 'text/x-server-parsed-html',
401
        'ssm' => 'application/streamingmedia',
402
        'sst' => 'application/vnd.ms-pki.certstore',
403
        'step' => 'application/step',
404
        'stl' => 'application/sla',
405
        'stp' => 'application/step',
406
        'sv4cpio' => 'application/x-sv4cpio',
407
        'sv4crc' => 'application/x-sv4crc',
408
        'svf' => 'image/vnd.dwg',
409
        'svr' => 'application/x-world',
410
        'swf' => 'application/x-shockwave-flash',
411
        't' => 'application/x-troff',
412
        'talk' => 'text/x-speech',
413
        'tar' => 'application/x-tar',
414
        'tbk' => 'application/toolbook',
415
        'tcl' => 'application/x-tcl',
416
        'tcsh' => 'text/x-script.tcsh',
417
        'tex' => 'application/x-tex',
418
        'texi' => 'application/x-texinfo',
419
        'texinfo' => 'application/x-texinfo',
420
        'text' => 'application/plain',
421
        'tgz' => 'application/gnutar',
422
        'tif' => 'image/tiff',
423
        'tiff' => 'image/tiff',
424
        'tr' => 'application/x-troff',
425
        'tsi' => 'audio/tsp-audio',
426
        'tsp' => 'application/dsptype',
427
        'tsv' => 'text/tab-separated-values',
428
        'turbot' => 'image/florian',
429
        'txt' => 'text/plain',
430
        'uil' => 'text/x-uil',
431
        'uni' => 'text/uri-list',
432
        'unis' => 'text/uri-list',
433
        'unv' => 'application/i-deas',
434
        'uri' => 'text/uri-list',
435
        'uris' => 'text/uri-list',
436
        'ustar' => 'application/x-ustar',
437
        'uu' => 'application/octet-stream',
438
        'uue' => 'text/x-uuencode',
439
        'vcd' => 'application/x-cdlink',
440
        'vcs' => 'text/x-vcalendar',
441
        'vda' => 'application/vda',
442
        'vdo' => 'video/vdo',
443
        'vew' => 'application/groupwise',
444
        'viv' => 'video/vivo',
445
        'vivo' => 'video/vivo',
446
        'vmd' => 'application/vocaltec-media-desc',
447
        'vmf' => 'application/vocaltec-media-file',
448
        'voc' => 'audio/voc',
449
        'vos' => 'video/vosaic',
450
        'vox' => 'audio/voxware',
451
        'vqe' => 'audio/x-twinvq-plugin',
452
        'vqf' => 'audio/x-twinvq',
453
        'vql' => 'audio/x-twinvq-plugin',
454
        'vrml' => 'application/x-vrml',
455
        'vrt' => 'x-world/x-vrt',
456
        'vsd' => 'application/x-visio',
457
        'vst' => 'application/x-visio',
458
        'vsw' => 'application/x-visio',
459
        'w60' => 'application/wordperfect6.0',
460
        'w61' => 'application/wordperfect6.1',
461
        'w6w' => 'application/msword',
462
        'wav' => 'audio/wav',
463
        'wb1' => 'application/x-qpro',
464
        'wbmp' => 'image/vnd.wap.wbmp',
465
        'web' => 'application/vnd.xara',
466
        'wiz' => 'application/msword',
467
        'wk1' => 'application/x-123',
468
        'wmf' => 'windows/metafile',
469
        'wml' => 'text/vnd.wap.wml',
470
        'wmlc' => 'application/vnd.wap.wmlc',
471
        'wmls' => 'text/vnd.wap.wmlscript',
472
        'wmlsc' => 'application/vnd.wap.wmlscriptc',
473
        'word' => 'application/msword',
474
        'wp' => 'application/wordperfect',
475
        'wp5' => 'application/wordperfect',
476
        'wp6' => 'application/wordperfect',
477
        'wpd' => 'application/wordperfect',
478
        'wq1' => 'application/x-lotus',
479
        'wri' => 'application/mswrite',
480
        'wrl' => 'application/x-world',
481
        'wrz' => 'model/vrml',
482
        'wsc' => 'text/scriplet',
483
        'wsrc' => 'application/x-wais-source',
484
        'wtk' => 'application/x-wintalk',
485
        'xbm' => 'image/x-xbitmap',
486
        'xdr' => 'video/x-amt-demorun',
487
        'xgz' => 'xgl/drawing',
488
        'xif' => 'image/vnd.xiff',
489
        'xl' => 'application/excel',
490
        'xla' => 'application/excel',
491
        'xlb' => 'application/excel',
492
        'xlc' => 'application/excel',
493
        'xld' => 'application/excel',
494
        'xlk' => 'application/excel',
495
        'xll' => 'application/excel',
496
        'xlm' => 'application/excel',
497
        'xls' => 'application/excel',
498
        'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
499
        'xlt' => 'application/excel',
500
        'xlv' => 'application/excel',
501
        'xlw' => 'application/excel',
502
        'xm' => 'audio/xm',
503
        'xml' => 'application/xml',
504
        'xmz' => 'xgl/movie',
505
        'xpix' => 'application/x-vnd.ls-xpix',
506
        'xpm' => 'image/x-xpixmap',
507
        'x-png' => 'image/png',
508
        'xsr' => 'video/x-amt-showrun',
509
        'xwd' => 'image/x-xwd',
510
        'xyz' => 'chemical/x-pdb',
511
        'z' => 'application/x-compress',
512
        'zip' => 'application/zip',
513
        'zoo' => 'application/octet-stream',
514
        'zsh' => 'text/x-script.zsh',
515
    );
516
517
    /**
518
     * Method that allows you to determine a MIME type for a file which you provide the extension for.
519
     *
520
     * @param string $ext The file extension.
521
     *
522
     * @return string
523
     *
524
     * @throws \Alpha\Exception\IllegalArguementException
525
     *
526
     * @since 1.0
527
     */
528
    public static function getMIMETypeByExtension($ext)
529
    {
530
        $ext = mb_strtolower($ext);
531
        if (!isset(self::$extensionToMIMEMappings[$ext])) {
532
            throw new IllegalArguementException('Unable to determine the MIME type for the extension ['.$ext.']');
533
        }
534
535
        return self::$extensionToMIMEMappings[$ext];
536
    }
537
538
    /**
539
     * Renders the contents of the directory as a HTML list.
540
     *
541
     * @param string $sourceDir    The path to the source directory.
542
     * @param string $fileList     The HTML list of files generated (pass by reference).
543
     * @param int    $fileCount    The current file count (used in recursive calls).
544
     * @param string[]  $excludeFiles An array of file names to exclude from the list rendered.
545
     *
546
     * @return int The current filecount for the directory.
547
     *
548
     * @throws \Alpha\Exception\AlphaException
549
     *
550
     * @since 1.0
551
     */
552
    public static function listDirectoryContents($sourceDir, &$fileList, $fileCount = 0, $excludeFiles = array())
0 ignored issues
show
Unused Code introduced by
The parameter $fileCount is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
553
    {
554
        try {
555
            $dir = new DirectoryIterator($sourceDir);
556
            $fileCount = 0;
557
558
            foreach ($dir as $file) {
559
                if (!in_array($file->getFilename(), $excludeFiles)) {
560
                    if ($file->isDir() && !$file->isDot()) {
561
                        $fileList .= '<em>'.$file->getPathname().'</em><br>';
562
                        $fileCount += self::listDirectoryContents($file->getPathname(), $fileList, $fileCount, $excludeFiles);
563
                    } elseif (!$file->isDot()) {
564
                        $fileName = $file->getFilename();
565
                        ++$fileCount;
566
                        $fileList .= '&nbsp;&nbsp;&nbsp;&nbsp;'.$fileName.'<br>';
567
                    }
568
                }
569
            }
570
571
            return $fileCount;
572
        } catch (\Exception $e) {
573
            throw new AlphaException('Failed list files in the ['.$sourceDir.'] directory, error is ['.$e->getMessage().']');
574
        }
575
    }
576
577
    /**
578
     * Gets the contents of the directory recursively as a nested hash array (dirname => file1, file2, file3...).
579
     *
580
     * @param string $sourceDir    The path to the source directory.
581
     * @param array $fileList      The list of files generated (pass by reference).
582
     *
583
     * @return array
584
     *
585
     * @throws \Alpha\Exception\AlphaException
586
     *
587
     * @since 3.0
588
     */
589
    public static function getDirectoryContents($sourceDir, &$fileList = array())
590
    {
591
        try {
592
            $dir = new DirectoryIterator($sourceDir);
593
594
            foreach ($dir as $file) {
595
                if ($file->isDir() && !$file->isDot()) {
596
                    self::getDirectoryContents($file->getPathname(), $fileList);
597
                } elseif (!$file->isDot()) {
598
                    $fileList[$sourceDir][] = $file->getFilename();
599
                }
600
            }
601
602
            return $fileList;
603
        } catch (\Exception $e) {
604
            throw new AlphaException('Failed list files in the ['.$sourceDir.'] directory, error is ['.$e->getMessage().']');
605
        }
606
    }
607
608
    /**
609
     * Recursively deletes the contents of the directory indicated (the directory itself is not deleted).
610
     *
611
     * @param string $sourceDir    The path to the source directory.
612
     * @param string[]  $excludeFiles An array of file names to exclude from the deletion.
613
     *
614
     * @throws \Alpha\Exception\AlphaException
615
     *
616
     * @since 1.0
617
     */
618
    public static function deleteDirectoryContents($sourceDir, $excludeFiles = array())
619
    {
620
        try {
621
            $dir = new DirectoryIterator($sourceDir);
622
623
            foreach ($dir as $file) {
624
                if ($file->isDir() && !$file->isDot()) {
625
                    if (count(scandir($file->getPathname())) == 2 && !in_array($file->getFilename(), $excludeFiles)) { // remove an empty directory
626
                        rmdir($file->getPathname());
627
                    } else {
628
                        self::deleteDirectoryContents($file->getPathname(), $excludeFiles);
629
                    }
630
                } elseif (!$file->isDot() && !in_array($file->getFilename(), $excludeFiles)) {
631
                    unlink($file->getPathname());
632
                }
633
            }
634
        } catch (\Exception $e) {
635
            throw new AlphaException('Failed to delete files files in the ['.$sourceDir.'] directory, error is ['.$e->getMessage().']');
636
        }
637
    }
638
639
    /**
640
     * Recursively copies the indicated folder, or single file, to the destination location.
641
     *
642
     * @param string $source The path to the source directory or file.
643
     * @param string $dest   The destination source directory or file.
644
     *
645
     * @throws \Alpha\Exception\AlphaException
646
     *
647
     * @since 1.1
648
     */
649
    public static function copy($source, $dest)
650
    {
651
        if (is_file($source)) {
652
            if (!copy($source, $dest)) {
653
                throw new AlphaException("Error copying the file [$source] to [$dest].");
654
            }
655
656
            return;
657
        }
658
659
        // Make destination directory if it does not already exist
660
        if (!file_exists($dest) && !is_dir($dest)) {
661
            if (!mkdir($dest, 0777, true)) {
662
                throw new AlphaException("Error creating the destination directory [$dest].");
663
            }
664
        }
665
666
        $dir = dir($source);
667
        if ($dir !== false) {
668
            while (false !== $entry = $dir->read()) {
669
                if ($entry == '.' || $entry == '..') {
670
                    continue;
671
                }
672
673
                if ($dest !== "$source/$entry") {
674
                    self::copy("$source/$entry", "$dest/$entry");
675
                }
676
            }
677
678
            $dir->close();
679
        }
680
    }
681
682
    /**
683
     * Recursively compresses the contents of the source directory indicated to the destintation zip archive.
684
     *
685
     * @param string $source The path to the source directory or file.
686
     * @param string $dest   The destination zip file file.
687
     *
688
     * @throws \Alpha\Exception\AlphaException
689
     *
690
     * @since 1.1
691
     */
692
    public static function zip($source, $dest)
693
    {
694
        if (extension_loaded('zip') === true) {
695
            if (file_exists($source) === true) {
696
                $zip = new ZipArchive();
697
698
                if ($zip->open($dest, ZipArchive::CREATE) === true) {
699
                    $source = realpath($source);
700
701
                    if (is_dir($source) === true) {
702
                        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
703
704
                        foreach ($files as $file) {
705
                            $file = realpath($file);
706
707
                            if (is_dir($file) === true) {
708
                                $zip->addEmptyDir(str_replace($source.'/', '', $file.'/'));
709
                            }
710
711
                            if (is_file($file) === true) {
712
                                $zip->addFromString(str_replace($source.'/', '', $file), file_get_contents($file));
713
                            }
714
                        }
715
                    }
716
717
                    if (is_file($source) === true) {
718
                        $zip->addFromString(basename($source), file_get_contents($source));
719
                    }
720
                }
721
722
                $zip->close();
723
            }
724
        } else {
725
            throw new AlphaException('Unable to create the zip file ['.$dest.'] as the zip extension is unavailable!');
726
        }
727
    }
728
}
729