-
Notifications
You must be signed in to change notification settings - Fork 16
/
PathHelper.php
53 lines (39 loc) 路 1.22 KB
/
PathHelper.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
<?php
namespace Castor;
use Symfony\Component\Filesystem\Path;
class PathHelper
{
public static function getRoot(): string
{
static $root;
if (null === $root) {
if (class_exists(\RepackedApplication::class)) {
return $root = Path::getDirectory(getcwd() ?: '.');
}
$path = getcwd() ?: '/';
while (!file_exists($path . '/castor.php')) {
if ('/' === $path) {
throw new \RuntimeException('Could not find root "castor.php" file.');
}
$path = Path::getDirectory($path);
}
$root = $path;
}
return $root;
}
public static function realpath(string $path): string
{
$realpath = realpath($path);
if (false === $realpath) {
throw new \RuntimeException(sprintf('Directory "%s" not found.', $path));
}
return $realpath;
}
public static function makeRelative(string $path): string
{
if (!Path::isAbsolute($path)) {
throw new \RuntimeException(sprintf('Path "%s" is not absolute.', $path));
}
return Path::makeRelative($path, self::getRoot());
}
}