1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Document; |
10
|
|
|
|
11
|
|
|
use JsonSchema\Exception\ResourceNotFoundException; |
12
|
|
|
use JsonSchema\Uri\Retrievers\AbstractRetriever; |
13
|
|
|
use Symfony\Component\Yaml\Yaml; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author John Kleijn <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class YamlCapableUriRetriever extends AbstractRetriever |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* TODO This is of course terribly inefficient |
22
|
|
|
* |
23
|
|
|
* @see \JsonSchema\Uri\Retrievers\UriRetrieverInterface::retrieve() |
24
|
|
|
* |
25
|
|
|
* @param string $uri |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function retrieve($uri) |
30
|
|
|
{ |
31
|
|
|
set_error_handler(function () use ($uri) { |
32
|
|
|
throw new ResourceNotFoundException('Schema not found at ' . $uri); |
33
|
|
|
}); |
34
|
|
|
$response = file_get_contents($uri); |
35
|
|
|
restore_error_handler(); |
36
|
|
|
|
37
|
|
|
if (false === $response) { |
38
|
|
|
throw new ResourceNotFoundException('Schema not found at ' . $uri); |
39
|
|
|
} |
40
|
|
|
if ($response == '' |
41
|
|
|
&& substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/' |
42
|
|
|
) { |
43
|
|
|
throw new ResourceNotFoundException('Schema not found at ' . $uri); |
44
|
|
|
} |
45
|
|
|
$this->contentType = null; |
46
|
|
|
if (preg_match('/\b(yml|yaml)\b/', $uri)) { |
47
|
|
|
$data = Yaml::parse($response); |
48
|
|
|
|
49
|
|
|
return json_encode($data); |
50
|
|
|
} |
51
|
|
|
if (!empty($http_response_header)) { |
52
|
|
|
foreach ($http_response_header as $header) { |
53
|
|
|
if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) { |
54
|
|
|
$actualContentType = trim($match[1]); |
55
|
|
|
if (strpos($actualContentType, 'yaml')) { |
56
|
|
|
return json_encode(Yaml::parse($response)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $response; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|