‪TYPO3CMS  10.4
Response.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Extbase\Mvc;
17 
21 
26 {
30  protected ‪$content;
31 
37  protected ‪$headers = [];
38 
44  protected ‪$additionalHeaderData = [];
45 
51  protected ‪$statusCode;
52 
58  protected ‪$statusMessage = 'OK';
59 
65  protected ‪$request;
66 
72  protected ‪$statusMessages = [
73  // INFORMATIONAL CODES
74  100 => 'Continue',
75  101 => 'Switching Protocols',
76  102 => 'Processing',
77  103 => 'Early Hints',
78  // SUCCESS CODES
79  200 => 'OK',
80  201 => 'Created',
81  202 => 'Accepted',
82  203 => 'Non-Authoritative Information',
83  204 => 'No Content',
84  205 => 'Reset Content',
85  206 => 'Partial Content',
86  207 => 'Multi-status',
87  208 => 'Already Reported',
88  226 => 'IM Used',
89  // REDIRECTION CODES
90  300 => 'Multiple Choices',
91  301 => 'Moved Permanently',
92  302 => 'Found',
93  303 => 'See Other',
94  304 => 'Not Modified',
95  305 => 'Use Proxy',
96  306 => 'Switch Proxy', // Deprecated
97  307 => 'Temporary Redirect',
98  308 => 'Permanent Redirect',
99  // CLIENT ERROR
100  400 => 'Bad Request',
101  401 => 'Unauthorized',
102  402 => 'Payment Required',
103  403 => 'Forbidden',
104  404 => 'Not Found',
105  405 => 'Method Not Allowed',
106  406 => 'Not Acceptable',
107  407 => 'Proxy Authentication Required',
108  408 => 'Request Timeout',
109  409 => 'Conflict',
110  410 => 'Gone',
111  411 => 'Length Required',
112  412 => 'Precondition Failed',
113  413 => 'Request Entity Too Large',
114  414 => 'URI Too Long',
115  415 => 'Unsupported Media Type',
116  416 => 'Requested range not satisfiable',
117  417 => 'Expectation Failed',
118  418 => 'I\'m a teapot',
119  422 => 'Unprocessable Entity',
120  423 => 'Locked',
121  424 => 'Failed Dependency',
122  425 => 'Unordered Collection',
123  426 => 'Upgrade Required',
124  428 => 'Precondition Required',
125  429 => 'Too Many Requests',
126  431 => 'Request Header Fields Too Large',
127  451 => 'Unavailable For Legal Reasons',
128  // SERVER ERROR
129  500 => 'Internal Server Error',
130  501 => 'Not Implemented',
131  502 => 'Bad Gateway',
132  503 => 'Service Unavailable',
133  504 => 'Gateway Time-out',
134  505 => 'HTTP Version not supported',
135  506 => 'Variant Also Negotiates',
136  507 => 'Insufficient Storage',
137  508 => 'Loop Detected',
138  509 => 'Bandwidth Limit Exceeded',
139  511 => 'Network Authentication Required',
140  ];
141 
147  public function ‪setContent(‪$content)
148  {
149  $this->content = ‪$content;
150  }
151 
157  public function ‪appendContent(‪$content)
158  {
159  $this->content .= ‪$content;
160  }
161 
167  public function ‪getContent()
168  {
169  return ‪$this->content;
170  }
171 
178  public function ‪shutdown()
179  {
180  ‪$content = $this->‪getContent();
181  $this->‪setContent('');
182  return ‪$content;
183  }
184 
190  public function ‪__toString()
191  {
192  return $this->‪getContent();
193  }
194 
202  public function ‪setStatus($code, $message = null)
203  {
204  if (!is_int($code)) {
205  throw new \InvalidArgumentException('The HTTP status code must be of type integer, ' . gettype($code) . ' given.', 1220526013);
206  }
207  if ($message === null && !isset($this->statusMessages[$code])) {
208  throw new \InvalidArgumentException('No message found for HTTP status code "' . $code . '".', 1220526014);
209  }
210  $this->statusCode = $code;
211  $this->statusMessage = $message ?? $this->statusMessages[$code];
212  }
213 
219  public function ‪getStatus()
220  {
221  return $this->statusCode . ' ' . ‪$this->statusMessage;
222  }
223 
230  public function ‪getStatusCode()
231  {
232  return $this->statusCode ?: 200;
233  }
234 
243  public function ‪setHeader($name, $value, $replaceExistingHeader = true)
244  {
245  if (stripos($name, 'HTTP') === 0) {
246  throw new \InvalidArgumentException('The HTTP status header must be set via setStatus().', 1220541963);
247  }
248  if ($replaceExistingHeader === true || !isset($this->headers[$name])) {
249  $this->headers[$name] = [$value];
250  } else {
251  $this->headers[$name][] = $value;
252  }
253  }
254 
260  public function ‪getHeaders()
261  {
262  $preparedHeaders = [];
263  if ($this->statusCode !== null) {
264  $protocolVersion = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
265  $statusHeader = $protocolVersion . ' ' . $this->statusCode . ' ' . ‪$this->statusMessage;
266  $preparedHeaders[] = $statusHeader;
267  }
268  foreach ($this->headers as $name => $values) {
269  foreach ($values as $value) {
270  $preparedHeaders[] = $name . ': ' . $value;
271  }
272  }
273  return $preparedHeaders;
274  }
275 
282  public function ‪getUnpreparedHeaders(): array
283  {
284  return ‪$this->headers;
285  }
286 
292  public function ‪sendHeaders()
293  {
294  if (headers_sent() === true) {
295  return;
296  }
297  foreach ($this->‪getHeaders() as $header) {
298  header($header);
299  }
300  }
301 
305  public function ‪send()
306  {
307  $this->‪sendHeaders();
308  if ($this->content !== null) {
309  echo $this->‪getContent();
310  }
311  }
312 
323  {
324  if (!is_string(‪$additionalHeaderData)) {
325  throw new \InvalidArgumentException('The additional header data must be of type String, ' . gettype(‪$additionalHeaderData) . ' given.', 1237370877);
326  }
327  if ($this->request->isCached()) {
329  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
330  $pageRenderer->addHeaderData(‪$additionalHeaderData);
331  } else {
332  $this->additionalHeaderData[] = ‪$additionalHeaderData;
333  }
334  }
335 
341  public function ‪getAdditionalHeaderData()
342  {
344  }
345 
350  public function ‪setRequest(‪Request ‪$request)
351  {
352  $this->request = ‪$request;
353  }
354 
359  public function ‪getRequest()
360  {
361  return ‪$this->request;
362  }
363 
367  protected function ‪getTypoScriptFrontendController()
368  {
369  return ‪$GLOBALS['TSFE'];
370  }
371 }
‪TYPO3\CMS\Extbase\Mvc\Response\getStatus
‪string getStatus()
Definition: Response.php:212
‪TYPO3\CMS\Extbase\Mvc\Response\$statusMessage
‪string $statusMessage
Definition: Response.php:53
‪TYPO3\CMS\Extbase\Mvc\Response\$headers
‪array $headers
Definition: Response.php:35
‪TYPO3\CMS\Extbase\Mvc\Response\$content
‪string $content
Definition: Response.php:29
‪TYPO3\CMS\Extbase\Mvc\Response\getTypoScriptFrontendController
‪TypoScriptFrontendController getTypoScriptFrontendController()
Definition: Response.php:360
‪TYPO3\CMS\Extbase\Mvc\Response\shutdown
‪string shutdown()
Definition: Response.php:171
‪TYPO3\CMS\Extbase\Mvc\Response\setHeader
‪setHeader($name, $value, $replaceExistingHeader=true)
Definition: Response.php:236
‪TYPO3\CMS\Extbase\Mvc\Response\getContent
‪string getContent()
Definition: Response.php:160
‪TYPO3\CMS\Extbase\Mvc\Response\$request
‪TYPO3 CMS Extbase Mvc Request $request
Definition: Response.php:59
‪TYPO3\CMS\Extbase\Mvc\ResponseInterface
Definition: ResponseInterface.php:22
‪TYPO3\CMS\Extbase\Mvc\Response\getRequest
‪TYPO3 CMS Extbase Mvc Request getRequest()
Definition: Response.php:352
‪TYPO3\CMS\Extbase\Mvc\Response\__toString
‪string __toString()
Definition: Response.php:183
‪TYPO3\CMS\Extbase\Mvc\Response\setStatus
‪setStatus($code, $message=null)
Definition: Response.php:195
‪TYPO3\CMS\Extbase\Mvc
‪TYPO3\CMS\Extbase\Mvc\Response\$additionalHeaderData
‪array $additionalHeaderData
Definition: Response.php:41
‪TYPO3\CMS\Extbase\Mvc\Response
Definition: Response.php:26
‪TYPO3\CMS\Extbase\Mvc\Response\send
‪send()
Definition: Response.php:298
‪TYPO3\CMS\Extbase\Mvc\Response\$statusMessages
‪array $statusMessages
Definition: Response.php:65
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:42
‪TYPO3\CMS\Extbase\Mvc\Response\getHeaders
‪string[] getHeaders()
Definition: Response.php:253
‪TYPO3\CMS\Extbase\Mvc\Response\appendContent
‪appendContent($content)
Definition: Response.php:150
‪TYPO3\CMS\Extbase\Mvc\Response\getUnpreparedHeaders
‪array getUnpreparedHeaders()
Definition: Response.php:275
‪TYPO3\CMS\Extbase\Mvc\Response\$statusCode
‪int $statusCode
Definition: Response.php:47
‪TYPO3\CMS\Extbase\Mvc\Response\sendHeaders
‪sendHeaders()
Definition: Response.php:285
‪TYPO3\CMS\Extbase\Mvc\Response\setContent
‪setContent($content)
Definition: Response.php:140
‪TYPO3\CMS\Extbase\Mvc\Response\addAdditionalHeaderData
‪addAdditionalHeaderData($additionalHeaderData)
Definition: Response.php:315
‪TYPO3\CMS\Extbase\Mvc\Response\setRequest
‪setRequest(Request $request)
Definition: Response.php:343
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Response\getStatusCode
‪int getStatusCode()
Definition: Response.php:223
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:31
‪TYPO3\CMS\Extbase\Mvc\Response\getAdditionalHeaderData
‪array getAdditionalHeaderData()
Definition: Response.php:334