1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* REST Middleware |
5
|
|
|
* |
6
|
|
|
* @package Force |
7
|
|
|
* @author Flipbox Factory <[email protected]> |
8
|
|
|
* @copyright 2010-2016 Flipbox Digital Limited |
9
|
|
|
* @license https://flipboxfactory.com/software/craft/force/license |
10
|
|
|
* @version Release: 1.3.0 |
11
|
|
|
* @link https://github.com/FlipboxFactory/Force |
12
|
|
|
* @since Class available since Release 1.0.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Flipbox\Relay\HubSpot\Middleware; |
16
|
|
|
|
17
|
|
|
use Flipbox\Relay\Middleware\AbstractMiddleware; |
|
|
|
|
18
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
19
|
|
|
use GuzzleHttp\Exception\ClientException; |
20
|
|
|
use GuzzleHttp\Psr7\Response as HttpResponse; |
21
|
|
|
use Psr\Http\Message\RequestInterface; |
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
23
|
|
|
|
24
|
|
|
class Client extends AbstractMiddleware |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null) |
31
|
|
|
{ |
32
|
|
|
// Prepare request |
33
|
|
|
$request = $this->prepRequest($request); |
34
|
|
|
|
35
|
|
|
// Call API |
36
|
|
|
$response = $this->call($request, $response); |
37
|
|
|
|
38
|
|
|
// Onward |
39
|
|
|
$response = $next($request, $response); |
40
|
|
|
|
41
|
|
|
return $response; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Call the API |
46
|
|
|
* |
47
|
|
|
* @param RequestInterface $request |
48
|
|
|
* @param ResponseInterface $response |
49
|
|
|
* @return ResponseInterface |
50
|
|
|
*/ |
51
|
|
|
private function call(RequestInterface $request, ResponseInterface $response) |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
// todo is there a way to append to an existing guzzle response? |
56
|
|
|
$client = new GuzzleHttpClient(); |
57
|
|
|
$httpResponse = $client->send($request); |
58
|
|
|
} catch (ClientException $e) { |
59
|
|
|
$this->error( |
60
|
|
|
"API Exception", |
61
|
|
|
[ |
62
|
|
|
'exception' => $e |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
$httpResponse = $e->getResponse(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Sync responses |
69
|
|
|
return $this->syncResponse($httpResponse, $response); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param RequestInterface $request |
74
|
|
|
* @return RequestInterface |
75
|
|
|
*/ |
76
|
|
|
private function prepRequest(RequestInterface $request) |
77
|
|
|
{ |
78
|
|
|
return $request->withHeader('Content-Type', 'application/json') |
79
|
|
|
->withHeader('Accept', 'application/json'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param HttpResponse $httpResponse |
84
|
|
|
* @param ResponseInterface $response |
85
|
|
|
* @return ResponseInterface |
86
|
|
|
*/ |
87
|
|
|
private function syncResponse(HttpResponse $httpResponse, ResponseInterface $response) |
88
|
|
|
{ |
89
|
|
|
// Add headers |
90
|
|
|
foreach ($httpResponse->getHeaders() as $name => $value) { |
91
|
|
|
$response = $response->withHeader($name, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $response->withStatus($httpResponse->getStatusCode(), $httpResponse->getReasonPhrase()) |
95
|
|
|
->withBody($httpResponse->getBody()) |
96
|
|
|
->withProtocolVersion($httpResponse->getProtocolVersion()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: