1
0
Fork 0

Allow PreFileDownloadEvent to carry transport options for metadata (#9831)

pull/9848/head
Adam 2021-04-19 07:55:52 -04:00 committed by GitHub
parent c3b76a8532
commit e291aa1d12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 3 deletions

View File

@ -47,6 +47,11 @@ class PreFileDownloadEvent extends Event
*/
private $context;
/**
* @var array
*/
private $transportOptions = array();
/**
* Constructor.
*
@ -135,4 +140,28 @@ class PreFileDownloadEvent extends Event
{
return $this->context;
}
/**
* Returns transport options for the download.
*
* Only available for events with type metadata, for packages set the transport options on the package itself.
*
* @return array
*/
public function getTransportOptions()
{
return $this->transportOptions;
}
/**
* Sets transport options for the download.
*
* Only available for events with type metadata, for packages set the transport options on the package itself.
*
* @param array $options
*/
public function setTransportOptions(array $options)
{
$this->transportOptions = $options;
}
}

View File

@ -1088,13 +1088,16 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
$retries = 3;
while ($retries--) {
try {
$options = $this->options;
if ($this->eventDispatcher) {
$preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename, 'metadata', array('repository' => $this));
$preFileDownloadEvent->setTransportOptions($this->options);
$this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
$filename = $preFileDownloadEvent->getProcessedUrl();
$options = $preFileDownloadEvent->getTransportOptions();
}
$response = $this->httpDownloader->get($filename, $this->options);
$response = $this->httpDownloader->get($filename, $options);
$json = $response->getBody();
if ($sha256 && $sha256 !== hash('sha256', $json)) {
// undo downgrade before trying again if http seems to be hijacked or modifying content somehow
@ -1180,13 +1183,15 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
$retries = 3;
while ($retries--) {
try {
$options = $this->options;
if ($this->eventDispatcher) {
$preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename, 'metadata', array('repository' => $this));
$preFileDownloadEvent->setTransportOptions($this->options);
$this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
$filename = $preFileDownloadEvent->getProcessedUrl();
$options = $preFileDownloadEvent->getTransportOptions();
}
$options = $this->options;
if (isset($options['http']['header'])) {
$options['http']['header'] = (array) $options['http']['header'];
}
@ -1254,13 +1259,15 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
}
$httpDownloader = $this->httpDownloader;
$options = $this->options;
if ($this->eventDispatcher) {
$preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $this->httpDownloader, $filename, 'metadata', array('repository' => $this));
$preFileDownloadEvent->setTransportOptions($this->options);
$this->eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent);
$filename = $preFileDownloadEvent->getProcessedUrl();
$options = $preFileDownloadEvent->getTransportOptions();
}
$options = $this->options;
if ($lastModifiedTime) {
if (isset($options['http']['header'])) {
$options['http']['header'] = (array) $options['http']['header'];