/
DownloadFactory.php
104 lines (92 loc) · 3.77 KB
/
DownloadFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace PhpBrew\Downloader;
use CLIFramework\Logger;
use GetOptionKit\OptionCollection;
use GetOptionKit\OptionResult;
class DownloadFactory
{
const METHOD_PHP_CURL = 'php_curl';
const METHOD_PHP_STREAM = 'php_stream';
const METHOD_WGET = 'wget';
const METHOD_CURL = 'curl';
private static $availableDownloaders = array(
self::METHOD_PHP_CURL => 'PhpBrew\Downloader\PhpCurlDownloader',
self::METHOD_PHP_STREAM => 'PhpBrew\Downloader\PhpStreamDownloader',
self::METHOD_WGET => 'PhpBrew\Downloader\WgetCommandDownloader',
self::METHOD_CURL => 'PhpBrew\Downloader\CurlCommandDownloader',
);
/**
* When php built-in extensions don't support openssl, we can use curl or wget instead.
*/
private static $fallbackDownloaders = array('curl', 'wget');
/**
* @param Logger $logger is used for creating downloader
* @param OptionResult $options options used for create downloader
* @param array $preferences Use downloader by preferences.
* @param bool $requireSsl
*
* @return BaseDownloader|null
*/
protected static function create(Logger $logger, OptionResult $options, array $preferences, $requireSsl = true)
{
foreach ($preferences as $prefKey) {
if (isset(self::$availableDownloaders[$prefKey])) {
$downloader = self::$availableDownloaders[$prefKey];
$down = new $downloader($logger, $options);
if ($down->hasSupport($requireSsl)) {
return $down;
}
}
}
return;
}
/**
* @param Logger $logger
* @param OptionResult $options
* @param string $downloader
*
* @return BaseDownloader
*/
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;
}
}
public static function addOptionsForCommand(OptionCollection $opts)
{
$opts->add('downloader:', 'Use alternative downloader.');
$opts->add('continue', 'Continue getting a partially downloaded file.');
$opts->add('http-proxy:', 'HTTP proxy address')
->valueName('Proxy host[:port]');
$opts->add('http-proxy-auth:', 'HTTP proxy authentication')
->valueName('Proxy username:password');
$opts->add(
'connect-timeout:',
'Connection timeout'
)
->valueName('Timeout in seconds');
return $opts;
}
}