1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\RemoteRef; |
4
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Context; |
6
|
|
|
use Swaggest\JsonSchema\RefResolver; |
7
|
|
|
use Swaggest\JsonSchema\RemoteRefProvider; |
8
|
|
|
use Swaggest\JsonSchema\Schema; |
9
|
|
|
|
10
|
|
|
class Preloaded implements RemoteRefProvider |
11
|
|
|
{ |
12
|
|
|
private $storage; |
13
|
|
|
|
14
|
|
|
private $schemaFiles; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
$this->schemaFiles = array( |
19
|
|
|
'http://json-schema.org/draft-04/schema' => __DIR__ . '/../../spec/json-schema.json', |
20
|
|
|
'http://json-schema.org/draft-06/schema' => __DIR__ . '/../../spec/json-schema-draft6.json', |
21
|
|
|
'http://json-schema.org/draft-07/schema' => __DIR__ . '/../../spec/json-schema-draft7.json', |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getSchemaData($url) |
26
|
|
|
{ |
27
|
|
|
if (isset($this->storage[$url])) { |
28
|
|
|
return $this->storage[$url]; |
29
|
|
|
} elseif (isset($this->schemaFiles[$url])) { |
30
|
|
|
$this->storage[$url] = json_decode(file_get_contents($this->schemaFiles[$url])); |
31
|
|
|
return $this->storage[$url]; |
32
|
|
|
} |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param RefResolver $refResolver |
38
|
|
|
* @param Context|null $options |
39
|
|
|
* @throws \Swaggest\JsonSchema\Exception |
40
|
|
|
*/ |
41
|
|
|
public function populateSchemas(RefResolver $refResolver, Context $options = null) |
42
|
|
|
{ |
43
|
|
|
if ($options === null) { |
44
|
|
|
$options = new Context(); |
45
|
|
|
$options->refResolver = $refResolver; |
46
|
|
|
$options->version = Schema::VERSION_AUTO; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$prev = $refResolver->getResolutionScope(); |
50
|
|
|
foreach ($this->storage as $url => $schemaData) { |
51
|
|
|
$refResolver->setupResolutionScope($url, $schemaData); |
52
|
|
|
$refResolver->setResolutionScope($url); |
53
|
|
|
$refResolver->preProcessReferences($schemaData, $options); |
54
|
|
|
} |
55
|
|
|
$refResolver->setResolutionScope($prev); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setSchemaData($url, $schemaData) |
59
|
|
|
{ |
60
|
|
|
$this->storage[$url] = $schemaData; |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function setSchemaFile($url, $path) |
65
|
|
|
{ |
66
|
|
|
$this->schemaFiles[$url] = $path; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
} |