This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake-phar.php
More file actions
92 lines (83 loc) · 2.4 KB
/
make-phar.php
File metadata and controls
92 lines (83 loc) · 2.4 KB
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
<?php
//getopt m
$opts = getopt("m");
build_phar("resourceTools.phar", __DIR__ . DIRECTORY_SEPARATOR, true, isset($opts["m"]));
/**
* @param string $file_phar
* @param string $dir
* @return void
*/
function build_phar(string $file_phar, string $dir, bool $compress, bool $isMini): void{
if(!preg_match('/^[a-z1-9.\s,_\-]*$/ui', $file_phar)){
printInfo('error: This program does not support output to directories other than the current directory');
printInfo('output: '. $file_phar .', regular expression: /^[a-z1-9\.\s,_]*$/ui');
return;
}
if(substr($file_phar, strrpos($file_phar, '.')+1) !== "phar"){
$file_phar .= ".phar";
}
printInfo("start build: ".$file_phar);
if(file_exists($file_phar)){
printInfo("Phar file already exists, overwriting");
Phar::unlinkArchive($file_phar);
}
$files = [];
if($isMini){
addDirectory($dir, "src", $files, ["decrypt.php", "encrypt.php", "main.php"]);
}else{
addDirectory($dir, "src", $files);
addDirectory($dir, "resources", $files);
}
addDirectory($dir, "vendor", $files);
$count = count($files);
printInfo("adding ".$count." files");
$phar = new Phar($file_phar, 0);
$phar->startBuffering();
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->buildFromIterator(new \ArrayIterator($files));
if($isMini){
$phar->setStub('<?php include "phar://".__FILE__ ."/src/mini/mini.php"; __HALT_COMPILER(); ?>');
}else{
$phar->setStub('<?php include "phar://".__FILE__ ."/src/main.php"; __HALT_COMPILER(); ?>');
}
if($compress){
printInfo("Compressing...");
$phar->compressFiles(Phar::GZ);
}
$phar->stopBuffering();
printInfo("end");
}
/**
* @param string $dir
* @param string $basePath
* @param array<string, string> $files
* @return void
*/
function addDirectory(string $dir, string $basePath, array &$files, array $excluded = []){//: void
$end_char = substr($dir, -1, 1);
if($end_char !== "/" && $end_char !== "\\"){
$dir .= DIRECTORY_SEPARATOR;
}
$targetPath = $dir.$basePath;
if(!is_dir($targetPath)){
return;
}
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetPath)) as $path => $file){
if($file->isFile() === false){
continue;
}
foreach($excluded as $exclude){
if(str_contains($path, $exclude)){
continue 2;
}
}
$files[str_replace($dir, "", $path)] = $path;
}
}
/**
* @param string $message
* @return void
*/
function printInfo(string $message){//: void
echo $message.PHP_EOL;
}