1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JeroenNoten\LaravelAdminLte\Http\Helpers; |
4
|
|
|
|
5
|
|
|
class CommandHelper |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Check if the directories for the files exists. |
9
|
|
|
* |
10
|
|
|
* @param $directory |
11
|
|
|
* @return void |
12
|
|
|
*/ |
13
|
|
|
public static function ensureDirectoriesExist($directory) |
14
|
|
|
{ |
15
|
|
|
// CHECK if directory exists, if not create it |
16
|
|
|
if (! is_dir($directory)) { |
17
|
|
|
mkdir($directory, 0755, true); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Recursive function that copies an entire directory to a destination. |
23
|
|
|
* |
24
|
|
|
* @param $source_directory |
25
|
|
|
* @param $destination_directory |
26
|
|
|
*/ |
27
|
|
|
public static function directoryCopy($source_directory, $destination_directory, $force = false, $recursive = false, $ignore = [], $ignore_ending = null) |
28
|
|
|
{ |
29
|
|
|
//Checks destination folder existance |
30
|
|
|
self::ensureDirectoriesExist($destination_directory); |
31
|
|
|
//Open source directory |
32
|
|
|
$directory = opendir($source_directory); |
33
|
|
|
|
34
|
|
|
while (false !== ($file = readdir($directory))) { |
35
|
|
|
if (($file != '.') && ($file != '..')) { |
36
|
|
|
if (is_dir($source_directory.'/'.$file) && $recursive) { |
37
|
|
|
self::directoryCopy($source_directory.'/'.$file, $destination_directory.'/'.$file, $force, $recursive, $ignore, $ignore_ending); |
38
|
|
|
} elseif (! is_dir($source_directory.'/'.$file)) { |
39
|
|
|
$checkup = true; |
40
|
|
|
|
41
|
|
View Code Duplication |
if ($ignore_ending) { |
|
|
|
|
42
|
|
|
if (! is_array($ignore_ending)) { |
43
|
|
|
$ignore_ending = str_replace('*', '', $ignore_ending); |
44
|
|
|
|
45
|
|
|
$checkup = (substr($file, -strlen($ignore_ending)) !== $ignore_ending); |
46
|
|
|
} else { |
47
|
|
|
foreach ($ignore_ending as $key => $ignore_ending_sub) { |
48
|
|
|
if ($checkup) { |
49
|
|
|
$ignore_ending_sub = str_replace('*', '', $ignore_ending_sub); |
50
|
|
|
|
51
|
|
|
$checkup = (substr($file, -strlen($ignore_ending_sub)) !== $ignore_ending_sub); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($checkup && (! in_array($file, $ignore))) { |
58
|
|
|
if (file_exists($destination_directory.'/'.$file) && ! $force) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
copy($source_directory.'/'.$file, $destination_directory.'/'.$file); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
closedir($directory); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Compare Directories. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public static function compareDirectories($package_path, $assets_path, $sub_folder = '', $ignore = [], $ignore_ending = [], $recursive = false, $internal = null) |
76
|
|
|
{ |
77
|
|
|
$return = []; |
|
|
|
|
78
|
|
|
$package_sha1 = ''; |
79
|
|
|
$assets_sha1 = ''; |
80
|
|
|
|
81
|
|
|
$handle = opendir($package_path); |
82
|
|
|
|
83
|
|
|
while ($file = readdir($handle)) { |
84
|
|
|
$checkup = true; |
85
|
|
|
|
86
|
|
|
if (in_array($file, $ignore)) { |
87
|
|
|
$checkup = false; |
88
|
|
View Code Duplication |
} elseif ($ignore_ending) { |
|
|
|
|
89
|
|
|
if (! is_array($ignore_ending)) { |
90
|
|
|
$ignore_ending = str_replace('*', '', $ignore_ending); |
91
|
|
|
|
92
|
|
|
$checkup = (substr($file, -strlen($ignore_ending)) !== $ignore_ending); |
93
|
|
|
} else { |
94
|
|
|
foreach ($ignore_ending as $key => $ignore_ending_sub) { |
95
|
|
|
if ($checkup) { |
96
|
|
|
$ignore_ending_sub = str_replace('*', '', $ignore_ending_sub); |
97
|
|
|
|
98
|
|
|
$checkup = (substr($file, -strlen($ignore_ending_sub)) !== $ignore_ending_sub); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($file == '.' || $file == '..' || ! $checkup) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
$sourcepath = $package_path.$sub_folder.DIRECTORY_SEPARATOR.$file; |
108
|
|
|
$destpath = $assets_path.DIRECTORY_SEPARATOR.$file; |
109
|
|
|
if (is_file($sourcepath)) { |
110
|
|
|
$package_sha1 .= sha1_file($sourcepath); |
111
|
|
|
if (file_exists($destpath) && is_file($destpath)) { |
112
|
|
|
$assets_sha1 .= sha1_file($destpath); |
113
|
|
|
} |
114
|
|
|
} elseif ($recursive) { |
115
|
|
|
$return = self::compareDirectories($sourcepath, $destpath, '', $ignore, $ignore_ending, $recursive, true); |
|
|
|
|
116
|
|
|
$package_sha1 .= $return['package_sha1']; |
117
|
|
|
$assets_sha1 .= $return['assets_sha1']; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
closedir($handle); |
122
|
|
|
|
123
|
|
|
if ($internal) { |
124
|
|
|
return [ |
125
|
|
|
'package_sha1' => $package_sha1, |
126
|
|
|
'assets_sha1' => $assets_sha1, |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $package_sha1 == $assets_sha1 ? true : ($assets_sha1 == '' ? null : false); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Rescursive directory remove. |
135
|
|
|
* |
136
|
|
|
* @param $directory |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public static function removeDirectory($directory) |
140
|
|
|
{ |
141
|
|
|
if (file_exists($directory)) { |
142
|
|
|
foreach (glob($directory.'/{,.}*[!.]', GLOB_MARK | GLOB_BRACE) as $file) { |
143
|
|
|
is_dir($file) ? self::removeDirectory($file) : unlink($file); |
144
|
|
|
} |
145
|
|
|
rmdir($directory); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.