public static function getInstance(Logger $logger, OptionResult $options, $downloader = null)
{
if (is_string($downloader)) {
//if we specific a downloader class clearly, then it's the only choice
if (class_exists($downloader) && is_subclass_of($downloader, 'PhpBrew\\Downloader\\BaseDownloader')) {
return new $downloader($logger, $options);
}
$downloader = array($downloader);
}
if (empty($downloader)) {
$downloader = array_keys(self::$availableDownloaders);
}
//if --downloader presents, we will use it as the first choice, even if the caller specific downloader by alias/array
if ($options->has('downloader')) {
$logger->info("Found --downloader option, try to use {$options->downloader} as default downloader.");
$downloader = array_merge(array($options->downloader), $downloader);
}
$instance = self::create($logger, $options, $downloader);
if ($instance === null) {
$logger->debug('Downloader not found, falling back to command-based downloader.');
//if all downloader not available, maybe we should throw exceptions here instead of returning null?
return self::create($logger, $options, self::$fallbackDownloaders);
} else {
return $instance;
}
}