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
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Loads a dereferenced JSON schema. |
20
|
|
|
* |
21
|
|
|
* @author Niels Nijens <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class SchemaLoader implements SchemaLoaderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FileLocatorInterface |
27
|
|
|
*/ |
28
|
|
|
private $fileLocator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DereferencerInterface |
32
|
|
|
*/ |
33
|
|
|
private $dereferencer; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The loaded JSON schemas. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $schemas = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructs a new SchemaLoader instance. |
44
|
|
|
* |
45
|
|
|
* @param FileLocatorInterface $fileLocator |
46
|
|
|
* @param DereferencerInterface $dereferencer |
47
|
|
|
*/ |
48
|
|
|
public function __construct(FileLocatorInterface $fileLocator, DereferencerInterface $dereferencer) |
49
|
|
|
{ |
50
|
|
|
$this->fileLocator = $fileLocator; |
51
|
|
|
$this->dereferencer = $dereferencer; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $file |
56
|
|
|
* |
57
|
|
|
* @return stdClass |
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
|
|
|
|