VersionPress\Utils\PathUtils::getRelativePath PHP Метод

getRelativePath() публичный статический Метод

Calculates relative path from two absolute paths.
public static getRelativePath ( string $from, string $to ) : string
$from string It has to be a directory!
$to string Directory or file the relative path will lead to.
Результат string
    public static function getRelativePath($from, $to)
    {
        // Windows FTW!
        $from = self::windowsFix($from);
        $to = self::windowsFix($to);
        $from = preg_replace('~([^/]*)/+([^/]*)~', '$1/$2', $from);
        $to = preg_replace('~([^/]*)/+([^/]*)~', '$1/$2', $to);
        $from = rtrim($from, '/');
        $to = rtrim($to, '/');
        $from = explode('/', $from);
        $to = explode('/', $to);
        $from = self::realpath($from);
        $to = self::realpath($to);
        $depthOfCommonPath = self::countCommonDepth($from, $to);
        $relPath = array_slice($to, $depthOfCommonPath);
        // get number of remaining dirs up to $from
        $remaining = count($from) - $depthOfCommonPath;
        // add .. up to first matching dir
        $totalLengthOfRelativePath = count($relPath) + $remaining;
        $relPath = array_pad($relPath, $totalLengthOfRelativePath * -1, '..');
        return implode('/', $relPath);
    }

Usage Example

 public function prepare_activateTwoPlugins()
 {
     $plugin1Path = PathUtils::getRelativePath(self::$testConfig->testSite->path, self::$pluginInfo['zipfile']);
     $plugin2Path = PathUtils::getRelativePath(self::$testConfig->testSite->path, self::$secondPluginInfo['zipfile']);
     self::$wpAutomation->runWpCliCommand('plugin', 'install', [$plugin1Path, $plugin2Path]);
     $this->url(self::$wpAdminPath . "/plugins.php");
 }
All Usage Examples Of VersionPress\Utils\PathUtils::getRelativePath