|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\LaravelPaths; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Symfony\Component\Finder\Exception\DirectoryNotFoundException; |
|
7
|
|
|
use Symfony\Component\Finder\Finder; |
|
8
|
|
|
|
|
9
|
|
|
class FilePath |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Normalize file path to standard formal |
|
13
|
|
|
* For a path like: "/usr/laravel/app\Http\..\..\database" returns "/usr/laravel/database". |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $path directory path |
|
16
|
|
|
* |
|
17
|
|
|
* @return string |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function normalize($path) |
|
20
|
|
|
{ |
|
21
|
|
|
$dir = \str_replace(['\\', '/', '//', '\\\\'], DIRECTORY_SEPARATOR, $path); |
|
22
|
|
|
|
|
23
|
|
|
$sections = \explode(DIRECTORY_SEPARATOR, $dir); |
|
24
|
|
|
|
|
25
|
|
|
$result = []; |
|
26
|
|
|
foreach ($sections as $section) { |
|
27
|
|
|
if ($section == '..') { |
|
28
|
|
|
\array_pop($result); |
|
29
|
|
|
} else { |
|
30
|
|
|
$result[] = $section; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return \implode(DIRECTORY_SEPARATOR, $result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* get relative path. removes base path of laravel installation from an absolute path. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $absFilePath Absolute directory path |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function getRelativePath($absFilePath) |
|
45
|
|
|
{ |
|
46
|
|
|
return \trim(Str::replaceFirst(base_path(), '', $absFilePath), '/\\'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* get all ".php" files in directory by giving a path. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path Directory path |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Symfony\Component\Finder\Finder |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function getAllPhpFiles($path) |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
|
|
return Finder::create()->files()->name('*.php')->in(base_path($path)); |
|
60
|
|
|
} catch (DirectoryNotFoundException $e) { |
|
61
|
|
|
return []; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|