Makes given path relative to the given root directory.

@param string $path
@param string $rootDirectory
@return string
@throws DataHandlingException Thrown when the path is not part of the given root directory.
423276
public static function makePathRelative($path, $rootDirectory)
    {
        $rootDirectory = self::formatDirectory($rootDirectory);
        $path = self::formatPath($path);

        if (!self::startsWith(self::formatDirectory($path), $rootDirectory)) {
            throw new DataHandlingException("Path is not part of the given root directory.");
        }

        return substr_replace($path, "", 0, strlen($rootDirectory));
    }
566845
public static function pickAppDir($path)
    {
        // when relational path.
        if ($path[0] !== '/') return $path;

        $root = null;
        foreach (self::$namespaces as $p => $ns) {
            if (strpos($path, $p) === 0) {
                if ($root === null || strlen($root) < strlen($p)) $root = $p;
            }
        }

        return $root ? $root : $path;
    }
19348
public static function root($path = null)
    {
        if (! is_null($path)) {
            if (! $path instanceof KhttpUrl) {
                $path = AnService::get('anahita:http.url', array('url' => $path));
            }

            self::$_root = $path;
        }

        if (is_null(self::$_root)) {
            self::$_root = self::$_base;
        }

        return self::$_root;
    }
457358
public function getRootApplicationDirectoryPathByFilePath(string $filePath): string
    {
        if ($this->isApplicationRootDefined()) {
            return ${static::CONST_NAME_APPLICATION_ROOT_DIR};
        }

        $vendorPosition = strpos($filePath, 'vendor');

        if ($vendorPosition !== false) {
            return substr($filePath, 0, $vendorPosition);
        }

        $sourcePosition = strpos($filePath, 'src');

        return substr($filePath, 0, $sourcePosition);
    }
1081072
protected function findRelativePath($path)
    {
        if (null !== $this->basePath) {
            return strtr($path, array($this->basePath . DIRECTORY_SEPARATOR => ''));
        }

        return $path;
    }
163595
public function dataDir($path = '')
    {
        $path = (empty($path) || starts_with($path, DIRECTORY_SEPARATOR)) ? $path : "/{$path}";

        return __COUNTRIES_DIR__.$this->toDir("/src/data$path");
    }
984106
public static function getUrlPathFromFilePath($filePath, $relativeToRootUrl = false) {
		$dir = $filePath;
		$rootPath = ROOT_PATH;
		
		if (strpos($dir, ROOT_PATH) === 0) {
			$dirUrl = ROOT_URL.substr(ROOT_PATH, strlen(__DIR__)+1);
		} else {
			// The directory URL is below the ROOT_URL. Let's try to find it (not 100% success rate)
			$rootPaths = explode(DIRECTORY_SEPARATOR, trim($rootPath, DIRECTORY_SEPARATOR));
			$thisDirs = explode(DIRECTORY_SEPARATOR, trim($dir, DIRECTORY_SEPARATOR));
		
			for ($i=0; $i<count($rootPaths); $i++) {
				if ($rootPaths[$i] != $thisDirs[$i]) {
					break;
				}
			}
		
			$nbSkip = count($rootPaths) - $i;
			$urls = explode('/', trim(ROOT_URL, '/'));
		
			if ($nbSkip > count($urls)) {
				throw new Exception('The directory "'.$dir.'" is almost certainly out of reach from the web.');
			}
		
			$dirUrls = array();
			for ($j=0; $j < count($urls)- $nbSkip; $j++) {
				$dirUrls[] = $urls[$j];
			}
			for ($k = $i; $k<count($thisDirs); $k++) {
				$dirUrls[] = $thisDirs[$k];
			}
			$dirUrl = '/'.implode('/', $dirUrls);
			
			if ($relativeToRootUrl) {
				$dirUrl = ltrim(str_repeat('/..', count($urls)).$dirUrl, '/');
			}
		}
		return $dirUrl;
	}
281358
static public function buildAbsolutePath($relative_path, $basepath) {
        if (strpos($relative_path, static::PATH_SEGMENT_SEPARATOR) === 0) {
            return static::normalizePath($relative_path);
        }
        $basedir = static::dirname($basepath);
        if ($basedir == '.' || $basedir == static::PATH_SEGMENT_SEPARATOR || $basedir == '\\' || $basedir == DIRECTORY_SEPARATOR) $basedir = '';
        return static::normalizePath($basedir . self::PATH_SEGMENT_SEPARATOR . $relative_path);
    }
1024322
public function parsePath(?string $path = null): ?string
    {
        if ($path === null) {
            return null;
        }
        $path = \str_replace('%DOCUMENT_ROOT%', $this->documentRoot, $path);
        $path = \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
        return $path;
    }
1006938
public function parent() {
        $path = $this->path;

        array_pop($path);

        if (empty($path)) {
            throw new \RuntimeException("Can't get parent of root path");
        }

        return new static($path);
    }
397016
final private static function getDirectoryStructureDo($relativePath, $rootDirectory, $exclude3rdParty)
    {
        $thisRoot = $rootDirectory;
        if ($relativePath !== '') {
            $thisRoot .= '/'.$relativePath;
        }
        $subDirs = array();
        $hDir = @opendir($thisRoot);
        if ($hDir === false) {
            throw new \Exception("Unable to open directory $rootDirectory");
        }
        while (($entry = @readdir($hDir)) !== false) {
            if ($entry[0] === '.') {
                continue;
            }
            $fullPath = $thisRoot.'/'.$entry;
            if (!is_dir($fullPath)) {
                continue;
            }
            if ($exclude3rdParty) {
                if (($entry === 'vendor') || preg_match('%/libraries/3rdparty$%', $fullPath)) {
                    continue;
                }
            }
            $subDirs[] = $entry;
        }
        @closedir($hDir);
        $result = array();
        foreach ($subDirs as $subDir) {
            $rel = ($relativePath === '') ? $subDir : "$relativePath/$subDir";
            $result = array_merge($result, static::getDirectoryStructureDo($rel, $rootDirectory, $exclude3rdParty));
            $result[] = $rel;
        }

        return $result;
    }
949964
public static function rootPath($directory) {
        $num = count(explode("/", $directory));
        $path = "";
        for ($i = 0; $i < $num; $i++)
            $path = self::join("..", $path);
        return $path;
    }
1032342
public function preparePathDirectory(string $path): string
    {
        $translatedPath = Yii::getAlias($path);

        if (!is_dir($translatedPath)) {
            FileHelper::createDirectory($translatedPath);
        }

        return $translatedPath;
    }
906929
public static function pickRootDir($path)
    {
        // when relational path.
        if ($path[0] !== '/') return $path;
        
        $f = function($names, $count) {
            for ($i = 0; $i < $count; $i++) {
                array_pop($names);
            }
            return $names;
        };

        $root = null;
        foreach (self::$namespaces as $p => $ns) {
            $d = join(DS, $f(explode(DS, $p), count(explode('\\', $ns))));
            if (strpos($path, $d) === 0) {
                if ($root === null || strlen($root) < strlen($d)) $root = $d;
            }
        }

        return $root ? $root : $path;
    }
707398
public function getMainPrefixAndPath(Dir $rootDir) {
    $prefixesPaths = $this->getPrefixes();
    $prefixes = array_keys($prefixesPaths);
    
    if (count($prefixes) > 0) {
      $firstPrefixPaths = array_shift($prefixesPaths);

      $path = array_shift($firstPrefixPaths);
      $dir = $path instanceof Dir ? $path : $rootDir->sub($path);
    
      return array(array_shift($prefixes), $dir);
    }
    
    throw new NoAutoLoadPrefixException('Cannot retrieve the main Prefix and Library Path from AutoLoadInfo. AutoLoadInfo is defined: '.print_r($prefixesPaths, true)."\nPlease insert autoload informations into your composer.json");
  }
1032454
protected function _getRalativePath($path)
    {
        if ($path) {
            $rootPath = str_replace(array('/', '\\'), '/', self::$_config['root']);

            $path = str_replace(array('/', '\\'), '/', $path);
            $path = str_replace($rootPath, '/', $path);
            $path = str_replace('//', '/', $path);
            $path = trim($path, '/');
        }
        return $path;
    }
278756
public function relative_file_root_path()
    {
        $subDirs = implode('/', $this->subDirs);
        if ($subDirs) {
            $subDirs .= '/';
        }
        return $subDirs . $this->fileRoot;
    }