|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\ApiDescriptions 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
|
|
|
namespace KleijnWeb\ApiDescriptions\Description; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\Common\Cache\Cache; |
|
11
|
|
|
use KleijnWeb\ApiDescriptions\Description\Description; |
|
12
|
|
|
use KleijnWeb\ApiDescriptions\Description\Factory\Factory; |
|
13
|
|
|
use KleijnWeb\ApiDescriptions\Document\Definition\Loader\DefinitionLoader; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author John Kleijn <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Repository |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $basePath; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $specifications = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Cache |
|
32
|
|
|
*/ |
|
33
|
|
|
private $cache; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var DefinitionLoader |
|
37
|
|
|
*/ |
|
38
|
|
|
private $loader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Factory |
|
42
|
|
|
*/ |
|
43
|
|
|
private $factory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Repository constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string|null $basePath |
|
49
|
|
|
* @param Cache|null $cache |
|
50
|
|
|
* @param DefinitionLoader|null $loader |
|
51
|
|
|
* @param Factory|null $factory |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
string $basePath = null, |
|
55
|
|
|
Cache $cache = null, |
|
56
|
|
|
DefinitionLoader $loader = null, |
|
57
|
|
|
Factory $factory = null |
|
58
|
|
|
) { |
|
59
|
|
|
$this->basePath = $basePath; |
|
60
|
|
|
$this->cache = $cache; |
|
61
|
|
|
$this->loader = $loader ?: new DefinitionLoader(); |
|
62
|
|
|
$this->factory = $factory ?: new Factory(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $documentPath |
|
67
|
|
|
* |
|
68
|
|
|
* @return Description |
|
69
|
|
|
*/ |
|
70
|
|
|
public function get(string $documentPath): Description |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$documentPath) { |
|
73
|
|
|
throw new \InvalidArgumentException("No document path provided"); |
|
74
|
|
|
} |
|
75
|
|
|
if ($this->basePath) { |
|
76
|
|
|
$documentPath = "$this->basePath/$documentPath"; |
|
77
|
|
|
} |
|
78
|
|
|
if (!isset($this->specifications[$documentPath])) { |
|
79
|
|
|
$this->specifications[$documentPath] = $this->load($documentPath); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->specifications[$documentPath]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $uri |
|
87
|
|
|
* |
|
88
|
|
|
* @return Description |
|
89
|
|
|
*/ |
|
90
|
|
|
private function load(string $uri): Description |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->cache && $specification = $this->cache->fetch($uri)) { |
|
93
|
|
|
return $specification; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$specification = $this->factory->create($uri, $this->loader->load($uri)); |
|
97
|
|
|
|
|
98
|
|
|
if ($this->cache) { |
|
99
|
|
|
$this->cache->save($uri, $specification); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $specification; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|