|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\Fractor\Skipper\FileSystem; |
| 6 | + |
| 7 | +use Nette\Utils\Strings; |
| 8 | +use Symfony\Component\Filesystem\Filesystem; |
| 9 | +use Webmozart\Assert\Assert; |
| 10 | + |
| 11 | +final class FilePathHelper |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @see https://regex101.com/r/d4F5Fm/1 |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + private const SCHEME_PATH_REGEX = '#^([a-z]+)\\:\\/\\/(.+)#'; |
| 18 | + |
| 19 | + /** |
| 20 | + * @see https://regex101.com/r/no28vw/1 |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + private const TWO_AND_MORE_SLASHES_REGEX = '#/{2,}#'; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + private const SCHEME_UNDEFINED = 'undefined'; |
| 29 | + private Filesystem $filesystem; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + |
| 33 | + ) { |
| 34 | + $this->filesystem = new Filesystem(); |
| 35 | + } |
| 36 | + |
| 37 | + public function relativePath(string $fileRealPath): string |
| 38 | + { |
| 39 | + if (!$this->filesystem->isAbsolutePath($fileRealPath)) { |
| 40 | + return $fileRealPath; |
| 41 | + } |
| 42 | + |
| 43 | + return $this->relativeFilePathFromDirectory($fileRealPath, (string)getcwd()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Used from |
| 48 | + * https://github.com/phpstan/phpstan-src/blob/02425e61aa48f0668b4efb3e73d52ad544048f65/src/File/FileHelper.php#L40, with custom modifications |
| 49 | + */ |
| 50 | + public function normalizePathAndSchema(string $originalPath): string |
| 51 | + { |
| 52 | + $directorySeparator = DIRECTORY_SEPARATOR; |
| 53 | + |
| 54 | + $matches = Strings::match($originalPath, self::SCHEME_PATH_REGEX); |
| 55 | + if ($matches !== null) { |
| 56 | + [, $scheme, $path] = $matches; |
| 57 | + } else { |
| 58 | + $scheme = self::SCHEME_UNDEFINED; |
| 59 | + $path = $originalPath; |
| 60 | + } |
| 61 | + |
| 62 | + $normalizedPath = PathNormalizer::normalize((string) $path); |
| 63 | + $path = Strings::replace($normalizedPath, self::TWO_AND_MORE_SLASHES_REGEX, '/'); |
| 64 | + |
| 65 | + $pathRoot = str_starts_with($path, '/') ? $directorySeparator : ''; |
| 66 | + $pathParts = explode('/', trim($path, '/')); |
| 67 | + |
| 68 | + $normalizedPathParts = $this->normalizePathParts($pathParts, $scheme); |
| 69 | + |
| 70 | + $pathStart = ($scheme !== self::SCHEME_UNDEFINED ? $scheme . '://' : ''); |
| 71 | + return PathNormalizer::normalize($pathStart . $pathRoot . implode($directorySeparator, $normalizedPathParts)); |
| 72 | + } |
| 73 | + |
| 74 | + private function relativeFilePathFromDirectory(string $fileRealPath, string $directory): string |
| 75 | + { |
| 76 | + Assert::directory($directory); |
| 77 | + $normalizedFileRealPath = PathNormalizer::normalize($fileRealPath); |
| 78 | + |
| 79 | + $relativeFilePath = $this->filesystem->makePathRelative($normalizedFileRealPath, $directory); |
| 80 | + return rtrim($relativeFilePath, '/'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param string[] $pathParts |
| 85 | + * @return string[] |
| 86 | + */ |
| 87 | + private function normalizePathParts(array $pathParts, string $scheme): array |
| 88 | + { |
| 89 | + $normalizedPathParts = []; |
| 90 | + |
| 91 | + foreach ($pathParts as $pathPart) { |
| 92 | + if ($pathPart === '.') { |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + if ($pathPart !== '..') { |
| 97 | + $normalizedPathParts[] = $pathPart; |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + /** @var string $removedPart */ |
| 102 | + $removedPart = array_pop($normalizedPathParts); |
| 103 | + if ($scheme !== 'phar') { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + if (!\str_ends_with($removedPart, '.phar')) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + $scheme = self::SCHEME_UNDEFINED; |
| 112 | + } |
| 113 | + |
| 114 | + return $normalizedPathParts; |
| 115 | + } |
| 116 | +} |
0 commit comments