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