Platformsh\Cli\Local\LocalProject::findTopDirectoryContaining PHP Method

findTopDirectoryContaining() protected static method

Find the highest level directory that contains a file.
protected static findTopDirectoryContaining ( string $file, callable $callback = null ) : string | false
$file string The filename to look for.
$callback callable A callback to validate the directory when found. Accepts one argument (the directory path). Return true to use the directory, or false to continue traversing upwards.
return string | false The path to the directory, or false if the file is not found.
    protected static function findTopDirectoryContaining($file, callable $callback = null)
    {
        static $roots = [];
        $cwd = getcwd();
        if ($cwd === false) {
            return false;
        }
        if (isset($roots[$cwd][$file])) {
            return $roots[$cwd][$file];
        }
        $roots[$cwd][$file] = false;
        $root =& $roots[$cwd][$file];
        $currentDir = $cwd;
        while (!$root) {
            if (file_exists($currentDir . '/' . $file)) {
                if ($callback === null || $callback($currentDir)) {
                    $root = $currentDir;
                    break;
                }
            }
            // The file was not found, go one directory up.
            $levelUp = dirname($currentDir);
            if ($levelUp === $currentDir || $levelUp === '.') {
                break;
            }
            $currentDir = $levelUp;
        }
        return $root;
    }