@@ -358,34 +358,37 @@ public function makePathRelative($endPath, $startPath)
358
358
$ startPath = str_replace ('\\' , '/ ' , $ startPath );
359
359
}
360
360
361
+ $ stripDriveLetter = function ($ path ) {
362
+ if (strlen ($ path ) > 2 && ': ' === $ path [1 ] && '/ ' === $ path [2 ] && ctype_alpha ($ path [0 ])) {
363
+ return substr ($ path , 2 );
364
+ }
365
+
366
+ return $ path ;
367
+ };
368
+
369
+ $ endPath = $ stripDriveLetter ($ endPath );
370
+ $ startPath = $ stripDriveLetter ($ startPath );
371
+
361
372
// Split the paths into arrays
362
373
$ startPathArr = explode ('/ ' , trim ($ startPath , '/ ' ));
363
374
$ endPathArr = explode ('/ ' , trim ($ endPath , '/ ' ));
364
375
365
- if ('/ ' !== $ startPath [0 ]) {
366
- array_shift ($ startPathArr );
367
- }
368
-
369
- if ('/ ' !== $ endPath [0 ]) {
370
- array_shift ($ endPathArr );
371
- }
372
-
373
- $ normalizePathArray = function ($ pathSegments ) {
376
+ $ normalizePathArray = function ($ pathSegments , $ absolute ) {
374
377
$ result = array ();
375
378
376
379
foreach ($ pathSegments as $ segment ) {
377
- if ('.. ' === $ segment ) {
380
+ if ('.. ' === $ segment && ($ absolute || count ( $ result )) ) {
378
381
array_pop ($ result );
379
- } else {
382
+ } elseif ( ' . ' !== $ segment ) {
380
383
$ result [] = $ segment ;
381
384
}
382
385
}
383
386
384
387
return $ result ;
385
388
};
386
389
387
- $ startPathArr = $ normalizePathArray ($ startPathArr );
388
- $ endPathArr = $ normalizePathArray ($ endPathArr );
390
+ $ startPathArr = $ normalizePathArray ($ startPathArr, static :: isAbsolutePath ( $ startPath ) );
391
+ $ endPathArr = $ normalizePathArray ($ endPathArr, static :: isAbsolutePath ( $ endPath ) );
389
392
390
393
// Find for which directory the common path stops
391
394
$ index = 0 ;
@@ -394,19 +397,14 @@ public function makePathRelative($endPath, $startPath)
394
397
}
395
398
396
399
// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
397
- if (count ($ startPathArr ) === 1 && $ startPathArr [0 ] === '' ) {
400
+ if (1 === count ($ startPathArr ) && '' === $ startPathArr [0 ]) {
398
401
$ depth = 0 ;
399
402
} else {
400
403
$ depth = count ($ startPathArr ) - $ index ;
401
404
}
402
405
403
- // When we need to traverse from the start, and we are starting from a root path, don't add '../'
404
- if ('/ ' === $ startPath [0 ] && 0 === $ index && 0 === $ depth ) {
405
- $ traverser = '' ;
406
- } else {
407
- // Repeated "../" for each level need to reach the common path
408
- $ traverser = str_repeat ('../ ' , $ depth );
409
- }
406
+ // Repeated "../" for each level need to reach the common path
407
+ $ traverser = str_repeat ('../ ' , $ depth );
410
408
411
409
$ endPathRemainder = implode ('/ ' , array_slice ($ endPathArr , $ index ));
412
410
@@ -500,7 +498,7 @@ public function isAbsolutePath($file)
500
498
{
501
499
return strspn ($ file , '/ \\' , 0 , 1 )
502
500
|| (strlen ($ file ) > 3 && ctype_alpha ($ file [0 ])
503
- && substr ($ file , 1 , 1 ) === ' : '
501
+ && ' : ' === substr ($ file , 1 , 1 )
504
502
&& strspn ($ file , '/ \\' , 2 , 1 )
505
503
)
506
504
|| null !== parse_url ($ file , PHP_URL_SCHEME )
0 commit comments