14
14
15
15
/**
16
16
* 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] >
19
19
*/
20
20
class FileGetContents extends AbstractRetriever
21
21
{
22
22
protected $ messageBody ;
23
-
23
+
24
24
/**
25
25
* {@inheritDoc}
26
26
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve()
27
27
*/
28
28
public function retrieve ($ uri )
29
29
{
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 ;
38
33
});
39
34
$ response = file_get_contents ($ uri );
40
35
restore_error_handler ();
41
36
37
+ if ($ errorMessage ) {
38
+ throw new ResourceNotFoundException ($ errorMessage );
39
+ }
40
+
42
41
if (false === $ response ) {
43
- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
42
+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
44
43
}
44
+
45
45
if ($ response == ''
46
46
&& substr ($ uri , 0 , 7 ) == 'file:// ' && substr ($ uri , -1 ) == '/ '
47
47
) {
48
- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
48
+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
49
49
}
50
50
51
51
$ this ->messageBody = $ response ;
52
- if (! empty ($ http_response_header )) {
52
+ if (!empty ($ http_response_header )) {
53
53
$ this ->fetchContentType ($ http_response_header );
54
54
} else {
55
55
// Could be a "file://" url or something else - fake up the response
56
56
$ this ->contentType = null ;
57
57
}
58
-
58
+
59
59
return $ this ->messageBody ;
60
60
}
61
-
61
+
62
62
/**
63
63
* @param array $headers HTTP Response Headers
64
64
* @return boolean Whether the Content-Type header was found or not
@@ -70,10 +70,10 @@ private function fetchContentType(array $headers)
70
70
return true ;
71
71
}
72
72
}
73
-
73
+
74
74
return false ;
75
75
}
76
-
76
+
77
77
/**
78
78
* @param string $header
79
79
* @return string|null
0 commit comments