|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace TYPO3\CMS\Core\Http; |
|
19
|
|
|
|
|
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
21
|
|
|
use Psr\Http\Message\UriInterface; |
|
22
|
|
|
|
|
23
|
|
|
class ResponseFactory implements ResponseFactoryInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Default flags for json_encode; value of: |
|
27
|
|
|
* |
|
28
|
|
|
* <code> |
|
29
|
|
|
* JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES |
|
30
|
|
|
* </code> |
|
31
|
|
|
* |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
public const JSON_FLAGS_RFC4627 = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new response. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $code HTTP status code; defaults to 200 |
|
40
|
|
|
* @param string $reasonPhrase Reason phrase to associate with status code |
|
41
|
|
|
* @return ResponseInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return new Response(null, $code, [], $reasonPhrase); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function createHtmlResponse(string $html): ResponseInterface |
|
49
|
|
|
{ |
|
50
|
|
|
$response = $this->createResponse(); |
|
51
|
|
|
$response->withAddedHeader('Content-Type', 'text/html; charset=utf-8'); |
|
52
|
|
|
|
|
53
|
|
|
$stream = new Stream('php://temp', 'wb+'); |
|
54
|
|
|
$stream->write($html); |
|
55
|
|
|
$stream->rewind(); |
|
56
|
|
|
return $response->withBody($stream); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function createJsonResponse(string $json): ResponseInterface |
|
60
|
|
|
{ |
|
61
|
|
|
$response = $this->createResponse(); |
|
62
|
|
|
$response->withAddedHeader('Content-Type', 'application/json; charset=utf-8'); |
|
63
|
|
|
|
|
64
|
|
|
$stream = new Stream('php://temp', 'wb+'); |
|
65
|
|
|
$stream->write($json); |
|
66
|
|
|
$stream->rewind(); |
|
67
|
|
|
return $response->withBody($stream); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Create a JSON response with the given data. |
|
72
|
|
|
* |
|
73
|
|
|
* Default JSON encoding is performed with the following options, which |
|
74
|
|
|
* produces RFC4627-compliant JSON, capable of embedding into HTML. |
|
75
|
|
|
* |
|
76
|
|
|
* - {@see JSON_HEX_TAG} |
|
77
|
|
|
* - {@see JSON_HEX_APOS} |
|
78
|
|
|
* - {@see JSON_HEX_AMP} |
|
79
|
|
|
* - {@see JSON_HEX_QUOT} |
|
80
|
|
|
* - {@see JSON_UNESCAPED_SLASHES} |
|
81
|
|
|
* |
|
82
|
|
|
* @param mixed $data |
|
83
|
|
|
* @param int $encodingOptions |
|
84
|
|
|
* @return ResponseInterface |
|
85
|
|
|
* @throws \JsonException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function createJsonResponseFromData($data, int $encodingOptions = self::JSON_FLAGS_RFC4627): ResponseInterface |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->createJsonResponse((string)json_encode($data, $encodingOptions | JSON_THROW_ON_ERROR)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function createRedirectResponse(UriInterface $uri, int $code = 303): ResponseInterface |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->createResponse($code)->withAddedHeader('location', (string)$uri); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|