Skip to content

Commit

Permalink
Restore BC in Locator::__construct()
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 authored and jtojnar committed May 30, 2023
1 parent 509dc63 commit ea32232
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,36 @@ private function flatten_headers(array $headers): array
return implode(',', $values);
}, $headers);
}

/**
* Create a File instance from another Response
*
* For BC reasons in some places there MUST be a `File` instance
* instead of a `Response` implementation
*
* @see Locator::__construct()
* @internal
*/
final public static function fromResponse(Response $response): self
{
$headers = [];

foreach ($response->get_headers() as $name => $header) {
$headers[$name] = implode(', ', $header);
}

/** @var File */
$file = (new \ReflectionClass(File::class))->newInstanceWithoutConstructor();

$file->url = $response->get_final_requested_uri();
$file->useragent = null;
$file->headers = $headers;
$file->body = $response->get_body_content();
$file->status_code = $response->get_status_code();
$file->permanent_url = $response->get_permanent_uri();

return $file;
}
}

class_alias('SimplePie\File', 'SimplePie_File');
2 changes: 1 addition & 1 deletion src/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Locator implements RegistryAware
private $http_client = null;

public function __construct(
/* File */ $file,
File $file,
$timeout = 10,
$useragent = null,
$max_checked_feeds = 10,
Expand Down
2 changes: 1 addition & 1 deletion src/SimplePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ protected function fetch_data(&$cache)
if (!$this->force_feed) {
// Check if the supplied URL is a feed, if it isn't, look for it.
$locate = $this->registry->create(Locator::class, [
&$file,
(! $file instanceof File) ? File::fromResponse($file) : $file,
$this->timeout,
$this->useragent,
$this->max_checked_feeds,
Expand Down

0 comments on commit ea32232

Please sign in to comment.