Skip to content

Commit 449bb4d

Browse files
committed
Merge pull request #271 from justinrainbow/revert-262-uri-retriever-error-handler
Revert "Do not override error handler"
2 parents 6dad7bf + 5292a93 commit 449bb4d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/JsonSchema/Uri/Retrievers/FileGetContents.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,34 @@
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-
if (!file_exists($uri)) {
31-
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
32-
}
30+
$context = stream_context_create(array(
31+
'http' => array(
32+
'method' => 'GET',
33+
'header' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
34+
)));
3335

36+
set_error_handler(function() use ($uri) {
37+
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
38+
});
3439
$response = file_get_contents($uri);
40+
restore_error_handler();
3541

3642
if (false === $response) {
37-
throw new ResourceNotFoundException('JSON schema was not retrieved at ' . $uri);
43+
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
3844
}
39-
4045
if ($response == ''
4146
&& substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
4247
) {
@@ -50,10 +55,10 @@ public function retrieve($uri)
5055
// Could be a "file://" url or something else - fake up the response
5156
$this->contentType = null;
5257
}
53-
58+
5459
return $this->messageBody;
5560
}
56-
61+
5762
/**
5863
* @param array $headers HTTP Response Headers
5964
* @return boolean Whether the Content-Type header was found or not
@@ -65,10 +70,10 @@ private function fetchContentType(array $headers)
6570
return true;
6671
}
6772
}
68-
73+
6974
return false;
7075
}
71-
76+
7277
/**
7378
* @param string $header
7479
* @return string|null

0 commit comments

Comments
 (0)