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 Doctrine\Common\Cache\Cache; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\Exception\ResourceNotReadableException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class DocumentRepository |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $basePath; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $documents = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Cache |
31
|
|
|
*/ |
32
|
|
|
private $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Loader |
36
|
|
|
*/ |
37
|
|
|
private $loader; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Initializes a new Repository. |
41
|
|
|
* |
42
|
|
|
* @param string $basePath |
43
|
|
|
* @param Cache $cache |
44
|
|
|
* @param Loader $loader |
45
|
|
|
*/ |
46
|
|
|
public function __construct($basePath = null, Cache $cache = null, Loader $loader = null) |
47
|
|
|
{ |
48
|
|
|
$this->basePath = $basePath; |
49
|
|
|
$this->cache = $cache; |
50
|
|
|
$this->loader = $loader ?: new Loader(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $documentPath |
55
|
|
|
* |
56
|
|
|
* @return SwaggerDocument |
57
|
|
|
*/ |
58
|
|
|
public function get($documentPath) |
59
|
|
|
{ |
60
|
|
|
if ($this->basePath) { |
61
|
|
|
$documentPath = "$this->basePath/$documentPath"; |
62
|
|
|
} |
63
|
|
|
if (!$documentPath) { |
64
|
|
|
throw new \InvalidArgumentException("No document path provided"); |
65
|
|
|
} |
66
|
|
|
if (!isset($this->documents[$documentPath])) { |
67
|
|
|
$this->documents[$documentPath] = $this->load($documentPath); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->documents[$documentPath]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $uri |
75
|
|
|
* |
76
|
|
|
* @return SwaggerDocument |
77
|
|
|
* @throws ResourceNotReadableException |
78
|
|
|
*/ |
79
|
|
|
private function load($uri) |
80
|
|
|
{ |
81
|
|
|
if ($this->cache && $document = $this->cache->fetch($uri)) { |
82
|
|
|
return $document; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$resolver = new RefResolver($this->loader->load($uri), $uri); |
86
|
|
|
$document = new SwaggerDocument($uri, $resolver->resolve()); |
87
|
|
|
|
88
|
|
|
if ($this->cache) { |
89
|
|
|
$this->cache->save($uri, $document); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $document; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|