1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OpenapiBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Niels Nijens <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Nijens\OpenapiBundle\Json; |
13
|
|
|
|
14
|
|
|
use League\JsonReference\DereferencerInterface; |
15
|
|
|
use stdClass; |
16
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
17
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
18
|
|
|
use Symfony\Component\Config\Resource\ResourceInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Loads a dereferenced JSON schema. |
22
|
|
|
* |
23
|
|
|
* @author Niels Nijens <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SchemaLoader implements SchemaLoaderInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var FileLocatorInterface |
29
|
|
|
*/ |
30
|
|
|
private $fileLocator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var DereferencerInterface |
34
|
|
|
*/ |
35
|
|
|
private $dereferencer; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The loaded JSON schemas. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $schemas = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructs a new SchemaLoader instance. |
46
|
|
|
* |
47
|
|
|
* @param FileLocatorInterface $fileLocator |
48
|
|
|
* @param DereferencerInterface $dereferencer |
49
|
|
|
*/ |
50
|
|
|
public function __construct(FileLocatorInterface $fileLocator, DereferencerInterface $dereferencer) |
51
|
|
|
{ |
52
|
|
|
$this->fileLocator = $fileLocator; |
53
|
|
|
$this->dereferencer = $dereferencer; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function load(string $file): stdClass |
60
|
|
|
{ |
61
|
|
|
$locatedFile = $this->fileLocator->locate($file); |
62
|
|
|
|
63
|
|
|
if (isset($this->schemas[$locatedFile]) === false) { |
64
|
|
|
$schema = $this->dereferencer->dereference('file://'.$locatedFile); |
|
|
|
|
65
|
|
|
$dereferencedSchema = json_decode(json_encode($schema)); |
66
|
|
|
|
67
|
|
|
$this->schemas[$locatedFile] = $dereferencedSchema; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->schemas[$locatedFile]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getFileResource(string $file): ?ResourceInterface |
77
|
|
|
{ |
78
|
|
|
$locatedFile = $this->fileLocator->locate($file); |
79
|
|
|
if (isset($this->schemas[$locatedFile])) { |
80
|
|
|
return new FileResource($locatedFile); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|