-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.php
executable file
·150 lines (139 loc) · 4.42 KB
/
bundle.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env php
<?php
/**
* Create Joomla! extension zip from manifest file
*
* @see https://docs.joomla.org/Manifest_files
* @author Charley Wu <[email protected]>
*/
if ($argc < 3) {
echo "Usage: $argv[0] manifest.xml archive.zip\n";
exit;
}
$file = $argv[1];
if (!file_exists($file)) {
exit("Failed to open $file.");
}
$builder = new JoomlaArchiveBuilder($file);
$zipPath = $argv[2];
$builder->build($zipPath);
class JoomlaArchiveBuilder
{
private string $file;
private string $type;
private SimpleXMLElement $xml;
private ZipArchive $zip;
private string $prefix;
public function __construct(string $file)
{
$this->xml = simplexml_load_file($file);
if (!$this->xml) {
throw new Exception("Failed to load $file.");
}
$this->type = $this->xml->attributes()['type'];
$this->file = $file;
$this->prefix = dirname($file);
$this->zip = new ZipArchive;
}
public function build($zipFile)
{
$zipPath = dirname($zipFile);
if (!file_exists($zipPath)) {
mkdir($zipPath, 0777, true);
}
$this->zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$this->addFile($this->file);
$this->addFiles();
$this->addLanguages();
$this->addMediaFiles();
$this->zip->close();
}
private function addLanguages()
{
if (empty($this->xml->languages)) return;
foreach ($this->xml->languages->language as $language) {
$this->addFile($language, $this->xml->languages->attributes()->folder);
}
if ($this->type == 'component' && !empty($this->xml->administration)) {
if (empty($this->xml->administration->languages)) return;
foreach ($this->xml->administration->languages->language as $language) {
$this->addFile($language, $this->xml->administration->languages->attributes()->folder);
}
}
}
private function addMediaFiles()
{
if ($this->type == 'package') return;
if (empty($this->xml->media)) return;
foreach ($this->xml->media->folder as $folder) {
$path = $this->joinPaths($this->prefix, $this->xml->media->attributes()->folder, $folder);
$this->addDirectory($path);
}
foreach ($this->xml->media->filename as $filename) {
$this->addFile($filename, $this->xml->media->attributes()->folder);
}
}
private function addFiles()
{
if (empty($this->xml->files)) return;
if ($this->type == 'package') {
foreach ($this->xml->files->file as $file) {
$this->addFile($file, $this->xml->files->attributes()->folder);
}
return;
}
foreach ($this->xml->files->folder as $folder) {
$path = $this->joinPaths($this->prefix, $this->xml->files->attributes()->folder, $folder);
$this->addDirectory($path);
}
foreach ($this->xml->files->filename as $filename) {
$this->addFile($filename, $this->xml->files->attributes()->folder);
}
if ($this->type == 'component' && !empty($this->xml->administration)) {
if (empty($this->xml->administration->files)) return;
foreach ($this->xml->administration->files->folder as $folder) {
$path = $this->joinPaths($this->prefix, $this->xml->administration->files->attributes()->folder, $folder);
$this->addDirectory($path);
}
foreach ($this->xml->administration->files->filename as $filename) {
$this->addFile($filename, $this->xml->administration->files->attributes()->folder);
}
}
}
private function addFile($file, $folder = null)
{
$path = (str_starts_with($file, $this->prefix)) ? $file : $this->joinPaths($this->prefix, $folder, $file);
$entry = str_replace($this->prefix, "", $path);
echo "Adding: $path\t$entry\n";
$this->zip->addFile($path, $entry);
}
private function addDirectory($path)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
$filename = $file->getFilename();
if (str_starts_with($filename, '.')) continue;
if ($file->isDir()) {
$this->zip->addEmptyDir(str_replace($this->prefix, '', $file));
continue;
}
if ($file->isFile()) {
$entry = str_replace($this->prefix, "", $file);
echo "Adding: $path\t$entry\n";
$this->zip->addFile($file, $entry);
continue;
}
}
}
private function joinPaths(...$paths)
{
if (sizeof($paths) === 0) return '';
$prefix = ($paths[0] === DIRECTORY_SEPARATOR) ? DIRECTORY_SEPARATOR : '';
$processed = array_map(function ($part) {
return rtrim($part, DIRECTORY_SEPARATOR);
}, array_filter($paths, function ($path) {
return !empty($path);
}));
return $prefix . implode(DIRECTORY_SEPARATOR, $processed);
}
}