1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Quantum PHP Framework |
||
5 | * |
||
6 | * An open source software development framework for PHP |
||
7 | * |
||
8 | * @package Quantum |
||
9 | * @author Arman Ag. <[email protected]> |
||
10 | * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
||
11 | * @link http://quantum.softberg.org/ |
||
12 | * @since 2.9.0 |
||
13 | */ |
||
14 | |||
15 | namespace Quantum\Libraries\Archive\Adapters; |
||
16 | |||
17 | use Quantum\Libraries\Archive\ArchiveInterface; |
||
18 | use Quantum\Libraries\Storage\FileSystem; |
||
19 | use Quantum\Exceptions\ArchiveException; |
||
20 | use Quantum\Exceptions\LangException; |
||
21 | use ZipArchive; |
||
22 | use Exception; |
||
23 | |||
24 | /** |
||
25 | * Class ZipAdapter |
||
26 | * @package Quantum\Libraries\Archive\Adapters |
||
27 | */ |
||
28 | class ZipAdapter implements ArchiveInterface |
||
29 | { |
||
30 | |||
31 | /** |
||
32 | * @var FileSystem |
||
33 | */ |
||
34 | private $fs; |
||
35 | |||
36 | /** |
||
37 | * @var ZipArchive |
||
38 | */ |
||
39 | private $archive; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $archiveName; |
||
45 | |||
46 | /** |
||
47 | * @var int|null |
||
48 | */ |
||
49 | private $mode; |
||
50 | |||
51 | /** |
||
52 | * ZipArchive constructor |
||
53 | * @throws LangException |
||
54 | * @throws ArchiveException |
||
55 | */ |
||
56 | public function __construct(string $archiveName, ?int $mode = ZipArchive::CREATE) |
||
57 | { |
||
58 | $this->archiveName = $archiveName; |
||
59 | $this->mode = $mode; |
||
60 | |||
61 | $this->fs = new FileSystem(); |
||
62 | $this->archive = new ZipArchive(); |
||
63 | |||
64 | $this->reopen(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | public function offsetExists(string $filename): bool |
||
71 | { |
||
72 | if (strpos($filename, '.') === false) { |
||
73 | $filename = rtrim($filename, '/') . '/'; |
||
74 | } |
||
75 | |||
76 | return $this->archive->locateName($filename) !== false; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @inheritDoc |
||
81 | */ |
||
82 | public function addEmptyDir(string $directory): bool |
||
83 | { |
||
84 | if (!$this->offsetExists($directory)) { |
||
85 | return $this->archive->addEmptyDir($directory); |
||
86 | } |
||
87 | |||
88 | return false; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | * @throws ArchiveException |
||
94 | * @throws LangException |
||
95 | */ |
||
96 | public function addFile(string $filePath, string $entryName = null): bool |
||
97 | { |
||
98 | if (!$this->fs->exists($filePath)) { |
||
99 | throw ArchiveException::fileNotFound($filePath); |
||
100 | } |
||
101 | |||
102 | return $this->archive->addFile($filePath, $entryName); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @inheritDoc |
||
107 | */ |
||
108 | public function addFromString(string $entryName, string $content): bool |
||
109 | { |
||
110 | return $this->archive->addFromString($entryName, $content); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | public function addMultipleFiles(array $fileNames): bool |
||
117 | { |
||
118 | try { |
||
119 | foreach ($fileNames as $entryName => $filePath) { |
||
120 | $this->addFile($filePath, $entryName); |
||
121 | } |
||
122 | } catch (Exception $e) { |
||
123 | return false; |
||
124 | } |
||
125 | |||
126 | return true; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @inheritDoc |
||
131 | */ |
||
132 | public function count(): int |
||
133 | { |
||
134 | return $this->archive->count(); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @inheritDoc |
||
139 | * @throws ArchiveException |
||
140 | * @throws LangException |
||
141 | */ |
||
142 | public function extractTo(string $pathToExtract, $files = null): bool |
||
143 | { |
||
144 | $this->reopen(); |
||
145 | return $this->archive->extractTo($pathToExtract); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @inheritDoc |
||
150 | * @throws ArchiveException |
||
151 | * @throws LangException |
||
152 | */ |
||
153 | public function deleteFile(string $filename): bool |
||
154 | { |
||
155 | if ($this->offsetExists($filename)) { |
||
156 | $state = $this->archive->deleteName($filename); |
||
157 | $this->reopen(); |
||
158 | return $state; |
||
159 | } |
||
160 | |||
161 | return false; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @inheritDoc |
||
166 | * @throws ArchiveException |
||
167 | * @throws LangException |
||
168 | */ |
||
169 | public function deleteMultipleFiles(array $fileNames): bool |
||
170 | { |
||
171 | foreach ($fileNames as $entryName) { |
||
172 | $this->deleteFile($entryName); |
||
173 | } |
||
174 | |||
175 | return true; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @throws LangException |
||
180 | * @throws ArchiveException |
||
181 | */ |
||
182 | private function reopen() |
||
183 | { |
||
184 | if ($this->archive->filename) { |
||
185 | $this->archive->close(); |
||
186 | } |
||
187 | |||
188 | if ($res = $this->archive->open($this->archiveName, $this->mode) !== true) { |
||
189 | throw ArchiveException::cantOpen($res); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
190 | } |
||
191 | |||
192 | } |
||
193 | } |
||
194 |