Skip to content

Commit

Permalink
Resolution of directory paths fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 23, 2021
1 parent 7cff1ad commit d0925f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.5.1] - 2021-12-23

### Fixed

- Resolution of directory paths

## [1.5.0] - 2021-12-22

### Added
Expand Down
31 changes: 18 additions & 13 deletions src/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public static function resolveAbsolutePath(string $srcPath, string $dstPath): ?s
}

// Unification of directories separators
$finalPath = self::uniformizePathSeparator(dirname($srcPath));
$finalPath = $srcPath;
if (substr($finalPath, -1) !== '/') {
$finalPath = self::uniformizePathSeparator(dirname($finalPath));
}
$finalPath = rtrim($finalPath, '/');
if ($finalPath === '.') {
$finalPath = '';
}
Expand All @@ -146,7 +150,7 @@ public static function resolveAbsolutePath(string $srcPath, string $dstPath): ?s

// Replacement of '../'
do {
$finalPath = preg_replace('#(/|^)([^\\\/?%*:|"<>.]+)/../#', '/', $finalPath, -1, $nbReplacements);
$finalPath = preg_replace('#(/|^)([^\\\/?%*:|"<>.]+)/..(/|$)#', '/', $finalPath, -1, $nbReplacements);
} while ($nbReplacements > 0);
if (false === strpos($finalPath, './')) {
Expand Down Expand Up @@ -190,20 +194,21 @@ public static function resolveRelativePath(string $srcPath, string $dstPath): st

$srcDepth = count($srcPath);
$dstDepth = count($dstPath);
$differentDepthPath = 0;
$differentDepthPath = false;

for ($i = 0; $i < $srcDepth; $i++) {
if (!isset($dstPath[$i]) || $srcPath[$i] !== $dstPath[$i] || $differentDepthPath > 0) {
$differentDepthPath++;
if (!isset($dstPath[$i]) || $srcPath[$i] !== $dstPath[$i]) {
$differentDepthPath = $i;
break;
}
}

$relativePath = '';
if ($differentDepthPath > 0) {
$relativePath .= str_repeat('../', $differentDepthPath);
$relativePath .= implode('/', array_slice($dstPath, min($dstDepth, $differentDepthPath) - 1));
if (false !== $differentDepthPath) {
$relativePath .= str_repeat('../', $srcDepth - $differentDepthPath);
$relativePath .= implode('/', array_slice($dstPath, min($dstDepth, $differentDepthPath)));
}
if ($differentDepthPath === 0) {
if (false === $differentDepthPath) {
$relativePath .= './';
$relativePath .= implode('/', array_slice($dstPath, $srcDepth, $dstDepth));
}
Expand Down Expand Up @@ -239,12 +244,12 @@ private static function uniformizePathSeparator(string $path): string
*/
private static function extractFilename(array &$path): ?string
{
$filename = end($path) ?: null;

if (null !== $filename) {
unset($path[count($path) - 1]);
if (false === ($filename = end($path))) {
return null;
}

unset($path[count($path) - 1]);

return $filename;
}
}
11 changes: 11 additions & 0 deletions tests/FileHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public function absolutePathProvider(): array
['foo/bar/index', '../../qux/quux.foo', '/qux/quux.foo'],
['foo/bar/index.md', '../../../qux.md', null],
['foo/bar/index', '../../qux/quux.foo#anchor', '/qux/quux.foo#anchor'],
['foo/bar/index', '../../', '/'],
['foo/bar/index', '../..', '/'],
['foo/bar/', '../', '/foo/'],
['foo/bar/', '..', '/foo/'],
['foo/bar/', '../qux', '/foo/qux'],
];
}

Expand Down Expand Up @@ -92,9 +97,15 @@ public function relativePathProvider(): array
['/foo/bar/quux/index.md', '/foo/qux/corge/baz.md', '../../qux/corge/baz.md'],
['./foo/index.md', './bar/baz.md', './bar/baz.md'],
['foo/index.md', '/bar/baz.md', '../bar/baz.md'],
['/foo/bar/index.md', '/foo/qux/baz.md', '../qux/baz.md'],
['/foo/bar/baz/qux/index.md', '/foo/qux/bar/baz/baz.md', '../../../qux/bar/baz/baz.md'],
['./foo/index.md', '../bar/baz.md', '../bar/baz.md'],
['./foo/index.md', '../foo/baz.md', './baz.md'],
['./foo/index.md', '../foo/baz.md#anchor', './baz.md#anchor'],
['/foo/bar/index.md', '/foo/qux/', '../qux/'],
['/foo/bar/index.md', '../../', '../../'],
['/foo/bar/index.md', '..', '../'],
['/foo/bar/index.md', '../', '../'],
];
}

Expand Down

0 comments on commit d0925f2

Please sign in to comment.