Skip to content

Commit

Permalink
move getRelativePathTo to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
matthi4s committed Feb 4, 2025
1 parent 351b4f2 commit 4eac176
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
40 changes: 3 additions & 37 deletions src/System/FilesystemElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

use Aternos\IO\Exception\ChmodException;
use Aternos\IO\Exception\MoveException;
use Aternos\IO\Exception\PathOutsideElementException;
use Aternos\IO\Exception\StatException;
use Aternos\IO\Exception\TouchException;
use Aternos\IO\Interfaces\Features\GetPathInterface;
use Aternos\IO\Interfaces\Util\PermissionsInterface;
use Aternos\IO\System\Directory\Directory;
use Aternos\IO\System\File\File;
use Aternos\IO\System\Link\DirectoryLink;
use Aternos\IO\System\Link\FileLink;
use Aternos\IO\System\Link\Link;
use Aternos\IO\System\Util\GetRelativePathToTrait;
use Aternos\IO\System\Util\Permissions;

/**
Expand All @@ -25,6 +24,8 @@
*/
abstract class FilesystemElement implements FilesystemInterface
{
use GetRelativePathToTrait;

/**
* Get the matching filesystem element for a path
*
Expand Down Expand Up @@ -81,41 +82,6 @@ public function getPath(): string
return $this->path;
}

/**
* @inheritDoc
* @throws PathOutsideElementException
*/
public function getRelativePathTo(GetPathInterface $element, bool $allowOutsideElement = false): string
{
$sourcePath = $element->getPath();
$targetPath = $this->getPath();

$sourcePathParts = explode(DIRECTORY_SEPARATOR, $sourcePath);
$targetPathParts = explode(DIRECTORY_SEPARATOR, $targetPath);

foreach ($sourcePathParts as $key => $sourcePathPart) {
if (isset($targetPathParts[$key]) && $targetPathParts[$key] === $sourcePathPart) {
unset($sourcePathParts[$key]);
unset($targetPathParts[$key]);
continue;
}

if (!$allowOutsideElement) {
throw new PathOutsideElementException("Path is outside of element (" . $sourcePath . " -> " . $targetPath . ")", $this);
}
break;
}

$relativePath = "";
foreach ($sourcePathParts as $ignored) {
$relativePath .= ".." . DIRECTORY_SEPARATOR;
}
foreach ($targetPathParts as $targetPathPart) {
$relativePath .= $targetPathPart . DIRECTORY_SEPARATOR;
}
return rtrim($relativePath, DIRECTORY_SEPARATOR);
}

/**
* @inheritDoc
* @throws MoveException
Expand Down
57 changes: 57 additions & 0 deletions src/System/Util/GetRelativePathToTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Aternos\IO\System\Util;

use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\PathOutsideElementException;
use Aternos\IO\Interfaces\Features\GetPathInterface;

/**
* Trait GetRelativePathToTrait
*
* Implements getRelativePathTo method
*
* @package Aternos\IO\System\Util
*/
trait GetRelativePathToTrait
{
/**
* Get the relative path to another element
*
* @param GetPathInterface $element
* @param bool $allowOutsideElement Allow paths outside the element, throws an exception otherwise
* @return string
* @throws PathOutsideElementException
* @throws IOException
*/
public function getRelativePathTo(GetPathInterface $element, bool $allowOutsideElement = false): string
{
$sourcePath = $element->getPath();
$targetPath = $this->getPath();

$sourcePathParts = explode(DIRECTORY_SEPARATOR, $sourcePath);
$targetPathParts = explode(DIRECTORY_SEPARATOR, $targetPath);

foreach ($sourcePathParts as $key => $sourcePathPart) {
if (isset($targetPathParts[$key]) && $targetPathParts[$key] === $sourcePathPart) {
unset($sourcePathParts[$key]);
unset($targetPathParts[$key]);
continue;
}

if (!$allowOutsideElement) {
throw new PathOutsideElementException("Path is outside of element (" . $sourcePath . " -> " . $targetPath . ")", $this);
}
break;
}

$relativePath = "";
foreach ($sourcePathParts as $ignored) {
$relativePath .= ".." . DIRECTORY_SEPARATOR;
}
foreach ($targetPathParts as $targetPathPart) {
$relativePath .= $targetPathPart . DIRECTORY_SEPARATOR;
}
return rtrim($relativePath, DIRECTORY_SEPARATOR);
}
}

0 comments on commit 4eac176

Please sign in to comment.