1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ApiDefinitionLoader |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ProxyBundle\Service; |
7
|
|
|
|
8
|
|
|
use Graviton\ProxyBundle\Definition\ApiDefinition; |
9
|
|
|
use Graviton\ProxyBundle\Definition\Loader\LoaderInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* load API definition from a external source |
13
|
|
|
* |
14
|
|
|
* @package Graviton\ProxyBundle\Service |
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
17
|
|
|
* @link http://swisscom.ch |
18
|
|
|
*/ |
19
|
|
|
class ApiDefinitionLoader |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const PROXY_ROUTE = "3rdparty"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var LoaderInterface |
28
|
|
|
*/ |
29
|
|
|
private $definitionLoader; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $options; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ApiDefinition |
38
|
|
|
*/ |
39
|
|
|
private $definition; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* set loader |
43
|
|
|
* |
44
|
|
|
* @param LoaderInterface $loader loader |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function setDefinitionLoader($loader) |
49
|
|
|
{ |
50
|
|
|
$this->definitionLoader = $loader; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* set options for the loader |
55
|
|
|
* |
56
|
|
|
* @param array $options options [uri, prefix] |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function setOption(array $options) |
61
|
|
|
{ |
62
|
|
|
$this->options = $options; |
63
|
|
|
if (!empty($this->definitionLoader)) { |
64
|
|
|
$this->definitionLoader->setOptions($options); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* get the origin service definition |
70
|
|
|
* |
71
|
|
|
* @param bool $forceReload Switch to force a new api definition object will be provided. |
72
|
|
|
* |
73
|
|
|
* @return mixed the origin service definition (type depends on dispersal strategy) |
74
|
|
|
*/ |
75
|
|
|
public function getOriginDefinition($forceReload = false) |
76
|
|
|
{ |
77
|
|
|
$this->loadApiDefinition($forceReload); |
78
|
|
|
|
79
|
|
|
return $this->definition->getOrigin(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* get a schema for one endpoint |
84
|
|
|
* |
85
|
|
|
* @param string $endpoint endpoint |
86
|
|
|
* @param bool $forceReload Switch to force a new api definition object will be provided. |
87
|
|
|
* |
88
|
|
|
* @return \stdClass |
89
|
|
|
*/ |
90
|
|
|
public function getEndpointSchema($endpoint, $forceReload = false) |
91
|
|
|
{ |
92
|
|
|
$this->loadApiDefinition($forceReload); |
93
|
|
|
|
94
|
|
|
return $this->definition->getSchema($endpoint); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* get an endpoint |
99
|
|
|
* |
100
|
|
|
* @param string $endpoint endpoint |
101
|
|
|
* @param boolean $withHost attach host name to the url |
102
|
|
|
* @param bool $forceReload Switch to force a new api definition object will be provided. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getEndpoint($endpoint, $withHost = false, $forceReload = false) |
107
|
|
|
{ |
108
|
|
|
$this->loadApiDefinition($forceReload); |
109
|
|
|
$url = ""; |
110
|
|
|
if ($withHost) { |
111
|
|
|
$url = $this->definition->getHost(); |
112
|
|
|
} |
113
|
|
|
//$this->definition->hasEndpoint($endpoint); |
114
|
|
|
|
115
|
|
|
/*$endpoints = $this->definition->getEndpoints(false); |
116
|
|
|
|
117
|
|
|
//has url a id |
118
|
|
|
$searchString = $endpoint; |
119
|
|
|
if (preg_match("@\/[0-9]{1,}$@", $endpoint)) { |
120
|
|
|
$searchString = substr($endpoint, 0, strrpos($endpoint, '/')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (in_array($searchString, $endpoints)) {*/ |
124
|
|
|
$url .= $endpoint; |
125
|
|
|
//} |
126
|
|
|
|
127
|
|
|
return $url; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* get all endpoints for an API |
132
|
|
|
* |
133
|
|
|
* @param boolean $withHost attach host name to the url |
134
|
|
|
* @param bool $forceReload Switch to force a new api definition object will be provided. |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getAllEndpoints($withHost = false, $forceReload = false) |
139
|
|
|
{ |
140
|
|
|
$this->loadApiDefinition($forceReload); |
141
|
|
|
|
142
|
|
|
$prefix = self::PROXY_ROUTE; |
143
|
|
|
if (isset($this->options['prefix'])) { |
144
|
|
|
$prefix .= "/".$this->options['prefix']; |
145
|
|
|
} |
146
|
|
|
$retVal = array(); |
147
|
|
|
if (is_object($this->definition)) { |
148
|
|
|
$retVal = $this->definition->getEndpoints($withHost, $prefix); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $retVal; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* internal load method |
156
|
|
|
* |
157
|
|
|
* @param bool $forceReload Switch to force a new api definition object will be provided. |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
private function loadApiDefinition($forceReload = false) |
162
|
|
|
{ |
163
|
|
|
$supported = $this->definitionLoader->supports($this->options['uri']); |
164
|
|
|
|
165
|
|
|
if ($forceReload || ($this->definition == null && $supported)) { |
166
|
|
|
$this->definition = $this->definitionLoader->load($this->options['uri']); |
167
|
|
|
} elseif (!$supported) { |
168
|
|
|
throw new \RuntimeException("This resource (".$this->options['uri'].") is not supported."); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|