|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\JsonReference; |
|
4
|
|
|
|
|
5
|
|
|
use League\JsonReference\DecoderManager; |
|
6
|
|
|
use League\JsonReference\Loader\CurlWebLoader; |
|
7
|
|
|
use League\JsonReference\Loader\FileGetContentsWebLoader; |
|
8
|
|
|
use League\JsonReference\Loader\FileLoader; |
|
9
|
|
|
|
|
10
|
|
|
final class LoaderManager |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var LoaderInterface[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private $loaders = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var DecoderManager |
|
19
|
|
|
*/ |
|
20
|
|
|
private $decoderManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param LoaderInterface[] $loaders |
|
24
|
|
|
* @param DecoderManager $decoderManager |
|
25
|
|
|
*/ |
|
26
|
74 |
|
public function __construct(array $loaders = [], DecoderManager $decoderManager = null) |
|
27
|
|
|
{ |
|
28
|
74 |
|
$this->decoderManager = $decoderManager ?: new DecoderManager([], 'json'); |
|
29
|
|
|
|
|
30
|
74 |
|
if (empty($loaders)) { |
|
31
|
72 |
|
$this->registerDefaultFileLoader(); |
|
32
|
72 |
|
$this->registerDefaultWebLoaders(); |
|
33
|
36 |
|
} |
|
34
|
|
|
|
|
35
|
74 |
|
foreach ($loaders as $prefix => $loader) { |
|
36
|
2 |
|
$this->registerLoader($prefix, $loader); |
|
37
|
37 |
|
} |
|
38
|
74 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Register a LoaderInterface for the given prefix. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $prefix |
|
44
|
|
|
* @param LoaderInterface $loader |
|
45
|
|
|
*/ |
|
46
|
14 |
|
public function registerLoader($prefix, LoaderInterface $loader) |
|
47
|
|
|
{ |
|
48
|
14 |
|
$this->loaders[$prefix] = $loader; |
|
49
|
14 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get all registered loaders, keyed by the prefix they are registered to load schemas for. |
|
53
|
|
|
* |
|
54
|
|
|
* @return LoaderInterface[] |
|
55
|
|
|
*/ |
|
56
|
10 |
|
public function getLoaders() |
|
57
|
|
|
{ |
|
58
|
10 |
|
return $this->loaders; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the loader for the given prefix. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $prefix |
|
65
|
|
|
* |
|
66
|
|
|
* @return LoaderInterface |
|
67
|
|
|
* @throws \InvalidArgumentException |
|
68
|
|
|
*/ |
|
69
|
60 |
|
public function getLoader($prefix) |
|
70
|
|
|
{ |
|
71
|
60 |
|
if (!$this->hasLoader($prefix)) { |
|
72
|
2 |
|
throw new \InvalidArgumentException(sprintf('A loader is not registered for the prefix "%s"', $prefix)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
58 |
|
return $this->loaders[$prefix]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $prefix |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
62 |
|
public function hasLoader($prefix) |
|
84
|
|
|
{ |
|
85
|
62 |
|
return isset($this->loaders[$prefix]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Register the default file loader. |
|
90
|
|
|
*/ |
|
91
|
72 |
|
private function registerDefaultFileLoader() |
|
92
|
|
|
{ |
|
93
|
72 |
|
$this->loaders['file'] = new FileLoader($this->decoderManager); |
|
94
|
72 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Register the default web loaders. If the curl extension is loaded, |
|
98
|
|
|
* the CurlWebLoader will be used. Otherwise the FileGetContentsWebLoader |
|
99
|
|
|
* will be used. You can override this by registering your own loader |
|
100
|
|
|
* for the 'http' and 'https' protocols. |
|
101
|
|
|
*/ |
|
102
|
72 |
|
private function registerDefaultWebLoaders() |
|
103
|
|
|
{ |
|
104
|
72 |
|
if (function_exists('curl_init')) { |
|
105
|
72 |
|
$this->loaders['https'] = new CurlWebLoader('https://', null, $this->decoderManager); |
|
106
|
72 |
|
$this->loaders['http'] = new CurlWebLoader('http://', null, $this->decoderManager); |
|
107
|
36 |
|
} else { |
|
108
|
|
|
$this->loaders['https'] = new FileGetContentsWebLoader('https://', $this->decoderManager); |
|
109
|
|
|
$this->loaders['http'] = new FileGetContentsWebLoader('http://', $this->decoderManager); |
|
110
|
|
|
} |
|
111
|
72 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return DecoderManager |
|
115
|
|
|
*/ |
|
116
|
8 |
|
public function getDecoderManager() |
|
117
|
|
|
{ |
|
118
|
8 |
|
return $this->decoderManager; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|