1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TransformationHandler |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ProxyBundle\Service; |
7
|
|
|
|
8
|
|
|
use Graviton\ProxyBundle\Transformation\RequestTransformationInterface; |
9
|
|
|
use Graviton\ProxyBundle\Transformation\ResponseTransformationInterface; |
10
|
|
|
use Graviton\ProxyBundle\Transformation\SchemaTransformationInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class handles all transformations for all proxied API's and their endpoints. |
16
|
|
|
* |
17
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
18
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
19
|
|
|
* @link http://swisscom.ch |
20
|
|
|
*/ |
21
|
|
|
class TransformationHandler |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $requestTransformations = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $responseTransformations = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $schemaTransformations = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Applies all transformations defined for the given API and endpoint on a request. |
40
|
|
|
* |
41
|
|
|
* @param string $api The API name |
42
|
|
|
* @param string $endpoint The endpoint |
43
|
|
|
* @param Request $requestIn The original request object. |
44
|
|
|
* @param Request $requestOut The request object to use for transformations |
45
|
|
|
* @return Request The transformed request |
46
|
|
|
*/ |
47
|
|
|
public function transformRequest($api, $endpoint, Request $requestIn, Request $requestOut) |
48
|
|
|
{ |
49
|
|
|
$transformations = $this->getRequestTransformations($api, $endpoint); |
50
|
|
|
foreach ($transformations as $transformation) { |
51
|
|
|
$transformedRequest = $transformation->transformRequest($requestIn, $requestOut); |
52
|
|
|
$requestOut = $transformedRequest instanceof Request ? $transformedRequest : $requestOut; |
53
|
|
|
} |
54
|
|
|
return $requestOut; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Applies all transformations defined for the given API and endpoint on a response. |
59
|
|
|
* |
60
|
|
|
* @param string $api The API name |
61
|
|
|
* @param string $endpoint The endpoint |
62
|
|
|
* @param Response $responseIn The original response object |
63
|
|
|
* @param Response $responseOut The response object to use for transformations |
64
|
|
|
* @return Response The transformed response |
65
|
|
|
*/ |
66
|
|
|
public function transformResponse($api, $endpoint, Response $responseIn, Response $responseOut) |
67
|
|
|
{ |
68
|
|
|
$transformations = $this->getResponseTransformations($api, $endpoint); |
69
|
|
|
foreach ($transformations as $transformation) { |
70
|
|
|
$transformedRequest = $transformation->transformResponse($responseIn, $responseOut); |
71
|
|
|
$responseOut = $transformedRequest instanceof Response ? $transformedRequest : $responseOut; |
72
|
|
|
} |
73
|
|
|
return $responseOut; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Applies all transformations defined for the given API and endpoint on a schema. |
78
|
|
|
* |
79
|
|
|
* @param string $api The API name |
80
|
|
|
* @param string $endpoint The endpoint |
81
|
|
|
* @param \stdClass $schemaIn The original schema object |
82
|
|
|
* @param \stdClass $schemaOut The schema object to use for transformations |
83
|
|
|
* @return \stdClass The transformed schema |
84
|
|
|
*/ |
85
|
|
|
public function transformSchema($api, $endpoint, \stdClass $schemaIn, \stdClass $schemaOut) |
86
|
|
|
{ |
87
|
|
|
$transformations = $this->getSchemaTransformations($api, $endpoint); |
88
|
|
|
foreach ($transformations as $transformation) { |
89
|
|
|
$transformedSchema = $transformation->transformSchema($schemaIn, $schemaOut); |
90
|
|
|
$schemaOut = $transformedSchema instanceof \stdClass ? $transformedSchema : $schemaOut; |
91
|
|
|
} |
92
|
|
|
return $schemaOut; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the request transformations registered for a given API and endpoint. |
97
|
|
|
* |
98
|
|
|
* @param string $api The API name |
99
|
|
|
* @param string $endpoint The endpoint |
100
|
|
|
* @return array The transformations |
101
|
|
|
*/ |
102
|
|
|
public function getRequestTransformations($api, $endpoint) |
103
|
|
|
{ |
104
|
|
|
return isset($this->requestTransformations[$api][$endpoint]) ? |
105
|
|
|
$this->requestTransformations[$api][$endpoint] : []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the transformations registered for a given API and endpoint. |
110
|
|
|
* |
111
|
|
|
* @param string $api The API name |
112
|
|
|
* @param string $endpoint The endpoint |
113
|
|
|
* @return array The transformations |
114
|
|
|
*/ |
115
|
|
|
public function getResponseTransformations($api, $endpoint) |
116
|
|
|
{ |
117
|
|
|
return isset($this->responseTransformations[$api][$endpoint]) ? |
118
|
|
|
$this->responseTransformations[$api][$endpoint] : []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns the transformations registered for a given API and endpoint. |
123
|
|
|
* |
124
|
|
|
* @param string $api The API name |
125
|
|
|
* @param string $endpoint The endpoint |
126
|
|
|
* @return array The transformations |
127
|
|
|
*/ |
128
|
|
|
public function getSchemaTransformations($api, $endpoint) |
129
|
|
|
{ |
130
|
|
|
return isset($this->schemaTransformations[$api][$endpoint]) ? |
131
|
|
|
$this->schemaTransformations[$api][$endpoint] : []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Adds a transformation to a given API and endpoint. |
136
|
|
|
* |
137
|
|
|
* @param string $api The API name |
138
|
|
|
* @param string $endpoint The API endpoint |
139
|
|
|
* @param RequestTransformationInterface $transformation The transformation |
140
|
|
|
* @return int The position of the added transformation within the transformation array. |
141
|
|
|
*/ |
142
|
|
|
public function addRequestTransformation($api, $endpoint, RequestTransformationInterface $transformation) |
143
|
|
|
{ |
144
|
|
|
$this->requestTransformations[$api][$endpoint][] = $transformation; |
145
|
|
|
return count($this->requestTransformations[$api][$endpoint]) - 1; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Adds a response transformation to a given API and endpoint. |
150
|
|
|
* |
151
|
|
|
* @param string $api The API name |
152
|
|
|
* @param string $endpoint The API endpoint |
153
|
|
|
* @param ResponseTransformationInterface $transformation The transformation |
154
|
|
|
* @return int The position of the added transformation within the transformation array. |
155
|
|
|
*/ |
156
|
|
|
public function addResponseTransformation($api, $endpoint, ResponseTransformationInterface $transformation) |
157
|
|
|
{ |
158
|
|
|
$this->responseTransformations[$api][$endpoint][] = $transformation; |
159
|
|
|
return count($this->responseTransformations[$api][$endpoint]) - 1; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Adds a schema transformation to a given API and endpoint. |
164
|
|
|
* |
165
|
|
|
* @param string $api The API name |
166
|
|
|
* @param string $endpoint The API endpoint |
167
|
|
|
* @param SchemaTransformationInterface $transformation The transformation |
168
|
|
|
* @return int The position of the added transformation within the transformation array. |
169
|
|
|
*/ |
170
|
|
|
public function addSchemaTransformation($api, $endpoint, SchemaTransformationInterface $transformation) |
171
|
|
|
{ |
172
|
|
|
$this->schemaTransformations[$api][$endpoint][] = $transformation; |
173
|
|
|
return count($this->schemaTransformations[$api][$endpoint]) - 1; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|