1
|
|
|
<?php namespace Comodojo\Zip\Base; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Utils\UniqueId; |
4
|
|
|
use \RecursiveIteratorIterator; |
5
|
|
|
use \RecursiveDirectoryIterator; |
6
|
|
|
use \FilesystemIterator; |
7
|
|
|
use \Exception; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* comodojo/zip - ZipArchive toolbox |
11
|
|
|
* |
12
|
|
|
* @package Comodojo Spare Parts |
13
|
|
|
* @author Marco Giovinazzi <[email protected]> |
14
|
|
|
* @license MIT |
15
|
|
|
* |
16
|
|
|
* LICENSE: |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
class ManagerTools { |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get a temporary folder name (random) |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public static function getTemporaryFolder(): string { |
35
|
|
|
|
36
|
|
|
return "zip-temp-folder-".UniqueId::generate(); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Unlink a folder recursively |
42
|
|
|
* |
43
|
|
|
* @param string $folder The folder to be removed |
44
|
|
|
* @param bool $remove_folder If true, the folder itself will be removed |
45
|
|
|
* @return bool |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
|
|
public static function recursiveUnlink(string $folder, bool $remove_folder = true): bool { |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
|
52
|
|
|
self::emptyFolder($folder); |
53
|
|
|
if ( $remove_folder && rmdir($folder) === false ) { |
54
|
|
|
throw new Exception("Error deleting folder: $folder"); |
55
|
|
|
} |
56
|
|
|
return true; |
57
|
|
|
|
58
|
|
|
} catch (Exception $e) { |
59
|
|
|
throw $e; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected static function emptyFolder(string $folder): bool { |
65
|
|
|
|
66
|
|
|
$iterator = new RecursiveIteratorIterator( |
67
|
|
|
new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS), |
68
|
|
|
RecursiveIteratorIterator::CHILD_FIRST |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
foreach ( $iterator as $path ) { |
72
|
|
|
|
73
|
|
|
$pathname = $path->getPathname(); |
74
|
|
|
|
75
|
|
|
if ( $path->isDir() ) { |
76
|
|
|
$action = rmdir($pathname); |
77
|
|
|
} else { |
78
|
|
|
$action = unlink($pathname); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( $action === false ) { |
82
|
|
|
throw new Exception("Error deleting $pathname during recursive unlink of folder: $folder"); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|