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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Backup\Filesystem;
use Magento\Framework\Backup\Filesystem\Iterator\Filter;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* Filesystem helper
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Helper
{
/**
* Constant can be used in getInfo() function as second parameter.
* Check whether directory and all files/sub directories are writable
*
* @const int
*/
const INFO_WRITABLE = 1;
/**
* Constant can be used in getInfo() function as second parameter.
* Check whether directory and all files/sub directories are readable
*
* @const int
*/
const INFO_READABLE = 2;
/**
* Constant can be used in getInfo() function as second parameter.
* Get directory size
*
* @const int
*/
const INFO_SIZE = 4;
/**
* Constant can be used in getInfo() function as second parameter.
* Combination of INFO_WRITABLE, INFO_READABLE, INFO_SIZE
*
* @const int
*/
const INFO_ALL = 7;
/**
* Recursively delete $path
*
* @param string $path
* @param array $skipPaths
* @param bool $removeRoot
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.ShortMethodName)
*/
public function rm($path, $skipPaths = [], $removeRoot = false)
{
$filesystemIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::CHILD_FIRST
);
$iterator = new Filter($filesystemIterator, $skipPaths);
foreach ($iterator as $item) {
$item->isDir() ? @rmdir($item->__toString()) : @unlink($item->__toString());
}
if ($removeRoot && is_dir($path)) {
@rmdir($path);
}
}
/**
* Get information (readable, writable, size) about $path
*
* @param string $path
* @param int $infoOptions
* @param array $skipFiles
* @return array
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function getInfo($path, $infoOptions = self::INFO_ALL, $skipFiles = [])
{
$info = [];
if ($infoOptions & self::INFO_READABLE) {
$info['readable'] = true;
$info['readableMeta'] = [];
}
if ($infoOptions & self::INFO_WRITABLE) {
$info['writable'] = true;
$info['writableMeta'] = [];
}
if ($infoOptions & self::INFO_SIZE) {
$info['size'] = 0;
}
$filesystemIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::CHILD_FIRST
);
$iterator = new Filter($filesystemIterator, $skipFiles);
foreach ($iterator as $item) {
if ($item->isLink()) {
continue;
}
if ($infoOptions & self::INFO_WRITABLE && !$item->isWritable()) {
$info['writable'] = false;
$info['writableMeta'][] = $item->getPathname();
}
if ($infoOptions & self::INFO_READABLE && !$item->isReadable()) {
$info['readable'] = false;
$info['readableMeta'][] = $item->getPathname();
}
if ($infoOptions & self::INFO_SIZE && !$item->isDir()) {
$info['size'] += $item->getSize();
}
}
return $info;
}
}