1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace loophp\UnalteredPsrHttpMessageBridgeBundle\Factory; |
11
|
|
|
|
12
|
|
|
use League\Uri\QueryString; |
13
|
|
|
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
|
17
|
|
|
use const PHP_QUERY_RFC1738; |
18
|
|
|
|
19
|
|
|
final class UnalteredPsrHttpFactory implements HttpMessageFactoryInterface |
20
|
|
|
{ |
21
|
6 |
|
private HttpMessageFactoryInterface $httpMessageFactory; |
22
|
|
|
|
23
|
6 |
|
public function __construct(HttpMessageFactoryInterface $httpMessageFactory) |
24
|
6 |
|
{ |
25
|
|
|
$this->httpMessageFactory = $httpMessageFactory; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function createRequest(Request $symfonyRequest) |
29
|
4 |
|
{ |
30
|
|
|
// Call the original object to avoid duplicating code. |
31
|
|
|
$request = $this->httpMessageFactory->createRequest($symfonyRequest); |
32
|
4 |
|
|
33
|
|
|
// If a query string does not exist, return the original object. |
34
|
|
|
if ('' === $unalteredQueryString = $symfonyRequest->server->get('QUERY_STRING', '')) { |
35
|
4 |
|
return $request; |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
// This is where all is happening. |
39
|
|
|
// We do not rely on $symfonyRequest->query->all() because it relies |
40
|
|
|
// on parse_str() which is altering the query string parameters. |
41
|
|
|
// We rely on $symfonyRequest->server->get('QUERY_STRING') so we are |
42
|
|
|
// sure that the query string hasn't been altered. |
43
|
|
|
// Create a new request with the URI and Params updated. |
44
|
|
|
return $request |
45
|
|
|
->withQueryParams( |
46
|
2 |
|
QueryString::extract($unalteredQueryString, '&', PHP_QUERY_RFC1738) |
47
|
2 |
|
) |
48
|
|
|
->withUri( |
49
|
2 |
|
$request |
50
|
|
|
->getUri() |
51
|
2 |
|
->withQuery($unalteredQueryString) |
52
|
2 |
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function createResponse(Response $symfonyResponse) |
56
|
|
|
{ |
57
|
|
|
return $this->httpMessageFactory->createResponse($symfonyResponse); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|