|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JeroenNoten\LaravelAdminLte\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\File; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
class CommandHelper |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Path to the package root folder. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $packagePath = __DIR__.'/../../'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Path to the package stubs folder. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $stubsPath = __DIR__.'/../Console/stubs'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Ensure a directory exists by creating it when needed. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $dir The path of the directory |
|
28
|
|
|
* @param int $mode The directory access mode |
|
29
|
|
|
* @param bool $recursive Allow creating nested directories present in path |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
19 |
|
public static function ensureDirectoryExists($dir, $mode = 0755, $recursive = true) |
|
33
|
|
|
{ |
|
34
|
19 |
|
if (! is_dir($dir)) { |
|
35
|
17 |
|
mkdir($dir, $mode, $recursive); |
|
36
|
|
|
} |
|
37
|
19 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Copy an entire directory to a destination. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $dir The path of the source folder |
|
43
|
|
|
* @param string $dest The path of the destination folder |
|
44
|
|
|
* @param bool $force Whether to force the overwrite of existing files |
|
45
|
|
|
* @param bool $recursive Whether to copy subfolders recursively |
|
46
|
|
|
* @param array $ignores Array of files to be ignored |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
19 |
|
public static function copyDirectory($dir, $dest, $force = false, $recursive = false, $ignores = []) |
|
50
|
|
|
{ |
|
51
|
|
|
// Open the source folder. Return if fails to open. |
|
52
|
|
|
|
|
53
|
19 |
|
if (! is_resource($dirHandler = @opendir($dir))) { |
|
54
|
2 |
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Ensure the destination folder exists. |
|
58
|
|
|
|
|
59
|
17 |
|
self::ensureDirectoryExists($dest); |
|
60
|
|
|
|
|
61
|
|
|
// Copy the source files to destination. |
|
62
|
|
|
|
|
63
|
17 |
|
while (($file = readdir($dirHandler)) !== false) { |
|
64
|
|
|
|
|
65
|
|
|
// Check if this file should be ignored. |
|
66
|
|
|
|
|
67
|
17 |
|
$filesToIgnore = array_merge($ignores, ['.', '..']); |
|
68
|
|
|
|
|
69
|
17 |
|
if (self::isIgnoredFile($file, $filesToIgnore)) { |
|
70
|
17 |
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Now, copy the file/folder. If the resource is a folder, proceed |
|
74
|
|
|
// recursively. Otherwise, copy the file to destination. |
|
75
|
|
|
|
|
76
|
17 |
|
$source = $dir.DIRECTORY_SEPARATOR.$file; |
|
77
|
17 |
|
$target = $dest.DIRECTORY_SEPARATOR.$file; |
|
78
|
|
|
|
|
79
|
17 |
|
if (is_dir($source) && $recursive) { |
|
80
|
16 |
|
self::copyDirectory($source, $target, $force, $recursive, $ignores); |
|
81
|
17 |
|
} elseif (is_file($source) && ($force || ! file_exists($target))) { |
|
82
|
17 |
|
copy($source, $target); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Close the source folder. |
|
87
|
|
|
|
|
88
|
17 |
|
closedir($dirHandler); |
|
89
|
17 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Compare two directories file by file. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $dir1 The path of the first folder |
|
95
|
|
|
* @param string $dir2 The path of the second folder |
|
96
|
|
|
* @param bool $recursive Whether to compare subfolders recursively |
|
97
|
|
|
* @param array $ignores Array of files to be ignored |
|
98
|
|
|
* @return bool|null Result of comparison or null if a folder not exists |
|
99
|
|
|
*/ |
|
100
|
16 |
|
public static function compareDirectories($dir1, $dir2, $recursive = false, $ignores = []) |
|
101
|
|
|
{ |
|
102
|
|
|
// Open the first folder. Return if fails to open. |
|
103
|
|
|
|
|
104
|
16 |
|
if (! is_resource($dirHandler = @opendir($dir1))) { |
|
105
|
1 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Check if the second folder exists. |
|
109
|
|
|
|
|
110
|
16 |
|
if (! is_dir($dir2)) { |
|
111
|
5 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Now, compare the folders. |
|
115
|
|
|
|
|
116
|
15 |
|
while (($file = readdir($dirHandler)) !== false) { |
|
117
|
|
|
|
|
118
|
|
|
// Check if this file should be ignored. |
|
119
|
|
|
|
|
120
|
15 |
|
$filesToIgnore = array_merge($ignores, ['.', '..']); |
|
121
|
|
|
|
|
122
|
15 |
|
if (self::isIgnoredFile($file, $filesToIgnore)) { |
|
123
|
15 |
|
continue; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// Get paths of the resources to compare. |
|
127
|
|
|
|
|
128
|
15 |
|
$source = $dir1.DIRECTORY_SEPARATOR.$file; |
|
129
|
15 |
|
$target = $dir2.DIRECTORY_SEPARATOR.$file; |
|
130
|
|
|
|
|
131
|
|
|
// If the resources to compare are files, check that both files are |
|
132
|
|
|
// equals. |
|
133
|
|
|
|
|
134
|
15 |
|
if (is_file($source) && ! self::compareFiles($source, $target)) { |
|
135
|
4 |
|
return false; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// If the resources to compare are folders, recursively compare the |
|
139
|
|
|
// folders. |
|
140
|
|
|
|
|
141
|
14 |
|
$isDir = is_dir($source) && $recursive; |
|
142
|
|
|
|
|
143
|
14 |
|
if ($isDir && ! (bool) self::compareDirectories($source, $target, $recursive, $ignores)) { |
|
144
|
3 |
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// Close the opened folder. |
|
149
|
|
|
|
|
150
|
14 |
|
closedir($dirHandler); |
|
151
|
|
|
|
|
152
|
|
|
// At this point all the resources compared are equals. |
|
153
|
|
|
|
|
154
|
14 |
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Check if two files are equals by comparing sha1 hash values. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $file1 The first file |
|
161
|
|
|
* @param string $file2 The second file |
|
162
|
|
|
* @return bool |
|
163
|
|
|
*/ |
|
164
|
15 |
|
public static function compareFiles($file1, $file2) |
|
165
|
|
|
{ |
|
166
|
15 |
|
if (! is_file($file1) || ! is_file($file2)) { |
|
167
|
3 |
|
return false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
15 |
|
return sha1_file($file1) === sha1_file($file2); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Recursively delete a directory. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $dir The directory to remove |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
11 |
|
public static function removeDirectory($dir) |
|
180
|
|
|
{ |
|
181
|
11 |
|
return File::deleteDirectory($dir); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the fully qualified path to some package resource. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $path Relative path to the resource |
|
188
|
|
|
* @return string Fully qualified path to the resource |
|
189
|
|
|
*/ |
|
190
|
14 |
|
public static function getPackagePath($path = null) |
|
191
|
|
|
{ |
|
192
|
14 |
|
if (! $path) { |
|
193
|
1 |
|
return self::$packagePath; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
14 |
|
return self::$packagePath.DIRECTORY_SEPARATOR.$path; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get the fully qualified path to some package stub resource. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $path Relative path to the stub resource |
|
203
|
|
|
* @return string Fully qualified path to the stub resource |
|
204
|
|
|
*/ |
|
205
|
14 |
|
public static function getStubPath($path = null) |
|
206
|
|
|
{ |
|
207
|
14 |
|
if (! $path) { |
|
208
|
1 |
|
return self::$stubsPath; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
14 |
|
return self::$stubsPath.DIRECTORY_SEPARATOR.$path; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get the fully qualified path relative to the configured view path. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string $path Relative path to some view |
|
218
|
|
|
* @return string Fully qualified path to the view |
|
219
|
|
|
*/ |
|
220
|
14 |
|
public static function getViewPath($path = null) |
|
221
|
|
|
{ |
|
222
|
14 |
|
$basePath = config('view.paths')[0] ?? resource_path('views'); |
|
223
|
|
|
|
|
224
|
14 |
|
if (! $path) { |
|
225
|
14 |
|
return $basePath; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
14 |
|
return $basePath.DIRECTORY_SEPARATOR.$path; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Check if a file is included in a set of ignored file patterns. |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $file The file to check |
|
235
|
|
|
* @param array $ignores Array of file patterns to be ignored |
|
236
|
|
|
* @return bool |
|
237
|
|
|
*/ |
|
238
|
22 |
|
protected static function isIgnoredFile($file, $ignores) |
|
239
|
|
|
{ |
|
240
|
22 |
|
foreach ($ignores as $pattern) { |
|
241
|
22 |
|
$match = Str::startsWith($pattern, 'regex:') ? |
|
242
|
2 |
|
preg_match(Str::substr($pattern, 6), $file) : |
|
243
|
22 |
|
Str::is($pattern, $file); |
|
244
|
|
|
|
|
245
|
22 |
|
if ($match) { |
|
246
|
22 |
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
22 |
|
return false; |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|