-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathFilesystemZipArchiveProvider.php
54 lines (41 loc) · 1.37 KB
/
FilesystemZipArchiveProvider.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
<?php
declare(strict_types=1);
namespace League\Flysystem\ZipArchive;
use ZipArchive;
class FilesystemZipArchiveProvider implements ZipArchiveProvider
{
/**
* @var bool
*/
private $parentDirectoryCreated = false;
public function __construct(private string $filename, private int $localDirectoryPermissions = 0700)
{
}
public function createZipArchive(): ZipArchive
{
if ($this->parentDirectoryCreated !== true) {
$this->parentDirectoryCreated = true;
$this->createParentDirectoryForZipArchive($this->filename);
}
return $this->openZipArchive();
}
private function createParentDirectoryForZipArchive(string $fullPath): void
{
$dirname = dirname($fullPath);
if (is_dir($dirname) || @mkdir($dirname, $this->localDirectoryPermissions, true)) {
return;
}
if ( ! is_dir($dirname)) {
throw UnableToCreateParentDirectory::atLocation($fullPath, error_get_last()['message'] ?? '');
}
}
private function openZipArchive(): ZipArchive
{
$archive = new ZipArchive();
$success = $archive->open($this->filename, ZipArchive::CREATE);
if ($success !== true) {
throw UnableToOpenZipArchive::atLocation($this->filename, $archive->getStatusString() ?: '');
}
return $archive;
}
}