Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/JsonSchema/Uri/Retrievers/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ class FileGetContents extends AbstractRetriever
*/
public function retrieve($uri)
{
$errorMessage = null;
set_error_handler(function ($errno, $errstr) use (&$errorMessage) {
$errorMessage = $errstr;
});
$response = file_get_contents($uri);
restore_error_handler();

if ($errorMessage) {
throw new ResourceNotFoundException($errorMessage);
}
$response = $this->getContents($uri);

if (false === $response) {
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
Expand All @@ -61,14 +52,35 @@ public function retrieve($uri)
return $this->messageBody;
}

/**
* @param string $uri
*
* @return string|bool
*/
protected function getContents($uri)
{
$errorMessage = null;
set_error_handler(function ($errno, $errstr) use (&$errorMessage) {
$errorMessage = $errstr;
});
$response = file_get_contents($uri);
restore_error_handler();

if ($errorMessage) {
throw new ResourceNotFoundException($errorMessage);
}

return $response;
}

/**
* @param array $headers HTTP Response Headers
*
* @return bool Whether the Content-Type header was found or not
*/
private function fetchContentType(array $headers)
{
foreach (array_reverse($headers) as $header) {
foreach ($headers as $header) {
if ($this->contentType = self::getContentTypeMatchInHeader($header)) {
return true;
}
Expand Down