-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathPathUtility.php
More file actions
112 lines (88 loc) · 3.19 KB
/
PathUtility.php
File metadata and controls
112 lines (88 loc) · 3.19 KB
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
<?php
namespace Photobooth\Utility;
use InvalidArgumentException;
use Photobooth\Environment;
class PathUtility
{
public static function getRootPath(): string
{
$path = realpath(__DIR__ . '/../../');
if ($path === false) {
throw new InvalidArgumentException('Rootpath could not be resolved.');
}
return $path;
}
public static function getAbsolutePath(string $path = ''): string
{
if ($path === '') {
return '';
}
if (self::isUrl($path)) {
return $path;
}
$documentRoot = self::getRootPath();
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
if (self::isAbsolutePath($path) && str_starts_with($path, self::getRootPath())) {
return $path;
}
$absolutePath = $documentRoot . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
$absolutePath = preg_replace('#' . DIRECTORY_SEPARATOR . '+#', DIRECTORY_SEPARATOR, (string) realpath($absolutePath));
if ($absolutePath && strpos($absolutePath, $documentRoot) === 0) {
return $absolutePath;
}
return $documentRoot . DIRECTORY_SEPARATOR . ltrim($path, DIRECTORY_SEPARATOR);
}
public static function isAbsolutePath(string $path): bool
{
if (Environment::isWindows() && (substr($path, 1, 2) === ':/' || substr($path, 1, 2) === ':\\')) {
return true;
}
return str_starts_with($path, '/');
}
public static function isUrl(string $path): bool
{
return str_starts_with($path, 'http');
}
public static function getPublicPath(string $path = '', bool $absolute = false): string
{
if (self::isUrl($path)) {
return $path;
}
if (self::isAbsolutePath($path)) {
$path = str_replace(self::getRootPath(), '', $path);
}
$path = self::fixFilePath(self::getBaseUrl() . $path);
if ($absolute) {
$path = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http') . '://' . $_SERVER['SERVER_NAME'] . $path;
}
return $path;
}
public static function getBaseUrl(): string
{
$documentRoot = (string) realpath($_SERVER['DOCUMENT_ROOT']);
$rootPath = self::getRootPath();
return self::fixFilePath(str_replace($documentRoot, '', $rootPath) . '/');
}
public static function fixFilePath(string $path): string
{
return str_replace(['\\', '//'], '/', $path);
}
public static function resolveFilePath(string $filePath): string
{
if (self::isUrl($filePath)) {
return $filePath;
}
$absolutePath = self::isAbsolutePath($filePath) ? $filePath : self::getAbsolutePath($filePath);
if (file_exists($absolutePath)) {
return $absolutePath;
}
$altPath1 = $_SERVER['DOCUMENT_ROOT'] . $filePath;
$altPath2 = $_SERVER['DOCUMENT_ROOT'] . '/' . $filePath;
if (file_exists($altPath1)) {
return $altPath1;
} elseif (file_exists($altPath2)) {
return $altPath2;
}
throw new \Exception('File not found: ' . $filePath);
}
}