|
@@ -31,16 +31,17 @@ class Converter implements ConverterInterface
|
|
|
/**
|
|
/**
|
|
|
* @param string $from The original base path (directory, not file!)
|
|
* @param string $from The original base path (directory, not file!)
|
|
|
* @param string $to The new base path (directory, not file!)
|
|
* @param string $to The new base path (directory, not file!)
|
|
|
|
|
+ * @param string $root Root directory (defaults to `getcwd`)
|
|
|
*/
|
|
*/
|
|
|
- public function __construct($from, $to)
|
|
|
|
|
|
|
+ public function __construct($from, $to, $root = '')
|
|
|
{
|
|
{
|
|
|
$shared = $this->shared($from, $to);
|
|
$shared = $this->shared($from, $to);
|
|
|
if ($shared === '') {
|
|
if ($shared === '') {
|
|
|
// when both paths have nothing in common, one of them is probably
|
|
// when both paths have nothing in common, one of them is probably
|
|
|
// absolute while the other is relative
|
|
// absolute while the other is relative
|
|
|
- $cwd = getcwd();
|
|
|
|
|
- $from = strpos($from, $cwd) === 0 ? $from : $cwd.'/'.$from;
|
|
|
|
|
- $to = strpos($to, $cwd) === 0 ? $to : $cwd.'/'.$to;
|
|
|
|
|
|
|
+ $root = $root ?: getcwd();
|
|
|
|
|
+ $from = strpos($from, $root) === 0 ? $from : preg_replace('/\/+/', '/', $root.'/'.$from);
|
|
|
|
|
+ $to = strpos($to, $root) === 0 ? $to : preg_replace('/\/+/', '/', $root.'/'.$to);
|
|
|
|
|
|
|
|
// or traveling the tree via `..`
|
|
// or traveling the tree via `..`
|
|
|
// attempt to resolve path, or assume it's fine if it doesn't exist
|
|
// attempt to resolve path, or assume it's fine if it doesn't exist
|
|
@@ -70,6 +71,14 @@ class Converter implements ConverterInterface
|
|
|
// deal with different operating systems' directory structure
|
|
// deal with different operating systems' directory structure
|
|
|
$path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/');
|
|
$path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/');
|
|
|
|
|
|
|
|
|
|
+ // remove leading current directory.
|
|
|
|
|
+ if (substr($path, 0, 2) === './') {
|
|
|
|
|
+ $path = substr($path, 2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // remove references to current directory in the path.
|
|
|
|
|
+ $path = str_replace('/./', '/', $path);
|
|
|
|
|
+
|
|
|
/*
|
|
/*
|
|
|
* Example:
|
|
* Example:
|
|
|
* /home/forkcms/frontend/cache/compiled_templates/../../core/layout/css/../images/img.gif
|
|
* /home/forkcms/frontend/cache/compiled_templates/../../core/layout/css/../images/img.gif
|
|
@@ -155,7 +164,7 @@ class Converter implements ConverterInterface
|
|
|
$to = mb_substr($this->to, mb_strlen($shared));
|
|
$to = mb_substr($this->to, mb_strlen($shared));
|
|
|
|
|
|
|
|
// add .. for every directory that needs to be traversed to new path
|
|
// add .. for every directory that needs to be traversed to new path
|
|
|
- $to = str_repeat('../', mb_substr_count($to, '/'));
|
|
|
|
|
|
|
+ $to = str_repeat('../', count(array_filter(explode('/', $to))));
|
|
|
|
|
|
|
|
return $to.ltrim($path, '/');
|
|
return $to.ltrim($path, '/');
|
|
|
}
|
|
}
|