Todaymade\Daux\DauxHelper::getRelativePath PHP Method

getRelativePath() public static method

public static getRelativePath ( string $from, string $to ) : string
$from string
$to string
return string
    public static function getRelativePath($from, $to)
    {
        // some 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) {
            // find first non-matching dir
            if ($dir === $to[$depth]) {
                // ignore this directory
                array_shift($relPath);
            } else {
                // get number of remaining dirs to $from
                $remaining = count($from) - $depth;
                if ($remaining > 1) {
                    // add traversals up to first matching dir
                    $padLength = (count($relPath) + $remaining - 1) * -1;
                    $relPath = array_pad($relPath, $padLength, '..');
                    break;
                } else {
                    //$relPath[0] = './' . $relPath[0];
                }
            }
        }
        return implode('/', $relPath);
    }

Usage Example

Beispiel #1
0
 /**
  * @param AbstractInline|Link $inline
  * @param ElementRendererInterface $htmlRenderer
  * @return HtmlElement
  * @throws Exception
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     // This can't be in the method type as
     // the method is an abstract and should
     // have the same interface
     if (!$inline instanceof Link) {
         throw new \RuntimeException('Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ . " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . get_class($inline) . "' was provided");
     }
     $element = parent::render($inline, $htmlRenderer);
     $url = $inline->getUrl();
     // Absolute urls, empty urls and anchors
     // should not go through the url resolver
     if (empty($url) || $url[0] == '#' || preg_match('|^(?:[a-z]+:)?//|', $url)) {
         return $element;
     }
     $file = $this->resolveInternalFile($url);
     $url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
     $element->setAttribute('href', $url);
     return $element;
 }
All Usage Examples Of Todaymade\Daux\DauxHelper::getRelativePath