1 | <?php |
||
27 | class ProxyController |
||
28 | { |
||
29 | /** |
||
30 | * @var Proxy |
||
31 | */ |
||
32 | private $proxy; |
||
33 | |||
34 | /** |
||
35 | * @var EngineInterface |
||
36 | */ |
||
37 | private $templating; |
||
38 | |||
39 | /** |
||
40 | * @var DiactorosFactory |
||
41 | */ |
||
42 | private $diactorosFactory; |
||
43 | |||
44 | /** |
||
45 | * @var ApiDefinitionLoader |
||
46 | */ |
||
47 | private $apiLoader; |
||
48 | |||
49 | /** |
||
50 | * @var HttpFoundationFactory |
||
51 | */ |
||
52 | private $httpFoundationFactory; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $proxySourceConfiguration; |
||
58 | |||
59 | /** |
||
60 | * @var TransformationHandler |
||
61 | */ |
||
62 | private $transformationHandler; |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | * |
||
67 | * @param Proxy $proxy proxy |
||
68 | * @param EngineInterface $templating twig templating engine |
||
69 | * @param ApiDefinitionLoader $loader definition loader |
||
70 | * @param DiactorosFactory $diactorosFactory convert HttpFoundation objects to PSR-7 |
||
71 | * @param HttpFoundationFactory $httpFoundationFactory convert PSR-7 interfaces to HttpFoundation |
||
72 | * @param TransformationHandler $transformationHandler transformation handler |
||
73 | * @param array $proxySourceConfiguration Set of sources to be recognized by the controller. |
||
74 | */ |
||
75 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * action for routing all requests directly to the third party API |
||
95 | * |
||
96 | * @param Request $request request |
||
97 | * |
||
98 | * @return \Psr\Http\Message\ResponseInterface|Response |
||
99 | */ |
||
100 | public function proxyAction(Request $request) |
||
101 | { |
||
102 | $api = $this->decideApiAndEndpoint($request->getUri()); |
||
103 | $this->registerProxySources(); |
||
104 | |||
105 | $url = $this->apiLoader->getEndpoint($api['endpoint'], true); |
||
106 | if (parse_url($url, PHP_URL_SCHEME) === false) { |
||
107 | $scheme = $request->getScheme(); |
||
108 | $url = $scheme.'://'.$url; |
||
109 | } |
||
110 | $response = null; |
||
111 | try { |
||
112 | $newRequest = Request::create( |
||
113 | $url, |
||
114 | $request->getMethod(), |
||
115 | array (), |
||
116 | array (), |
||
117 | array (), |
||
118 | array (), |
||
119 | $request->getContent(false) |
||
|
|||
120 | ); |
||
121 | $newRequest->headers->add($request->headers->all()); |
||
122 | |||
123 | |||
124 | |||
125 | $newRequest = $this->transformationHandler->transformRequest( |
||
126 | $api['apiName'], |
||
127 | $api['endpoint'], |
||
128 | $request, |
||
129 | $newRequest |
||
130 | ); |
||
131 | $psrRequest = $this->diactorosFactory->createRequest($newRequest); |
||
132 | $psrRequest = $psrRequest->withUri($psrRequest->getUri()->withPort(parse_url($url, PHP_URL_PORT))); |
||
133 | $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url)); |
||
134 | $response = $this->httpFoundationFactory->createResponse($psrResponse); |
||
135 | $this->transformationHandler->transformResponse( |
||
136 | $api['apiName'], |
||
137 | $api['endpoint'], |
||
138 | $response, |
||
139 | clone $response |
||
140 | ); |
||
141 | } catch (ClientException $e) { |
||
142 | $response = $e->getResponse(); |
||
143 | } catch (ServerException $serverException) { |
||
144 | $response = $serverException->getResponse(); |
||
145 | } |
||
146 | |||
147 | return $response; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * get schema info |
||
152 | * |
||
153 | * @param Request $request request |
||
154 | * |
||
155 | * @return Response |
||
156 | */ |
||
157 | public function schemaAction(Request $request) |
||
177 | |||
178 | /** |
||
179 | * get API name and endpoint from the url (third party API) |
||
180 | * |
||
181 | * @param string $url the url |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function decideApiAndEndpoint($url) |
||
205 | |||
206 | /** |
||
207 | * Registers configured external services to be proxied. |
||
208 | * |
||
209 | * @return Void |
||
210 | */ |
||
211 | private function registerProxySources() |
||
219 | |||
220 | /** |
||
221 | * get host, scheme and port |
||
222 | * |
||
223 | * @param string $url the url |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | private function getHostWithScheme($url) |
||
237 | } |
||
238 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.