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 (
30
+ $ context = stream_context_create ([
31
+ 'http ' => [
32
32
'method ' => 'GET ' ,
33
- 'header ' => "Accept: " . Validator::SCHEMA_MEDIA_TYPE
34
- )));
33
+ 'header ' => "Accept: " .Validator::SCHEMA_MEDIA_TYPE ,
34
+ ],
35
+ ]);
35
36
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
+ ];
38
52
});
39
53
$ response = file_get_contents ($ uri );
40
54
restore_error_handler ();
41
55
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
+
42
63
if (false === $ response ) {
43
- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
64
+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
44
65
}
66
+
45
67
if ($ response == ''
46
68
&& substr ($ uri , 0 , 7 ) == 'file:// ' && substr ($ uri , -1 ) == '/ '
47
69
) {
48
- throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
70
+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
49
71
}
50
72
51
73
$ this ->messageBody = $ response ;
52
- if (! empty ($ http_response_header )) {
74
+ if (!empty ($ http_response_header )) {
53
75
$ this ->fetchContentType ($ http_response_header );
54
76
} else {
55
77
// Could be a "file://" url or something else - fake up the response
56
78
$ this ->contentType = null ;
57
79
}
58
-
80
+
59
81
return $ this ->messageBody ;
60
82
}
61
-
83
+
62
84
/**
63
85
* @param array $headers HTTP Response Headers
64
86
* @return boolean Whether the Content-Type header was found or not
@@ -70,10 +92,10 @@ private function fetchContentType(array $headers)
70
92
return true ;
71
93
}
72
94
}
73
-
95
+
74
96
return false ;
75
97
}
76
-
98
+
77
99
/**
78
100
* @param string $header
79
101
* @return string|null
0 commit comments