|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padawan\Framework\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Phine\Path\Path; |
|
6
|
|
|
|
|
7
|
|
|
class PathResolver |
|
8
|
|
|
{ |
|
9
|
|
|
public function __construct(Path $path) |
|
10
|
|
|
{ |
|
11
|
|
|
$this->path = $path; |
|
12
|
|
|
} |
|
13
|
|
|
public function canonical($path) |
|
14
|
|
|
{ |
|
15
|
|
|
return $this->path->canonical($path); |
|
16
|
|
|
} |
|
17
|
|
|
public function relative($from, $to, $addDot = false) |
|
18
|
|
|
{ |
|
19
|
|
|
$fromParts = array_values(array_filter($this->path->split($from))); |
|
20
|
|
|
$toParts = array_values(array_filter($this->path->split($to))); |
|
21
|
|
|
$i = 0; |
|
22
|
|
|
$count = min(count($fromParts), count($toParts)); |
|
23
|
|
|
while ($i < $count && $fromParts[$i] === $toParts[$i]) { |
|
24
|
|
|
++$i; |
|
25
|
|
|
} |
|
26
|
|
|
$pathToCommon = array_slice($toParts, $i); |
|
27
|
|
|
$upsNumber = count($fromParts) - $i; |
|
28
|
|
|
return $this->join(array_merge( |
|
29
|
|
|
$addDot ? ['.'] : [], |
|
30
|
|
|
$upsNumber ? array_fill(0, $upsNumber, '..') : [], |
|
31
|
|
|
$pathToCommon |
|
32
|
|
|
)); |
|
33
|
|
|
} |
|
34
|
|
|
public function join($elements) |
|
35
|
|
|
{ |
|
36
|
|
|
return str_replace("//", "/", $this->path->join($elements)); |
|
37
|
|
|
} |
|
38
|
|
|
public function read($filePath) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($this->exists($filePath)) { |
|
41
|
|
|
return file_get_contents($filePath); |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new \Exception(sprintf("File not found %s", $filePath)); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
public function create($filePath, $isDir = false) |
|
47
|
|
|
{ |
|
48
|
|
|
$dirPath = $filePath; |
|
49
|
|
|
if (!$isDir) { |
|
50
|
|
|
$dirPath = dirname($dirPath); |
|
51
|
|
|
} |
|
52
|
|
|
if (!empty($dirPath) && !$this->exists($dirPath) && !$this->isDir($dirPath)) { |
|
53
|
|
|
mkdir($dirPath); |
|
54
|
|
|
} elseif (!$this->isDir($dirPath)) { |
|
55
|
|
|
throw new \Exception("Not a directory"); |
|
56
|
|
|
} |
|
57
|
|
|
if (!$isDir) { |
|
58
|
|
|
if (!$this->exists($filePath)) { |
|
59
|
|
|
return touch($filePath); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
public function getWorkingDirectory() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->canonical(getcwd()); |
|
66
|
|
|
} |
|
67
|
|
|
public function getAbsolutePath($path, $cwd = null) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$cwd) { |
|
70
|
|
|
$cwd = $this->getWorkingDirectory(); |
|
71
|
|
|
} |
|
72
|
|
|
return $this->join([$cwd, $path]); |
|
73
|
|
|
} |
|
74
|
|
|
public function remove($path) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->path->remove($path); |
|
77
|
|
|
} |
|
78
|
|
|
public function exists($filePath) |
|
79
|
|
|
{ |
|
80
|
|
|
return file_exists($filePath); |
|
81
|
|
|
} |
|
82
|
|
|
public function write($filePath, $content = "") |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->exists($filePath)) { |
|
85
|
|
|
$this->create($filePath); |
|
86
|
|
|
} |
|
87
|
|
|
if (!$this->isFile($filePath)) { |
|
88
|
|
|
throw new \Exception("Unable to write to non-file"); |
|
89
|
|
|
} |
|
90
|
|
|
return file_put_contents($filePath, $content); |
|
91
|
|
|
} |
|
92
|
|
|
public function isDir($path) |
|
93
|
|
|
{ |
|
94
|
|
|
return is_dir($path); |
|
95
|
|
|
} |
|
96
|
|
|
public function isFile($path) |
|
97
|
|
|
{ |
|
98
|
|
|
return is_file($path); |
|
99
|
|
|
} |
|
100
|
|
|
public function getDirFiles($dir) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$this->isDir($dir)) { |
|
103
|
|
|
return []; |
|
104
|
|
|
} |
|
105
|
|
|
return array_filter(scandir($dir), function($file) { |
|
106
|
|
|
return $file !== '.' && $file !== '..' && $file !== '.git'; |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
public function getDirFilesRecursive($dir) |
|
110
|
|
|
{ |
|
111
|
|
|
$files = []; |
|
112
|
|
|
foreach ($this->getDirFiles($dir) as $file) { |
|
113
|
|
|
$path = $this->join([$dir, $file]); |
|
114
|
|
|
if ($this->isFile($path)) { |
|
115
|
|
|
$files[] = $path; |
|
116
|
|
|
} elseif ($this->isDir($path)) { |
|
117
|
|
|
$files = array_merge( |
|
118
|
|
|
$files, |
|
119
|
|
|
$this->getDirFilesRecursive( |
|
120
|
|
|
$path |
|
121
|
|
|
) |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
return $files; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** @var Path */ |
|
129
|
|
|
private $path; |
|
130
|
|
|
} |
|
131
|
|
|
|