Skip to content

Commit 3a8ef5c

Browse files
Throw ResourceNotFoundException outside of re-defined error-handler
1 parent 5c133a3 commit 3a8ef5c

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/JsonSchema/Uri/Retrievers/FileGetContents.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,73 @@
1414

1515
/**
1616
* Tries to retrieve JSON schemas from a URI using file_get_contents()
17-
*
18-
* @author Sander Coolen <[email protected]>
17+
*
18+
* @author Sander Coolen <[email protected]>
1919
*/
2020
class FileGetContents extends AbstractRetriever
2121
{
2222
protected $messageBody;
23-
23+
2424
/**
2525
* {@inheritDoc}
2626
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
2727
*/
2828
public function retrieve($uri)
2929
{
30-
$context = stream_context_create(array(
31-
'http' => array(
30+
$context = stream_context_create([
31+
'http' => [
3232
'method' => 'GET',
33-
'header' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
34-
)));
33+
'header' => "Accept: ".Validator::SCHEMA_MEDIA_TYPE,
34+
],
35+
]);
3536

36-
set_error_handler(function() use ($uri) {
37-
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
37+
$error = null;
38+
set_error_handler(function (
39+
$errno,
40+
$errstr,
41+
$errfile,
42+
$errline,
43+
array $errcontext
44+
) use (&$error) {
45+
$error = [
46+
$errno,
47+
$errstr,
48+
$errfile,
49+
$errline,
50+
$errcontext,
51+
];
3852
});
3953
$response = file_get_contents($uri);
4054
restore_error_handler();
4155

56+
if ($error) {
57+
list (, $errstr, $errfile, $errline) = $error;
58+
$message = sprintf('%s on line %s in %s', $errstr, $errline,
59+
$errfile);
60+
throw new ResourceNotFoundException($message);
61+
}
62+
4263
if (false === $response) {
43-
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
64+
throw new ResourceNotFoundException('JSON schema not found at '.$uri);
4465
}
66+
4567
if ($response == ''
4668
&& substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
4769
) {
48-
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
70+
throw new ResourceNotFoundException('JSON schema not found at '.$uri);
4971
}
5072

5173
$this->messageBody = $response;
52-
if (! empty($http_response_header)) {
74+
if (!empty($http_response_header)) {
5375
$this->fetchContentType($http_response_header);
5476
} else {
5577
// Could be a "file://" url or something else - fake up the response
5678
$this->contentType = null;
5779
}
58-
80+
5981
return $this->messageBody;
6082
}
61-
83+
6284
/**
6385
* @param array $headers HTTP Response Headers
6486
* @return boolean Whether the Content-Type header was found or not
@@ -70,10 +92,10 @@ private function fetchContentType(array $headers)
7092
return true;
7193
}
7294
}
73-
95+
7496
return false;
7597
}
76-
98+
7799
/**
78100
* @param string $header
79101
* @return string|null

0 commit comments

Comments
 (0)