1
|
|
|
<?php namespace Comodojo\Zip; |
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
|
|
|
public static function getTemporaryFolder(): string { |
30
|
|
|
|
31
|
|
|
// return UniqueId::generateCustom("zip-temp-folder"); |
|
|
|
|
32
|
|
|
|
33
|
|
|
return "zip-temp-folder-".UniqueId::generate(); |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $folder |
39
|
|
|
*/ |
40
|
|
|
public static function recursiveUnlink(string $folder, bool $remove_folder = true): bool { |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
|
44
|
|
|
self::emptyFolder($folder); |
45
|
|
|
|
46
|
|
|
if ( $remove_folder && rmdir($folder) === false ) { |
47
|
|
|
throw new Exception("Error deleting folder ".$folder); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
} catch (Exception $e) { |
51
|
|
|
|
52
|
|
|
throw $e; |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return true; |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected static function emptyFolder(string $folder): bool { |
61
|
|
|
|
62
|
|
|
$iterator = new RecursiveIteratorIterator( |
63
|
|
|
new RecursiveDirectoryIterator($folder, FilesystemIterator::SKIP_DOTS), |
64
|
|
|
RecursiveIteratorIterator::CHILD_FIRST |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
foreach ( $iterator as $path ) { |
68
|
|
|
|
69
|
|
|
$pathname = $path->getPathname(); |
70
|
|
|
|
71
|
|
|
if ( $path->isDir() ) { |
72
|
|
|
|
73
|
|
|
$action = rmdir($pathname); |
74
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
|
77
|
|
|
$action = unlink($pathname); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( $action === false ) throw new Exception("Error deleting ".$pathname." during recursive unlink of folder ".$folder); |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.