1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Backup\Tasks\Backup; |
4
|
|
|
|
5
|
|
|
use Spatie\Backup\Helpers\Format; |
6
|
|
|
use ZipArchive; |
7
|
|
|
|
8
|
|
|
class Zip |
9
|
|
|
{ |
10
|
|
|
/** @var \ZipArchive */ |
11
|
|
|
protected $zipFile; |
12
|
|
|
|
13
|
|
|
/** @var int */ |
14
|
|
|
protected $fileCount = 0; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $pathToZip; |
18
|
|
|
|
19
|
|
|
public static function createForManifest(Manifest $manifest, string $pathToZip): Zip |
20
|
|
|
{ |
21
|
|
|
$zip = new static($pathToZip); |
22
|
|
|
|
23
|
|
|
foreach ($manifest->files() as $file) { |
24
|
|
|
$zip->add($file, self::determineNameOfFileInZip($file, $pathToZip)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $zip; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected static function determineNameOfFileInZip(string $pathToFile, string $pathToZip) |
31
|
|
|
{ |
32
|
|
|
$zipDirectory = pathinfo($pathToZip, PATHINFO_DIRNAME); |
33
|
|
|
|
34
|
|
|
$fileDirectory = pathinfo($pathToFile, PATHINFO_DIRNAME); |
35
|
|
|
|
36
|
|
|
if (starts_with($fileDirectory, $zipDirectory)) { |
37
|
|
|
return str_replace($zipDirectory, '', $pathToFile); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $pathToFile; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __construct(string $pathToZip) |
44
|
|
|
{ |
45
|
|
|
$this->zipFile = new ZipArchive(); |
46
|
|
|
|
47
|
|
|
$this->pathToZip = $pathToZip; |
48
|
|
|
|
49
|
|
|
$this->open($pathToZip); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function path(): string |
53
|
|
|
{ |
54
|
|
|
return $this->pathToZip; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function size(): int |
58
|
|
|
{ |
59
|
|
|
if ($this->fileCount === 0) { |
60
|
|
|
return 0; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return filesize($this->pathToZip); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function humanReadableSize(): string |
67
|
|
|
{ |
68
|
|
|
return Format::humanReadableSize($this->size()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function open() |
72
|
|
|
{ |
73
|
|
|
$this->zipFile->open($this->pathToZip, ZipArchive::CREATE); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function close() |
77
|
|
|
{ |
78
|
|
|
$this->zipFile->close(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string|array $files |
83
|
|
|
* @param string $nameInZip |
84
|
|
|
* |
85
|
|
|
* @return \Spatie\Backup\Tasks\Backup\Zip |
86
|
|
|
*/ |
87
|
|
|
public function add($files, string $nameInZip = null): Zip |
88
|
|
|
{ |
89
|
|
|
if (is_array($files)) { |
90
|
|
|
$nameInZip = null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (is_string($files)) { |
94
|
|
|
$files = [$files]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->open(); |
98
|
|
|
|
99
|
|
|
foreach ($files as $file) { |
100
|
|
|
if (file_exists($file)) { |
101
|
|
|
$this->zipFile->addFile($file, $nameInZip).PHP_EOL; |
102
|
|
|
} |
103
|
|
|
$this->fileCount++; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->close(); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function count(): int |
112
|
|
|
{ |
113
|
|
|
return $this->fileCount; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.