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;
    }
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;
    }
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 ; }
Find the highest level directory that contains a file.

@param string $file
The filename to look for.
@param callable $callback
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.
Find the highest level directory that contains a file .