1 | <?php |
||
17 | class CurlRemoteFilesystem extends Util\RemoteFilesystem |
||
18 | { |
||
19 | protected $io; |
||
20 | protected $config; |
||
21 | protected $options; |
||
22 | |||
23 | protected $retryAuthFailure = true; |
||
24 | |||
25 | protected $pluginConfig; |
||
26 | |||
27 | private $_lastHeaders = array(); |
||
28 | |||
29 | // global flags |
||
30 | private $_retry = false; |
||
31 | |||
32 | /** @var Aspects\JoinPoint */ |
||
33 | public $onPreDownload; |
||
34 | |||
35 | /** @var Aspects\JoinPoint */ |
||
36 | public $onPostDownload; |
||
37 | |||
38 | /** |
||
39 | * @param IO\IOInterface $io |
||
40 | * @param CConfig $config |
||
41 | * @param array $options |
||
42 | */ |
||
43 | 10 | public function __construct(IO\IOInterface $io, CConfig $config, array $options = array()) |
|
49 | |||
50 | 10 | public function setPluginConfig(array $pluginConfig) |
|
54 | |||
55 | /** |
||
56 | * Copy the remote file in local. |
||
57 | * |
||
58 | * @param string $origin host/domain text |
||
59 | * @param string $fileUrl targeturl |
||
60 | * @param string $fileName the local filename |
||
61 | * @param bool $progress Display the progression |
||
62 | * @param array $options Additional context options |
||
63 | * |
||
64 | * @return bool true |
||
65 | */ |
||
66 | 4 | public function copy($origin, $fileUrl, $fileName, $progress = true, $options = array()) |
|
86 | |||
87 | /** |
||
88 | * Get the content. |
||
89 | * |
||
90 | * @param string $origin The origin URL |
||
91 | * @param string $fileUrl The file URL |
||
92 | * @param bool $progress Display the progression |
||
93 | * @param array $options Additional context options |
||
94 | * |
||
95 | * @return bool|string The content |
||
96 | */ |
||
97 | 2 | public function getContents($origin, $fileUrl, $progress = true, $options = array()) |
|
109 | |||
110 | /** |
||
111 | * @param string $origin |
||
112 | * @param string $fileUrl |
||
113 | * @param boolean $progress |
||
114 | * @param \Closure $exec |
||
115 | */ |
||
116 | 6 | protected function fetch($origin, $fileUrl, $progress, $options, $exec) |
|
117 | { |
||
118 | do { |
||
119 | 6 | $this->_retry = false; |
|
120 | |||
121 | 6 | $request = Factory::getHttpGetRequest($origin, $fileUrl, $this->io, $this->config, $this->pluginConfig); |
|
122 | 6 | $this->onPreDownload = Factory::getPreEvent($request); |
|
123 | 6 | $this->onPostDownload = Factory::getPostEvent($request); |
|
124 | |||
125 | 6 | $options += $this->options; |
|
126 | 6 | $request->processRFSOption($options); |
|
127 | |||
128 | 6 | if ($this->io->isDebug()) { |
|
129 | 1 | $this->io->write('Downloading ' . $fileUrl); |
|
130 | 1 | } |
|
131 | |||
132 | 6 | if ($progress) { |
|
133 | 4 | $this->io->write(" Downloading: <comment>Connecting...</comment>", false); |
|
134 | 4 | $request->curlOpts[CURLOPT_NOPROGRESS] = false; |
|
135 | 4 | $request->curlOpts[CURLOPT_PROGRESSFUNCTION] = array($this, 'progress'); |
|
136 | 4 | } else { |
|
137 | 2 | $request->curlOpts[CURLOPT_NOPROGRESS] = true; |
|
138 | 2 | $request->curlOpts[CURLOPT_PROGRESSFUNCTION] = null; |
|
139 | } |
||
140 | |||
141 | 6 | $this->onPreDownload->notify(); |
|
142 | |||
143 | 6 | $opts = $request->getCurlOpts(); |
|
144 | 6 | $ch = Factory::getConnection($origin, isset($opts[CURLOPT_USERPWD])); |
|
145 | |||
146 | 6 | curl_setopt_array($ch, $opts); |
|
147 | |||
148 | 6 | list($execStatus,) = $exec($ch, $request); |
|
149 | 4 | } while ($this->_retry); |
|
150 | |||
151 | 4 | if ($progress) { |
|
152 | 2 | $this->io->overwrite(" Downloading: <comment>100%</comment>"); |
|
153 | 2 | } |
|
154 | |||
155 | 4 | return $execStatus; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * Retrieve the options set in the constructor |
||
160 | * |
||
161 | * @return array Options |
||
162 | */ |
||
163 | 1 | public function getOptions() |
|
167 | |||
168 | /** |
||
169 | * Returns the headers of the last request |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | 1 | public function getLastHeaders() |
|
177 | |||
178 | /** |
||
179 | * @internal |
||
180 | * @param resource $ch |
||
181 | * @param Aspects\HttpGetRequest $request |
||
182 | * @return array {int, Aspects\HttpGetResponse} |
||
183 | */ |
||
184 | 6 | public function exec($ch, Aspects\HttpGetRequest $request) |
|
204 | |||
205 | /** |
||
206 | * @internal |
||
207 | */ |
||
208 | 4 | public function progress() |
|
226 | |||
227 | /** |
||
228 | * @internal |
||
229 | * @param resource $ch |
||
230 | * @param string $header |
||
231 | * @return int |
||
232 | */ |
||
233 | 6 | public function processHeader($ch, $header) |
|
238 | |||
239 | 3 | protected function promptAuth(Aspects\HttpGetRequest $req, Aspects\HttpGetResponse $res) |
|
244 | } |
||
245 |