1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\JsonReference\Loader; |
4
|
|
|
|
5
|
|
|
use League\JsonReference\DecoderManager; |
6
|
|
|
use League\JsonReference\DecoderInterface; |
7
|
|
|
use League\JsonReference\LoaderInterface; |
8
|
|
|
use League\JsonReference\SchemaLoadingException; |
9
|
|
|
use function League\JsonReference\determineMediaType; |
10
|
|
|
|
11
|
|
|
final class FileGetContentsWebLoader implements LoaderInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $prefix; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var DecoderManager |
20
|
|
|
*/ |
21
|
|
|
private $decoderManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $prefix |
25
|
|
|
* @param DecoderInterface|DecoderManager $decoders |
|
|
|
|
26
|
6 |
|
*/ |
27
|
|
View Code Duplication |
public function __construct($prefix, $decoderManager = null) |
|
|
|
|
28
|
6 |
|
{ |
29
|
6 |
|
$this->prefix = $prefix; |
30
|
6 |
|
|
31
|
|
|
if ($decoderManager instanceof DecoderInterface) { |
32
|
|
|
$this->decoderManager = new DecoderManager([null => $decoderManager]); |
33
|
|
|
} else { |
34
|
|
|
$this->decoderManager = $decoderManager ?: new DecoderManager(); |
35
|
6 |
|
} |
36
|
|
|
} |
37
|
6 |
|
|
38
|
6 |
|
/** |
39
|
2 |
|
* {@inheritdoc} |
40
|
6 |
|
*/ |
41
|
3 |
|
public function load($path) |
42
|
2 |
|
{ |
43
|
|
|
$uri = $this->prefix . $path; |
44
|
2 |
|
set_error_handler(function () use ($uri) { |
45
|
1 |
|
throw SchemaLoadingException::create($uri); |
46
|
|
|
}); |
47
|
|
|
$response = file_get_contents($uri); |
48
|
1 |
|
restore_error_handler(); |
49
|
|
|
|
50
|
|
|
if (!$response) { |
51
|
|
|
throw SchemaLoadingException::create($uri); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$headers = $this->parseHttpResponseHeader($http_response_header); |
55
|
|
|
$type = determineMediaType(['headers' => $headers, 'uri' => $uri]); |
56
|
|
|
return $this->decoderManager->getDecoder($type)->decode($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Parse http headers returned by $http_response_header |
61
|
|
|
* @link http://php.net/manual/en/reserved.variables.httpresponseheader.php |
62
|
|
|
* |
63
|
|
|
* @param array $headers |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public static function parseHttpResponseHeader($headers) |
68
|
|
|
{ |
69
|
|
|
$head = array(); |
70
|
|
|
foreach ($headers as $k => $v) { |
71
|
|
|
$t = explode(':', $v, 2); |
72
|
|
|
if (isset($t[1])) { |
73
|
|
|
$head[ trim($t[0]) ] = trim($t[1]); |
74
|
|
|
} else { |
75
|
|
|
$head[] = $v; |
76
|
|
|
if (preg_match("#HTTP/[0-9\.]+\s+([0-9]+)#", $v, $out)) { |
77
|
|
|
$head['response_code'] = intval($out[1]); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $head; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.