-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathZipArchiver.php
More file actions
135 lines (107 loc) · 3.79 KB
/
ZipArchiver.php
File metadata and controls
135 lines (107 loc) · 3.79 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
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
<?php
namespace Elendev\ComposerPush;
use Composer\IO\IOInterface;
use Composer\IO\NullIO;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class ZipArchiver
{
/**
* Archive the given directory in the $destination file
*
* @param string $source archive source
* @param string $destination archive destination
* @param string $subDirectory subdirectory in which the sources will be
* archived. If null, put at the root of the directory.
* @param array $ignores
* @param \Composer\IO\IOInterface|null $io
*
* @throws \Exception
*/
public static function archiveDirectory(
$source,
$destination,
$version,
$subDirectory = null,
$ignores = [],
$keepDotFiles = false,
$io = null
) {
if (empty($io)) {
$io = new NullIO();
}
if ($subDirectory) {
$io->write('[ZIP Archive] Archive into the subdirectory ' . $subDirectory);
} else {
$io->write('[ZIP Archive] Archive into root directory');
}
$finder = new Finder();
$fileSystem = new Filesystem();
$finder->in($source)->ignoreVCS(true)->ignoreDotFiles(!$keepDotFiles);
foreach ($ignores as $ignore) {
$finder->notPath($ignore);
}
$archive = new \ZipArchive();
$io->write(
'Create ZIP file ' . $destination,
true,
IOInterface::VERY_VERBOSE
);
if ($archive->open($destination, \ZipArchive::CREATE) !== true) {
$io->writeError(
'Impossible to create ZIP file ' . $destination,
true
);
throw new \Exception('Impossible to create the file ' . $destination);
}
foreach ($finder as $fileInfo) {
if ($subDirectory) {
$zipPath = $subDirectory . '/';
} else {
$zipPath = '';
}
$zipPath .= rtrim($fileSystem->makePathRelative(
$fileInfo->getRealPath(),
$source
), '/');
if (!$fileInfo->isFile()) {
continue;
}
$io->write(
'Zip file ' . $fileInfo->getPath() . ' to ' . $zipPath,
true,
IOInterface::VERY_VERBOSE
);
$archive->addFile($fileInfo->getRealPath(), $zipPath);
}
$archive->close();
$io->write('Update version in ZIP archive to ' . $version, true, IOInterface::VERBOSE);
self::updateVersion($destination, $subDirectory, $version, $io);
$io->write('Zip archive ' . $destination . ' done');
}
/**
* Update the version of the composer.json file of the zip archive
* @param $zipFile
* @param $subDirectory
* @param $version
* @param \Composer\IO\IOInterface|null $io
* @throws \Exception
*/
private static function updateVersion($zipFile, $subDirectory, $version, $io)
{
$archive = new \ZipArchive();
if ($archive->open($zipFile) !== true) {
throw new \Exception('Impossible to update Composer version in composer.json');
}
$filePath = ($subDirectory ? $subDirectory . '/' : '') . 'composer.json';
$content = json_decode($archive->getFromName($filePath));
if ($content === false || $content === null) {
$io->write('No composer.json file in the archive (path: ' . $filePath . ')', true, IOInterface::VERBOSE);
return;
}
$content->version = $version;
$archive->deleteName($filePath);
$archive->addFromString($filePath, json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$archive->close();
}
}