1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\File;
4:
5: use Nette\Utils\Strings;
6: use PHPStan\DependencyInjection\AutowiredParameter;
7: use PHPStan\DependencyInjection\AutowiredService;
8: use function array_pop;
9: use function explode;
10: use function implode;
11: use function ltrim;
12: use function preg_match;
13: use function rtrim;
14: use function str_ends_with;
15: use function str_replace;
16: use function str_starts_with;
17: use function strlen;
18: use function strtolower;
19: use function substr;
20: use function trim;
21: use const DIRECTORY_SEPARATOR;
22:
23: #[AutowiredService]
24: final class FileHelper
25: {
26:
27: /**
28: * Directory separator => original path => normalized path. A path that normalizes to itself,
29: * which most do, is kept once - as the key and the value - so an entry costs little more than
30: * its hash bucket.
31: *
32: * @var array<string, array<string, string>>
33: */
34: private static array $normalizedPathsCache = [];
35:
36: private string $workingDirectory;
37:
38: public function __construct(
39: #[AutowiredParameter(ref: '%currentWorkingDirectory%')]
40: string $workingDirectory,
41: )
42: {
43: $this->workingDirectory = $this->normalizePath($workingDirectory);
44: }
45:
46: public function getWorkingDirectory(): string
47: {
48: return $this->workingDirectory;
49: }
50:
51: /** @api */
52: public function absolutizePath(string $path): string
53: {
54: if (DIRECTORY_SEPARATOR === '/') {
55: if (str_starts_with($path, '/')) {
56: return $path;
57: }
58: } elseif (substr($path, 1, 1) === ':') {
59: return $path;
60: }
61:
62: if (preg_match('~^[a-z0-9+\-.]+://~i', $path) === 1) {
63: return $path;
64: }
65:
66: return rtrim($this->getWorkingDirectory(), '/\\') . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
67: }
68:
69: /** @api */
70: public function normalizePath(string $originalPath, string $directorySeparator = DIRECTORY_SEPARATOR): string
71: {
72: $cached = self::$normalizedPathsCache[$directorySeparator][$originalPath] ?? null;
73: if ($cached !== null) {
74: return $cached;
75: }
76:
77: $isLocalPath = false;
78: if ($originalPath !== '') {
79: if ($originalPath[0] === '/') {
80: $isLocalPath = true;
81: } elseif (strlen($originalPath) >= 3 && $originalPath[1] === ':' && $originalPath[2] === '\\') { // e.g. C:\
82: $isLocalPath = true;
83: }
84: }
85:
86: $matches = null;
87: if (!$isLocalPath) {
88: $matches = Strings::match($originalPath, '~^([a-z0-9+\-.]+)://(.+)$~is');
89: }
90:
91: if ($matches !== null) {
92: [, $scheme, $path] = $matches;
93: $scheme = strtolower($scheme);
94: } else {
95: $scheme = null;
96: $path = $originalPath;
97: }
98:
99: $path = $this->normalizeSeparator($path);
100: $pathRoot = str_starts_with($path, '/') ? $directorySeparator : '';
101: $pathParts = explode('/', trim($path, '/'));
102:
103: $normalizedPathParts = [];
104: foreach ($pathParts as $pathPart) {
105: if ($pathPart === '.') {
106: continue;
107: }
108: if ($pathPart === '..') {
109: $removedPart = array_pop($normalizedPathParts);
110: if ($scheme === 'phar' && $removedPart !== null && str_ends_with($removedPart, '.phar')) {
111: $scheme = null;
112: }
113: } else {
114: $normalizedPathParts[] = $pathPart;
115: }
116: }
117:
118: $normalizedPath = ($scheme !== null ? $scheme . '://' : '') . $pathRoot . implode($directorySeparator, $normalizedPathParts);
119: if ($normalizedPath === $originalPath) {
120: $normalizedPath = $originalPath;
121: }
122:
123: return self::$normalizedPathsCache[$directorySeparator][$originalPath] = $normalizedPath;
124: }
125:
126: public function normalizeSeparator(string $path): string
127: {
128: return str_replace(['\\', '//', '///', '////'], '/', $path);
129: }
130:
131: }
132: