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 */
2020class 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 (
32- 'method ' => 'GET ' ,
33- 'header ' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
34- )));
35-
36- set_error_handler (function () use ($ uri ) {
37- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
30+ $ errorMessage = null ;
31+ set_error_handler (function ($ errno , $ errstr ) use (&$ errorMessage ) {
32+ $ errorMessage = $ errstr ;
3833 });
3934 $ response = file_get_contents ($ uri );
4035 restore_error_handler ();
4136
37+ if ($ errorMessage ) {
38+ throw new ResourceNotFoundException ($ errorMessage );
39+ }
40+
4241 if (false === $ response ) {
43- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
42+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
4443 }
44+
4545 if ($ response == ''
4646 && substr ($ uri , 0 , 7 ) == 'file:// ' && substr ($ uri , -1 ) == '/ '
4747 ) {
48- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
48+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
4949 }
5050
5151 $ this ->messageBody = $ response ;
52- if (! empty ($ http_response_header )) {
52+ if (!empty ($ http_response_header )) {
5353 $ this ->fetchContentType ($ http_response_header );
5454 } else {
5555 // Could be a "file://" url or something else - fake up the response
5656 $ this ->contentType = null ;
5757 }
58-
58+
5959 return $ this ->messageBody ;
6060 }
61-
61+
6262 /**
6363 * @param array $headers HTTP Response Headers
6464 * @return boolean Whether the Content-Type header was found or not
@@ -70,10 +70,10 @@ private function fetchContentType(array $headers)
7070 return true ;
7171 }
7272 }
73-
73+
7474 return false ;
7575 }
76-
76+
7777 /**
7878 * @param string $header
7979 * @return string|null
0 commit comments