1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mujhtech\NavToastr; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class CommandHelper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Ensure a directory exists by creating it when needed. |
12
|
|
|
* |
13
|
|
|
* @param string $dir The path of the directory |
14
|
|
|
* @param int $mode The directory access mode |
15
|
|
|
* @param bool $recursive Allow creating nested directories present in path |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public static function ensureDirectoryExists($dir, $mode = 0755, $recursive = true) |
19
|
|
|
{ |
20
|
|
|
if (! is_dir($dir)) { |
21
|
|
|
mkdir($dir, $mode, $recursive); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Copy an entire directory to a destination. |
27
|
|
|
* |
28
|
|
|
* @param string $dir The path of the source folder |
29
|
|
|
* @param string $dest The path of the destination folder |
30
|
|
|
* @param bool $force Whether to force the overwrite of existing files |
31
|
|
|
* @param bool $recursive Whether to copy subfolders recursively |
32
|
|
|
* @param array $ignores Array of files to be ignored |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public static function copyDirectory($dir, $dest, $force = false, $recursive = false, $ignores = []) |
36
|
|
|
{ |
37
|
|
|
// Open the source folder. Return if fails to open. |
38
|
|
|
|
39
|
|
|
if (! is_resource($dirHandler = @opendir($dir))) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Ensure the destination folder exists. |
44
|
|
|
|
45
|
|
|
self::ensureDirectoryExists($dest); |
46
|
|
|
|
47
|
|
|
// Copy the source files to destination. |
48
|
|
|
|
49
|
|
|
while (($file = readdir($dirHandler)) !== false) { |
50
|
|
|
|
51
|
|
|
// Check if this file should be ignored. |
52
|
|
|
|
53
|
|
|
$filesToIgnore = array_merge($ignores, ['.', '..']); |
54
|
|
|
|
55
|
|
|
if (self::isIgnoredFile($file, $filesToIgnore)) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Now, copy the file/folder. If the resource is a folder, proceed |
60
|
|
|
// recursively. Otherwise, copy the file to destination. |
61
|
|
|
|
62
|
|
|
$source = $dir.DIRECTORY_SEPARATOR.$file; |
63
|
|
|
$target = $dest.DIRECTORY_SEPARATOR.$file; |
64
|
|
|
|
65
|
|
|
if (is_dir($source) && $recursive) { |
66
|
|
|
self::copyDirectory($source, $target, $force, $recursive, $ignores); |
67
|
|
|
} elseif (is_file($source) && ($force || ! file_exists($target))) { |
68
|
|
|
copy($source, $target); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Close the source folder. |
73
|
|
|
|
74
|
|
|
closedir($dirHandler); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Compare two directories file by file. |
79
|
|
|
* |
80
|
|
|
* @param string $dir1 The path of the first folder |
81
|
|
|
* @param string $dir2 The path of the second folder |
82
|
|
|
* @param bool $recursive Whether to compare subfolders recursively |
83
|
|
|
* @param array $ignores Array of files to be ignored |
84
|
|
|
* @return bool|null Result of comparison or null if a folder not exists |
85
|
|
|
*/ |
86
|
|
|
public static function compareDirectories($dir1, $dir2, $recursive = false, $ignores = []) |
87
|
|
|
{ |
88
|
|
|
// Open the first folder. Return if fails to open. |
89
|
|
|
|
90
|
|
|
if (! is_resource($dirHandler = @opendir($dir1))) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Check if the second folder exists. |
95
|
|
|
|
96
|
|
|
if (! is_dir($dir2)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Now, compare the folders. |
101
|
|
|
|
102
|
|
|
while (($file = readdir($dirHandler)) !== false) { |
103
|
|
|
|
104
|
|
|
// Check if this file should be ignored. |
105
|
|
|
|
106
|
|
|
$filesToIgnore = array_merge($ignores, ['.', '..']); |
107
|
|
|
|
108
|
|
|
if (self::isIgnoredFile($file, $filesToIgnore)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Get paths of the resources to compare. |
113
|
|
|
|
114
|
|
|
$source = $dir1.DIRECTORY_SEPARATOR.$file; |
115
|
|
|
$target = $dir2.DIRECTORY_SEPARATOR.$file; |
116
|
|
|
|
117
|
|
|
// If the resources to compare are files, check that both files are |
118
|
|
|
// equals. |
119
|
|
|
|
120
|
|
|
if (is_file($source) && ! self::compareFiles($source, $target)) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// If the resources to compare are folders, recursively compare the |
125
|
|
|
// folders. |
126
|
|
|
|
127
|
|
|
$isDir = is_dir($source) && $recursive; |
128
|
|
|
|
129
|
|
|
if ($isDir && ! (bool) self::compareDirectories($source, $target, $recursive, $ignores)) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Close the opened folder. |
135
|
|
|
|
136
|
|
|
closedir($dirHandler); |
137
|
|
|
|
138
|
|
|
// At this point all the resources compared are equals. |
139
|
|
|
|
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check if two files are equals by comparing sha1 hash values. |
145
|
|
|
* |
146
|
|
|
* @param string $file1 The first file |
147
|
|
|
* @param string $file2 The second file |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public static function compareFiles($file1, $file2) |
151
|
|
|
{ |
152
|
|
|
if (! is_file($file1) || ! is_file($file2)) { |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return sha1_file($file1) === sha1_file($file2); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Recursively delete a directory. |
161
|
|
|
* |
162
|
|
|
* @param string $dir The directory to remove |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public static function removeDirectory($dir) |
166
|
|
|
{ |
167
|
|
|
return File::deleteDirectory($dir); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Check if a file is included in a set of ignored file patterns. |
172
|
|
|
* |
173
|
|
|
* @param string $file The file to check |
174
|
|
|
* @param array $ignores Array of file patterns to be ignored |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
protected static function isIgnoredFile($file, $ignores) |
178
|
|
|
{ |
179
|
|
|
foreach ($ignores as $pattern) { |
180
|
|
|
$match = Str::startsWith($pattern, 'regex:') ? |
181
|
|
|
preg_match(Str::substr($pattern, 6), $file) : |
182
|
|
|
Str::is($pattern, $file); |
183
|
|
|
|
184
|
|
|
if ($match) { |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
} |