-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPharUtilities.php
77 lines (60 loc) · 1.96 KB
/
PharUtilities.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
<?php
class PharUtilities {
/**
* Determines if the passed file is a PHAR file, determined by the extension.
* @param $filename
* @return bool
*/
public static function IsPhar($filename) {
$filename = strtolower($filename);
$e = explode('.', $filename);
$extension = $e[count($e)-1];
return ($extension == 'phar');
}
public static function ExtractToTempDirectory($rootDirectory, $filename) {
if (!self::IsPhar($filename)) {
return;
}
$phar = new Phar($filename);
$phar->extractTo($rootDirectory . '/__lib');
}
public static function CleanUp($rootDirectory) {
self::DeleteDirectoryAndContents($rootDirectory.'/__lib');
}
public static function DeleteDirectoryAndContents($dirPath) {
$it = new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
self::_DeleteFile($file->getRealPath());
//unlink($file->getRealPath());
}
}
rmdir($dirPath);
}
private static function _DeleteFile($filename) {
if (!defined('WINDOWS_SERVER')) {
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0) !== false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($filename)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$filename\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
}
}