forked from KennFatt/UnPhar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunphar.php
133 lines (114 loc) · 3.61 KB
/
unphar.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
<?php
declare(strict_types=1);
/**
* UnPhar - PHP Utility tool to extracting a Phar (PHP Archive) file in batch mode.
*
* @author KennFatt https://github.com/KennFatt
* @website https://kennan.xyz/
*/
namespace unphar {
use Phar;
use PharException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use UnexpectedValueException;
/** Path to current directory */
define("DIRECTORY_ROOT", dirname(__FILE__) . DIRECTORY_SEPARATOR);
/**
* Relative path to folder `phars/`.
*
* The folder is used to be the place of all your .phar files.
*/
define("DIRECTORY_PHARS", DIRECTORY_ROOT . "phars");
/**
* Relative path to folder `out/`.
*
* The place for all extracted .phar files.
*/
define("DIRECTORY_OUT", DIRECTORY_ROOT . "out");
function cli(): void
{
checkDirectory();
$pharFiles = scanPharFiles();
if ($pharFiles === []) {
printf("There is no .phar file inside your `phars/` folder, please try again.\n");
return;
}
extractPharFiles($pharFiles);
}
/** Safely check required directories. */
function checkDirectory(): void
{
if (!is_dir(DIRECTORY_PHARS)) {
mkdir(DIRECTORY_PHARS, 0777);
}
if (!is_dir(DIRECTORY_OUT)) {
mkdir(DIRECTORY_OUT, 0777);
}
}
/**
* Scan all files inside folder `phars/` and
* store it into an array if the file were valid Phar.
*
* @return \Phar[]|array
*/
function scanPharFiles(): array
{
$phars = [];
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DIRECTORY_PHARS));
/**
* @var \SplFileInfo $fileInfo
*/
foreach ($iterator as $pathName => $fileInfo) {
if (
$fileInfo->getFilename() === "." ||
$fileInfo->getFilename() === ".." ||
strtolower($fileInfo->getExtension()) !== "phar"
) {
continue;
}
try {
$cleanFileName =
substr($fileInfo->getBasename(), 0, strlen($fileInfo->getBasename()) - 5);
if (!isset($phars[$cleanFileName])) {
$phars[$cleanFileName] = new Phar($pathName);
}
} catch (UnexpectedValueException $e) {
printf("File: `%s` is not a valid Phar.\n", $fileInfo->getFilename());
}
}
return $phars;
}
/**
* Extract all given files of Phar to respective directory
* inside folder `out/`.
*
* @param \Phar[] $pharFiles Array of scanned Phar files.
*/
function extractPharFiles(array $pharFiles): void
{
$opts = getopt("", ["override"]);
foreach ($pharFiles as $folderName => $pharFile) {
$outPath = DIRECTORY_OUT . DIRECTORY_SEPARATOR . $folderName;
if (is_dir($outPath) && !isset($opts["override"])) {
printf(
"Skipping to extract `%s` because the destination path is already exists!\n",
$folderName
);
continue;
}
@mkdir($outPath);
try {
$pharFile->extractTo($outPath, null, true);
printf(
"Successfully extracting `%s` to `%s`\n",
$folderName,
$outPath
);
} catch (PharException $e) {
printf("%s\n", $e->getMessage());
}
}
}
\unphar\cli();
};