Skip to content

Throw ResourceNotFoundException outside of re-defined error-handler #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
36 changes: 18 additions & 18 deletions src/JsonSchema/Uri/Retrievers/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,51 @@

/**
* Tries to retrieve JSON schemas from a URI using file_get_contents()
*
* @author Sander Coolen <[email protected]>
*
* @author Sander Coolen <[email protected]>
*/
class FileGetContents extends AbstractRetriever
{
protected $messageBody;

/**
* {@inheritDoc}
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
*/
public function retrieve($uri)
{
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
)));

set_error_handler(function() use ($uri) {
throw new ResourceNotFoundException('JSON schema not found at ' . $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);
}

if (false === $response) {
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
throw new ResourceNotFoundException('JSON schema not found at '.$uri);
}

if ($response == ''
&& substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
) {
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
throw new ResourceNotFoundException('JSON schema not found at '.$uri);
}

$this->messageBody = $response;
if (! empty($http_response_header)) {
if (!empty($http_response_header)) {
$this->fetchContentType($http_response_header);
} else {
// Could be a "file://" url or something else - fake up the response
$this->contentType = null;
}

return $this->messageBody;
}

/**
* @param array $headers HTTP Response Headers
* @return boolean Whether the Content-Type header was found or not
Expand All @@ -70,10 +70,10 @@ private function fetchContentType(array $headers)
return true;
}
}

return false;
}

/**
* @param string $header
* @return string|null
Expand Down