|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of BraincraftedBootstrapBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2012-2013 by Florian Eckerstorfer |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Braincrafted\Bundle\BootstrapBundle\Util; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* PathUtil |
|
12
|
|
|
* |
|
13
|
|
|
* @package BraincraftedBootstrapBundle |
|
14
|
|
|
* @subpackage Util |
|
15
|
|
|
* @author Florian Eckerstorfer <[email protected]> |
|
16
|
|
|
* @copyright 2012-2013 Florian Eckerstorfer |
|
17
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
|
18
|
|
|
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2 |
|
19
|
|
|
*/ |
|
20
|
|
|
class PathUtil |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Returns the relative path $from to $to. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $from |
|
26
|
|
|
* @param string $to |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
* |
|
30
|
|
|
* @link http://stackoverflow.com/a/2638272/776654 |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getRelativePath($from, $to) |
|
33
|
|
|
{ |
|
34
|
|
|
// some compatibility fixes for Windows paths |
|
35
|
|
|
$from = is_dir($from) ? rtrim($from, '\/') . '/' : $from; |
|
36
|
|
|
$to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; |
|
37
|
|
|
$from = str_replace('\\', '/', $from); |
|
38
|
|
|
$to = str_replace('\\', '/', $to); |
|
39
|
|
|
|
|
40
|
|
|
$from = explode('/', $from); |
|
41
|
|
|
$to = explode('/', $to); |
|
42
|
|
|
$relPath = $to; |
|
43
|
|
|
|
|
44
|
|
|
foreach ($from as $depth => $dir) { |
|
45
|
|
|
// find first non-matching dir |
|
46
|
|
|
if ($dir === $to[$depth]) { |
|
47
|
|
|
// ignore this directory |
|
48
|
|
|
array_shift($relPath); |
|
49
|
|
|
} else { |
|
50
|
|
|
// get number of remaining dirs to $from |
|
51
|
|
|
$remaining = count($from) - $depth; |
|
52
|
|
|
if ($remaining > 1) { |
|
53
|
|
|
// add traversals up to first matching dir |
|
54
|
|
|
$padLength = (count($relPath) + $remaining - 1) * -1; |
|
55
|
|
|
$relPath = array_pad($relPath, $padLength, '..'); |
|
56
|
|
|
break; |
|
57
|
|
|
} else { |
|
58
|
|
|
$relPath[0] = './' . $relPath[0]; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return implode('/', $relPath); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|