Xinax\LaravelGettext\FileSystem::getRelativePath PHP Method

getRelativePath() public method

Return the relative path from a file or directory to anothe
Author: Laurent Goussard
public getRelativePath ( string $from, string $to ) : string
$from string
$to string
return string
    public function getRelativePath($from, $to)
    {
        // Compatibility fixes for Windows paths
        $from = is_dir($from) ? rtrim($from, '\\/') . '/' : $from;
        $to = is_dir($to) ? rtrim($to, '\\/') . '/' : $to;
        $from = str_replace('\\', '/', $from);
        $to = str_replace('\\', '/', $to);
        $from = explode('/', $from);
        $to = explode('/', $to);
        $relPath = $to;
        foreach ($from as $depth => $dir) {
            if ($dir !== $to[$depth]) {
                // Number of remaining directories
                $remaining = count($from) - $depth;
                if ($remaining > 1) {
                    // Add traversals up to first matching directory
                    $padLength = (count($relPath) + $remaining - 1) * -1;
                    $relPath = array_pad($relPath, $padLength, '..');
                    break;
                }
                $relPath[0] = './' . $relPath[0];
            }
            array_shift($relPath);
        }
        return implode('/', $relPath);
    }

Usage Example

 public function testGetRelativePath()
 {
     // dir/
     $from = __DIR__;
     $to = dirname(dirname(__DIR__));
     $result = $this->fileSystem->getRelativePath($to, $from);
     // Relative path from base path: unit/
     $this->assertSame("unit/", $result);
 }
All Usage Examples Of Xinax\LaravelGettext\FileSystem::getRelativePath