Passed
Push — master ( 70acef...8b9af3 )
by Florian
07:10 queued 02:43
created

CommandHelper::getStubPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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 23
    public static function ensureDirectoryExists($dir, $mode = 0755, $recursive = true)
33
    {
34 23
        if (! is_dir($dir)) {
35 21
            mkdir($dir, $mode, $recursive);
36
        }
37 23
    }
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 23
    public static function copyDirectory($dir, $dest, $force = false, $recursive = false, $ignores = [])
50
    {
51
        // Open the source folder. Return if fails to open.
52
53 23
        if (! is_resource($dirHandler = @opendir($dir))) {
54 2
            return;
55
        }
56
57
        // Ensure the destination folder exists.
58
59 21
        self::ensureDirectoryExists($dest);
60
61
        // Copy the source files to destination.
62
63 21
        while (($file = readdir($dirHandler)) !== false) {
64
65
            // Check if this file should be ignored.
66
67 21
            $filesToIgnore = array_merge($ignores, ['.', '..']);
68
69 21
            if (self::isIgnoredFile($file, $filesToIgnore)) {
70 21
                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 21
            $source = $dir.DIRECTORY_SEPARATOR.$file;
77 21
            $target = $dest.DIRECTORY_SEPARATOR.$file;
78
79 21
            if (is_dir($source) && $recursive) {
80 18
                self::copyDirectory($source, $target, $force, $recursive, $ignores);
81 21
            } elseif (is_file($source) && ($force || ! file_exists($target))) {
82 21
                copy($source, $target);
83
            }
84
        }
85
86
        // Close the source folder.
87
88 21
        closedir($dirHandler);
89 21
    }
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 20
    public static function compareDirectories($dir1, $dir2, $recursive = false, $ignores = [])
101
    {
102
        // Open the first folder. Return if fails to open.
103
104 20
        if (! is_resource($dirHandler = @opendir($dir1))) {
105 1
            return;
106
        }
107
108
        // Check if the second folder exists.
109
110 20
        if (! is_dir($dir2)) {
111 8
            return;
112
        }
113
114
        // Now, compare the folders.
115
116 19
        while (($file = readdir($dirHandler)) !== false) {
117
118
            // Check if this file should be ignored.
119
120 19
            $filesToIgnore = array_merge($ignores, ['.', '..']);
121
122 19
            if (self::isIgnoredFile($file, $filesToIgnore)) {
123 19
                continue;
124
            }
125
126
            // Get paths of the resources to compare.
127
128 19
            $source = $dir1.DIRECTORY_SEPARATOR.$file;
129 19
            $target = $dir2.DIRECTORY_SEPARATOR.$file;
130
131
            // If the resources to compare are files, check that both files are
132
            // equals.
133
134 19
            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 18
            $isDir = is_dir($source) && $recursive;
142
143 18
            if ($isDir && ! (bool) self::compareDirectories($source, $target, $recursive, $ignores)) {
144 3
                return false;
145
            }
146
        }
147
148
        // Close the opened folder.
149
150 18
        closedir($dirHandler);
151
152
        // At this point all the resources compared are equals.
153
154 18
        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 19
    public static function compareFiles($file1, $file2)
165
    {
166 19
        if (! is_file($file1) || ! is_file($file2)) {
167 3
            return false;
168
        }
169
170 19
        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 15
    public static function removeDirectory($dir)
180
    {
181 15
        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 19
    public static function getPackagePath($path = null)
191
    {
192 19
        if (! $path) {
193 1
            return self::$packagePath;
194
        }
195
196 19
        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 19
    public static function getStubPath($path = null)
206
    {
207 19
        if (! $path) {
208 1
            return self::$stubsPath;
209
        }
210
211 19
        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 19
    public static function getViewPath($path = null)
221
    {
222 19
        $basePath = config('view.paths')[0] ?? resource_path('views');
223
224 19
        if (! $path) {
225 19
            return $basePath;
226
        }
227
228 19
        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 26
    protected static function isIgnoredFile($file, $ignores)
239
    {
240 26
        foreach ($ignores as $pattern) {
241 26
            $match = Str::startsWith($pattern, 'regex:') ?
242 2
                     preg_match(Str::substr($pattern, 6), $file) :
243 26
                     Str::is($pattern, $file);
244
245 26
            if ($match) {
246 26
                return true;
247
            }
248
        }
249
250 26
        return false;
251
    }
252
}
253