bug #16226 [filesystem] makeRelativePath does not work correctly from root (jaytaph, fabpot)

This PR was merged into the 2.3 branch.

Discussion
----------

[filesystem] makeRelativePath does not work correctly from root

| Q             | A
| ------------- | ---
| Bug fix?      | yes/no
| New feature?  | yes/no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #1234
| License       | MIT
| Doc PR        | #14066, #14067

When using `makeRelativePath`, it returns an incorrect path when trying to fetch an entry from the root:

      $fs->makePathRelative('/foo/bar/baz', '/');

Actual result:

      ../foo/bar/baz

Expected result:

      foo/bar/baz

As we have specified an absolute path, there is no point on having an `..` added. It works, because a root directory has a `..` which points to itself, but it could result in issues when the relative path is actually prefixed or concatted.

Commits
-------

791b124 fixed CS
7bb394e Added separated handling of root paths
This commit is contained in:
Fabien Potencier 2015-10-16 13:57:06 +02:00
commit 751f2bebdb
2 changed files with 9 additions and 2 deletions

View File

@ -330,8 +330,13 @@ class Filesystem
// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
$depth = count($startPathArr) - $index;
// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);
// When we need to traverse from the start, and we are starting from a root path, don't add '../'
if ('/' === $startPath[0] && 0 === $index && 1 === $depth) {
$traverser = '';
} else {
// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);
}
$endPathRemainder = implode('/', array_slice($endPathArr, $index));

View File

@ -781,6 +781,8 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
array('/a/aab/bb', '/a/aa/', '../aab/bb/'),
array('/a/aab/bb/', '/a/aa', '../aab/bb/'),
array('/a/aab/bb/', '/a/aa/', '../aab/bb/'),
array('/a/aab/bb/', '/', 'a/aab/bb/'),
array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
);
if ('\\' === DIRECTORY_SEPARATOR) {