-
Notifications
You must be signed in to change notification settings - Fork 3
/
Directory.php
133 lines (112 loc) · 3.99 KB
/
Directory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php declare(strict_types = 1);
namespace PharIo\FileSystem;
class Directory {
/** @var string */
private $path;
public function __construct(string $path) {
$this->ensureIsDirectory($path);
$this->path = $path;
}
/*
* Taken from http://stackoverflow.com/questions/2637945/getting-relative-path-from-absolute-path-in-php#comment18071708_2637945
* Credits go to http://stackoverflow.com/users/208809/gordon
*/
public function getRelativePathTo(Directory $directory): string {
$to = $this->asString();
$from = $directory->asString();
// some compatibility fixes for Windows paths
$from = is_dir($from) ? rtrim($from, '\/') . '/' : $from;
$to = is_dir($to) ? rtrim($to, '\/') . '/' : $to;
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach ($from as $depth => $dir) {
// find first non-matching dir
if ($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - $depth;
if ($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
}
$relPath[0] = './' . $relPath[0];
}
}
return implode('/', $relPath);
}
public function exists(): bool {
clearstatcache(true, $this->path);
return file_exists($this->path);
}
/**
* @throws DirectoryException
*/
public function ensureExists(int $mode = 0755): void {
$this->ensureValidMode($mode);
try {
if (!$this->exists()) {
if (!@mkdir($this->path, $mode, true)) {
throw new DirectoryException(
sprintf('Creating directory "%s" failed.', $this->path),
DirectoryException::CreateFailed
);
}
}
clearstatcache(true, $this->path);
if ((fileperms($this->path) & 0777) !== $mode) {
chmod($this->path, $mode);
}
} catch (\ErrorException $e) {
throw new DirectoryException(
sprintf('Creating directory "%s" failed.', $this->path),
DirectoryException::CreateFailed,
$e
);
}
}
public function child(string $child): Directory {
return new Directory($this->path . DIRECTORY_SEPARATOR . $child);
}
public function hasChild(string $child): bool {
return file_exists($this->path . DIRECTORY_SEPARATOR . $child);
}
public function file(string $filename): Filename {
return new Filename($this->path . DIRECTORY_SEPARATOR . $filename);
}
public function asString(): string {
return $this->path;
}
public function withAbsolutePath(): Directory {
return new Directory(realpath($this->asString()));
}
public function isWritable(): bool {
return is_writable($this->path);
}
/**
* @throws DirectoryException
*/
private function ensureIsDirectory(string $path) {
if (!file_exists($path) || is_dir($path)) {
return;
}
throw new DirectoryException(
sprintf('Path %s exists but is not a directory', $path),
DirectoryException::InvalidType
);
}
private function ensureValidMode(int $mode) {
if ($mode < 0 || $mode > 777) {
throw new DirectoryException(
sprintf('Mode %d is not valid', $mode),
DirectoryException::InvalidMode
);
}
}
}