-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPharOperator.php
94 lines (78 loc) · 2.38 KB
/
PharOperator.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
<?php
declare(strict_types=1);
namespace axios\tools;
class PharOperator
{
private \Phar $phar;
private string $zip_dir;
/**
* @var array exclude files or folders
*/
private array $exclude;
/**
* @var mixed|string
*
* @example '/\.(json|php|ini)$/'
*/
private $include;
private string $index = 'autoload.php';
public function __construct($zip_dir = null, $include = '/\.*$/', $exclude = [])
{
if (!file_exists($zip_dir)) {
throw new \InvalidArgumentException("{$zip_dir} is not exist.");
}
$this->zip_dir = Path::join($zip_dir);
$this->exclude = $exclude;
$this->include = $include;
}
/**
* @return $this
*/
public function setIndex(string $index): self
{
$this->index = $index;
return $this;
}
public function addExclude(string $exclude): self
{
$this->exclude[] = Path::join($exclude);
return $this;
}
public function zip(string $output_path): void
{
$save_path = $output_path;
if (is_dir($save_path)) {
throw new \InvalidArgumentException("{$output_path} must be file path.");
}
if (!file_exists(\dirname($save_path))) {
@mkdir(\dirname($save_path), 0755, true);
}
if (file_exists($save_path)) {
@unlink($save_path);
}
$this->phar = new \Phar($save_path);
$this->phar->buildFromDirectory($this->zip_dir, $this->include);
if (!empty($this->exclude)) {
$this->remove($this->exclude);
}
$this->phar->setStub($this->phar->createDefaultStub($this->index));
$this->phar->stopBuffering();
}
private function remove(array $exclude): void
{
foreach ($exclude as $file) {
$path = Path::join($this->zip_dir, $file);
if (is_dir($path)) {
$files = Path::search($path, $this->include);
foreach ($files as $filepath) {
$file = str_replace($this->zip_dir . \DIRECTORY_SEPARATOR, '', $filepath);
if ($this->phar->offsetExists($file)) {
$this->phar->delete($file);
}
}
} elseif ($this->phar->offsetExists($file)) {
$this->phar->delete($file);
}
}
}
}