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
- 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
+ )));
33
35
36
+ set_error_handler (function () use ($ uri ) {
37
+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
38
+ });
34
39
$ response = file_get_contents ($ uri );
40
+ restore_error_handler ();
35
41
36
42
if (false === $ response ) {
37
- throw new ResourceNotFoundException ('JSON schema was not retrieved at ' . $ uri );
43
+ throw new ResourceNotFoundException ('JSON schema not found at ' . $ uri );
38
44
}
39
-
40
45
if ($ response == ''
41
46
&& substr ($ uri , 0 , 7 ) == 'file:// ' && substr ($ uri , -1 ) == '/ '
42
47
) {
@@ -50,10 +55,10 @@ public function retrieve($uri)
50
55
// Could be a "file://" url or something else - fake up the response
51
56
$ this ->contentType = null ;
52
57
}
53
-
58
+
54
59
return $ this ->messageBody ;
55
60
}
56
-
61
+
57
62
/**
58
63
* @param array $headers HTTP Response Headers
59
64
* @return boolean Whether the Content-Type header was found or not
@@ -65,10 +70,10 @@ private function fetchContentType(array $headers)
65
70
return true ;
66
71
}
67
72
}
68
-
73
+
69
74
return false ;
70
75
}
71
-
76
+
72
77
/**
73
78
* @param string $header
74
79
* @return string|null
0 commit comments