|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCache package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\SymfonyCache; |
|
13
|
|
|
|
|
14
|
|
|
use Http\Client\HttpAsyncClient; |
|
15
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
16
|
|
|
use Http\Message\ResponseFactory; |
|
17
|
|
|
use Http\Promise\FulfilledPromise; |
|
18
|
|
|
use Psr\Http\Message\RequestInterface; |
|
19
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
22
|
|
|
use Zend\Diactoros\ServerRequest; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* An implementation of HttpAsyncClient that allows direct calling of the |
|
26
|
|
|
* Symfony HttpCache kernel without executing a real HTTP request. |
|
27
|
|
|
* It can only be used if you have a single node setup of Symfony and serves |
|
28
|
|
|
* as kind of a shortcut for easier configuration. |
|
29
|
|
|
* If you use Varnish or have a multiple node Symfony setup, this client is entirely |
|
30
|
|
|
* useless and cannot be used. |
|
31
|
|
|
* It HttPlug libraries to convert between PSR-7 requests and responses. Both are |
|
32
|
|
|
* optional dependencies of this package thus existence of the respective classes |
|
33
|
|
|
* is checked for in the constructor of this client. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Yanick Witschi <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class KernelClient implements HttpAsyncClient |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var HttpCacheAwareKernelInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $kernel; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var HttpFoundationFactory |
|
46
|
|
|
*/ |
|
47
|
|
|
private $httpFoundationFactory; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var ResponseFactory |
|
51
|
|
|
*/ |
|
52
|
|
|
private $psr7Factory; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* KernelClient constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param HttpCacheAwareKernelInterface $kernel |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(HttpCacheAwareKernelInterface $kernel, ResponseFactory $responseFactory = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->kernel = $kernel; |
|
62
|
|
|
|
|
63
|
|
|
if (!class_exists(HttpFoundationFactory::class)) { |
|
64
|
|
|
throw new \RuntimeException('Install symfony/psr-http-message-bridge to use this client.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!class_exists(ResponseFactory::class)) { |
|
68
|
|
|
throw new \RuntimeException('Install a php-http/message-factory package to use this client.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->httpFoundationFactory = new HttpFoundationFactory(); |
|
72
|
|
|
$this->psr7Factory = $responseFactory ?: MessageFactoryDiscovery::find(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Converts a PSR-7 request to a Symfony HttpFoundation request, runs |
|
77
|
|
|
* it through the HttpCache kernel and converts the response back to a PSR-7 |
|
78
|
|
|
* response. |
|
79
|
|
|
* |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function sendAsyncRequest(RequestInterface $request) |
|
83
|
|
|
{ |
|
84
|
|
|
$symfonyRequest = Request::createFromGlobals(); |
|
85
|
|
|
$symfonyRequest->server->set('REMOTE_ADDR', '127.0.0.1'); |
|
86
|
|
|
|
|
87
|
|
|
$query = []; |
|
88
|
|
|
$parts = explode('&', $request->getUri()->getQuery()); |
|
89
|
|
|
foreach ($parts as $part) { |
|
90
|
|
|
$chunks = explode('=', $part, 2); |
|
91
|
|
|
$query[$chunks[0]] = $chunks[1]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$serverRequest = new ServerRequest( |
|
95
|
|
|
$symfonyRequest->server->all(), |
|
96
|
|
|
[], |
|
97
|
|
|
$request->getUri(), |
|
98
|
|
|
$request->getMethod(), |
|
99
|
|
|
$request->getBody(), |
|
100
|
|
|
$request->getHeaders(), |
|
101
|
|
|
$symfonyRequest->cookies->all(), |
|
102
|
|
|
$query |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
$symfonyRequest = $this->httpFoundationFactory->createRequest($serverRequest); |
|
106
|
|
|
$symfonyResponse = $this->kernel->getHttpCache()->handle($symfonyRequest, HttpKernelInterface::MASTER_REQUEST, false); |
|
107
|
|
|
$psrResponse = $this->psr7Factory->createResponse($symfonyResponse); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
return new FulfilledPromise($psrResponse); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: