Avoid weird recursion pattern by retrying after the first request is complete

main
Jordi Boggiano 11 years ago
parent 4d52900dff
commit 29fcca8595

@ -28,7 +28,7 @@ class RemoteFilesystem
private $originUrl; private $originUrl;
private $fileUrl; private $fileUrl;
private $fileName; private $fileName;
private $result; private $retry;
private $progress; private $progress;
private $lastProgress; private $lastProgress;
private $options; private $options;
@ -58,9 +58,7 @@ class RemoteFilesystem
*/ */
public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array()) public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array())
{ {
$this->get($originUrl, $fileUrl, $options, $fileName, $progress); return $this->get($originUrl, $fileUrl, $options, $fileName, $progress);
return $this->result;
} }
/** /**
@ -75,9 +73,7 @@ class RemoteFilesystem
*/ */
public function getContents($originUrl, $fileUrl, $progress = true, $options = array()) public function getContents($originUrl, $fileUrl, $progress = true, $options = array())
{ {
$this->get($originUrl, $fileUrl, $options, null, $progress); return $this->get($originUrl, $fileUrl, $options, null, $progress);
return $this->result;
} }
/** /**
@ -94,7 +90,6 @@ class RemoteFilesystem
protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true) protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true)
{ {
$this->bytesMax = 0; $this->bytesMax = 0;
$this->result = null;
$this->originUrl = $originUrl; $this->originUrl = $originUrl;
$this->fileUrl = $fileUrl; $this->fileUrl = $fileUrl;
$this->fileName = $fileName; $this->fileName = $fileName;
@ -134,7 +129,7 @@ class RemoteFilesystem
$errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')'; $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')';
} }
restore_error_handler(); restore_error_handler();
if (isset($e)) { if (isset($e) && !$this->retry) {
throw $e; throw $e;
} }
@ -190,12 +185,13 @@ class RemoteFilesystem
} }
} }
// avoid overriding if content was loaded by a sub-call to get() if ($this->retry) {
if (null === $this->result) { $this->retry = false;
$this->result = $result;
return $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress);
} }
if (false === $this->result) { if (false === $result) {
$e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode); $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode);
if (!empty($http_response_header[0])) { if (!empty($http_response_header[0])) {
$e->setHeaders($http_response_header); $e->setHeaders($http_response_header);
@ -203,6 +199,8 @@ class RemoteFilesystem
throw $e; throw $e;
} }
return $result;
} }
/** /**
@ -232,7 +230,8 @@ class RemoteFilesystem
$password = $this->io->askAndHideAnswer(' Password: '); $password = $this->io->askAndHideAnswer(' Password: ');
$this->io->setAuthentication($this->originUrl, $username, $password); $this->io->setAuthentication($this->originUrl, $username, $password);
$this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress); $this->retry = true;
throw new TransportException('RETRY');
break; break;
} }

Loading…
Cancel
Save