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;
}