Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
moved makePathRelative to Filesystem
  • Loading branch information
fabpot committed Sep 28, 2011
1 parent 8cb0cc6 commit 258a1fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
Expand Up @@ -86,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filesystem->remove($targetDir);

if ($input->getOption('symlink')) {
$relativeOriginDir = $this->makePathRelative($originDir, realpath($bundlesDir));
$relativeOriginDir = $filesystem->makePathRelative($originDir, realpath($bundlesDir));
$filesystem->symlink($relativeOriginDir, $targetDir);
} else {
$filesystem->mkdir($targetDir, 0777);
Expand All @@ -96,31 +96,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}
}

/**
* Given an existing path, convert it to a path relative to a given starting path
*
* @var string Absolute path of target
* @var string Absolute path where traversal begins
* @return string Path of target relative to starting path
*/
protected function makePathRelative($endPath, $startPath)
{
// Find for which character the the common path stops
$offset = 0;
while ($startPath[$offset] === $endPath[$offset]) {
$offset++;
}

// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
$depth = substr_count(substr($startPath, $offset), DIRECTORY_SEPARATOR) + 1;

// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);

// Construct $endPath from traversing to the common path, then to the remaining $endPath
$relativePath = $traverser.substr($endPath, $offset);

return $relativePath;
}
}
26 changes: 26 additions & 0 deletions src/Symfony/Component/HttpKernel/Util/Filesystem.php
Expand Up @@ -168,6 +168,32 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false)
}
}

/**
* Given an existing path, convert it to a path relative to a given starting path
*
* @var string Absolute path of target
* @var string Absolute path where traversal begins
*
* @return string Path of target relative to starting path
*/
public function makePathRelative($endPath, $startPath)
{
// Find for which character the the common path stops
$offset = 0;
while ($startPath[$offset] === $endPath[$offset]) {
$offset++;
}

// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
$depth = substr_count(substr($startPath, $offset), DIRECTORY_SEPARATOR) + 1;

// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);

// Construct $endPath from traversing to the common path, then to the remaining $endPath
return $traverser.substr($endPath, $offset);
}

/**
* Mirrors a directory to another.
*
Expand Down

0 comments on commit 258a1fd

Please sign in to comment.