Skip to content

Commit

Permalink
Merge branch 'feature/AGRO/886' into 'master'
Browse files Browse the repository at this point in the history
Revert "AGRO-886 Revert last changes because error: There is not directory . Nothing to symlink."

See merge request !12
  • Loading branch information
Татьяна Порай committed Feb 9, 2018
2 parents 15f1d8e + 7330b6c commit 392eed5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ public function findShortestPath($from, $to, $directories = false)
$commonPath = rtrim($commonPath, $this->dirSeparator) . $this->dirSeparator;
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), $this->dirSeparator);
$commonPathCode = str_repeat(self::DIRECTORY_UP . $this->dirSeparator, $sourcePathDepth);

$shortestPath = ($commonPathCode . substr($to, strlen($commonPath))) ?
: self::DIRECTORY_CURRENT . $this->dirSeparator;
$commonPathWithCode = $commonPathCode . substr($to, strlen($commonPath));
$shortestPath = $this->getCurrentDirectoryForPath() . $commonPathWithCode;
}
}
return $shortestPath;
Expand All @@ -238,16 +237,21 @@ public function findShortestPath($from, $to, $directories = false)
public function relativeSymlink($target, $link)
{
$cwd = getcwd();

$relativePath = $this->findShortestPath($link, $target);
chdir(dirname($link));
$linkPath = $link;
if (!$this->isAbsolutePath($link) && $this->isAbsolutePath($target)) {
$linkPath = preg_replace("#^\." . $this->dirSeparator . "#", '', $link);
$linkPath = $target . $this->dirSeparator . $linkPath;
$linkPath = $this->normalizePath($linkPath);
}
$relativePath = $this->findShortestPath($linkPath, $target);
chdir(dirname($target));
echo "$relativePath <- $linkPath\n";
if ($this->isOSWindows()) {
$command = 'mklink /d';
exec("$command $link $relativePath", $output, $returnVar);
$result = $returnVar > 0 ? false : true;
exec("$command $linkPath $relativePath", $output, $returnVar);
$result = ($returnVar == 0);
} else {
echo "$relativePath <- $link\n";
$result = symlink($relativePath, $link);
$result = symlink($relativePath, $linkPath);
}
chdir($cwd);
return (bool)$result;
Expand All @@ -262,4 +266,14 @@ private function isOSWindows()
{
return strtoupper(substr(PHP_OS, 0, strlen('WIN'))) === 'WIN';
}

/**
* Get current directory for path.
*
* @return string
*/
private function getCurrentDirectoryForPath()
{
return self::DIRECTORY_CURRENT . $this->dirSeparator;
}
}
18 changes: 18 additions & 0 deletions tests/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,22 @@ public function testRelativeSymlink()
'Check relative link after move'
);
}

/**
* Testing relativeSymlink for link.
*
* @return void
*/
public function testRelativeSymlinkForDir()
{
$fileSystem = new FileSystem();
$dirTest = sys_get_temp_dir() . DIRECTORY_SEPARATOR . __FUNCTION__;
$linkDir = $dirTest . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR;
$fileSystem->ensureDirectoryExists($linkDir);
$link = $fileSystem->findShortestPath($dirTest, $linkDir . 'link');
$fileSystem->relativeSymlink($dirTest, $link);
$this->assertTrue(file_exists($linkDir));
$fileSystem->emptyDirectory($dirTest);
$fileSystem->deleteByPath($dirTest);
}
}

0 comments on commit 392eed5

Please sign in to comment.