Complex classes like Response often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Response, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class Response extends \yii\base\Response implements ResponseInterface |
||
69 | { |
||
70 | use MessageTrait; |
||
71 | |||
72 | /** |
||
73 | * @event ResponseEvent an event that is triggered at the beginning of [[send()]]. |
||
74 | */ |
||
75 | const EVENT_BEFORE_SEND = 'beforeSend'; |
||
76 | /** |
||
77 | * @event ResponseEvent an event that is triggered at the end of [[send()]]. |
||
78 | */ |
||
79 | const EVENT_AFTER_SEND = 'afterSend'; |
||
80 | /** |
||
81 | * @event ResponseEvent an event that is triggered right after [[prepare()]] is called in [[send()]]. |
||
82 | * You may respond to this event to filter the response content before it is sent to the client. |
||
83 | */ |
||
84 | const EVENT_AFTER_PREPARE = 'afterPrepare'; |
||
85 | const FORMAT_RAW = 'raw'; |
||
86 | const FORMAT_HTML = 'html'; |
||
87 | const FORMAT_JSON = 'json'; |
||
88 | const FORMAT_JSONP = 'jsonp'; |
||
89 | const FORMAT_XML = 'xml'; |
||
90 | |||
91 | /** |
||
92 | * @var string the response format. This determines how to convert [[data]] into [[content]] |
||
93 | * when the latter is not set. The value of this property must be one of the keys declared in the [[formatters]] array. |
||
94 | * By default, the following formats are supported: |
||
95 | * |
||
96 | * - [[FORMAT_RAW]]: the data will be treated as the response content without any conversion. |
||
97 | * No extra HTTP header will be added. |
||
98 | * - [[FORMAT_HTML]]: the data will be treated as the response content without any conversion. |
||
99 | * The "Content-Type" header will set as "text/html". |
||
100 | * - [[FORMAT_JSON]]: the data will be converted into JSON format, and the "Content-Type" |
||
101 | * header will be set as "application/json". |
||
102 | * - [[FORMAT_JSONP]]: the data will be converted into JSONP format, and the "Content-Type" |
||
103 | * header will be set as "text/javascript". Note that in this case `$data` must be an array |
||
104 | * with "data" and "callback" elements. The former refers to the actual data to be sent, |
||
105 | * while the latter refers to the name of the JavaScript callback. |
||
106 | * - [[FORMAT_XML]]: the data will be converted into XML format. Please refer to [[XmlResponseFormatter]] |
||
107 | * for more details. |
||
108 | * |
||
109 | * You may customize the formatting process or support additional formats by configuring [[formatters]]. |
||
110 | * @see formatters |
||
111 | */ |
||
112 | public $format = self::FORMAT_HTML; |
||
113 | /** |
||
114 | * @var string the MIME type (e.g. `application/json`) from the request ACCEPT header chosen for this response. |
||
115 | * This property is mainly set by [[\yii\filters\ContentNegotiator]]. |
||
116 | */ |
||
117 | public $acceptMimeType; |
||
118 | /** |
||
119 | * @var array the parameters (e.g. `['q' => 1, 'version' => '1.0']`) associated with the [[acceptMimeType|chosen MIME type]]. |
||
120 | * This is a list of name-value pairs associated with [[acceptMimeType]] from the ACCEPT HTTP header. |
||
121 | * This property is mainly set by [[\yii\filters\ContentNegotiator]]. |
||
122 | */ |
||
123 | public $acceptParams = []; |
||
124 | /** |
||
125 | * @var array the formatters for converting data into the response content of the specified [[format]]. |
||
126 | * The array keys are the format names, and the array values are the corresponding configurations |
||
127 | * for creating the formatter objects. |
||
128 | * @see format |
||
129 | * @see defaultFormatters |
||
130 | */ |
||
131 | public $formatters = []; |
||
132 | /** |
||
133 | * @var mixed the original response data. When this is not null, it will be converted into [[content]] |
||
134 | * according to [[format]] when the response is being sent out. |
||
135 | * @see content |
||
136 | */ |
||
137 | public $data; |
||
138 | /** |
||
139 | * @var array the stream range to be applied on [[send()]]. This should be an array of the begin position and the end position. |
||
140 | * Note that when this property is set, the [[data]] property will be ignored by [[send()]]. |
||
141 | */ |
||
142 | public $bodyRange; |
||
143 | /** |
||
144 | * @var string the charset of the text response. If not set, it will use |
||
145 | * the value of [[Application::charset]]. |
||
146 | */ |
||
147 | public $charset; |
||
148 | /** |
||
149 | * @var string the HTTP status description that comes together with the status code. |
||
150 | * @see httpStatuses |
||
151 | */ |
||
152 | public $reasonPhrase = 'OK'; |
||
153 | /** |
||
154 | * @var bool whether the response has been sent. If this is true, calling [[send()]] will do nothing. |
||
155 | */ |
||
156 | public $isSent = false; |
||
157 | /** |
||
158 | * @var array list of HTTP status codes and the corresponding texts |
||
159 | */ |
||
160 | public static $httpStatuses = [ |
||
161 | 100 => 'Continue', |
||
162 | 101 => 'Switching Protocols', |
||
163 | 102 => 'Processing', |
||
164 | 118 => 'Connection timed out', |
||
165 | 200 => 'OK', |
||
166 | 201 => 'Created', |
||
167 | 202 => 'Accepted', |
||
168 | 203 => 'Non-Authoritative', |
||
169 | 204 => 'No Content', |
||
170 | 205 => 'Reset Content', |
||
171 | 206 => 'Partial Content', |
||
172 | 207 => 'Multi-Status', |
||
173 | 208 => 'Already Reported', |
||
174 | 210 => 'Content Different', |
||
175 | 226 => 'IM Used', |
||
176 | 300 => 'Multiple Choices', |
||
177 | 301 => 'Moved Permanently', |
||
178 | 302 => 'Found', |
||
179 | 303 => 'See Other', |
||
180 | 304 => 'Not Modified', |
||
181 | 305 => 'Use Proxy', |
||
182 | 306 => 'Reserved', |
||
183 | 307 => 'Temporary Redirect', |
||
184 | 308 => 'Permanent Redirect', |
||
185 | 310 => 'Too many Redirect', |
||
186 | 400 => 'Bad Request', |
||
187 | 401 => 'Unauthorized', |
||
188 | 402 => 'Payment Required', |
||
189 | 403 => 'Forbidden', |
||
190 | 404 => 'Not Found', |
||
191 | 405 => 'Method Not Allowed', |
||
192 | 406 => 'Not Acceptable', |
||
193 | 407 => 'Proxy Authentication Required', |
||
194 | 408 => 'Request Time-out', |
||
195 | 409 => 'Conflict', |
||
196 | 410 => 'Gone', |
||
197 | 411 => 'Length Required', |
||
198 | 412 => 'Precondition Failed', |
||
199 | 413 => 'Request Entity Too Large', |
||
200 | 414 => 'Request-URI Too Long', |
||
201 | 415 => 'Unsupported Media Type', |
||
202 | 416 => 'Requested range unsatisfiable', |
||
203 | 417 => 'Expectation failed', |
||
204 | 418 => 'I\'m a teapot', |
||
205 | 421 => 'Misdirected Request', |
||
206 | 422 => 'Unprocessable entity', |
||
207 | 423 => 'Locked', |
||
208 | 424 => 'Method failure', |
||
209 | 425 => 'Unordered Collection', |
||
210 | 426 => 'Upgrade Required', |
||
211 | 428 => 'Precondition Required', |
||
212 | 429 => 'Too Many Requests', |
||
213 | 431 => 'Request Header Fields Too Large', |
||
214 | 449 => 'Retry With', |
||
215 | 450 => 'Blocked by Windows Parental Controls', |
||
216 | 451 => 'Unavailable For Legal Reasons', |
||
217 | 500 => 'Internal Server Error', |
||
218 | 501 => 'Not Implemented', |
||
219 | 502 => 'Bad Gateway or Proxy Error', |
||
220 | 503 => 'Service Unavailable', |
||
221 | 504 => 'Gateway Time-out', |
||
222 | 505 => 'HTTP Version not supported', |
||
223 | 507 => 'Insufficient storage', |
||
224 | 508 => 'Loop Detected', |
||
225 | 509 => 'Bandwidth Limit Exceeded', |
||
226 | 510 => 'Not Extended', |
||
227 | 511 => 'Network Authentication Required', |
||
228 | ]; |
||
229 | |||
230 | /** |
||
231 | * @var int the HTTP status code to send with the response. |
||
232 | */ |
||
233 | private $_statusCode = 200; |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Initializes this component. |
||
238 | */ |
||
239 | 156 | public function init() |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 32 | public function getStatusCode() |
|
254 | |||
255 | /** |
||
256 | * Sets the response status code. |
||
257 | * This method will set the corresponding status text if `$text` is null. |
||
258 | * @param int $code the status code |
||
259 | * @param string $reasonPhrase the status text. If not set, it will be set automatically based on the status code. |
||
260 | * @throws InvalidArgumentException if the status code is invalid. |
||
261 | * @return $this the response object itself |
||
262 | */ |
||
263 | 30 | public function setStatusCode($code, $reasonPhrase = null) |
|
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function withStatus($code, $reasonPhrase = '') |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | 13 | public function getReasonPhrase() |
|
301 | |||
302 | /** |
||
303 | * Sets the response status code based on the exception. |
||
304 | * @param \Exception|\Error $e the exception object. |
||
305 | * @throws InvalidArgumentException if the status code is invalid. |
||
306 | * @return $this the response object itself |
||
307 | * @since 2.0.12 |
||
308 | */ |
||
309 | 14 | public function setStatusCodeByException($e) |
|
318 | |||
319 | /** |
||
320 | * @return string body content string. |
||
321 | * @since 2.1.0 |
||
322 | */ |
||
323 | 51 | public function getContent() |
|
327 | |||
328 | /** |
||
329 | * @param string $content body content string. |
||
330 | * @since 2.1.0 |
||
331 | */ |
||
332 | 52 | public function setContent($content) |
|
338 | |||
339 | /** |
||
340 | * Sends the response to the client. |
||
341 | */ |
||
342 | 15 | public function send() |
|
343 | { |
||
344 | 15 | if ($this->isSent) { |
|
345 | return; |
||
346 | } |
||
347 | 15 | $this->trigger(self::EVENT_BEFORE_SEND); |
|
348 | 15 | $this->prepare(); |
|
349 | 15 | $this->trigger(self::EVENT_AFTER_PREPARE); |
|
350 | 15 | $this->sendHeaders(); |
|
351 | 15 | $this->sendContent(); |
|
352 | 15 | $this->trigger(self::EVENT_AFTER_SEND); |
|
353 | 15 | $this->isSent = true; |
|
354 | 15 | } |
|
355 | |||
356 | /** |
||
357 | * Clears the headers, cookies, content, status code of the response. |
||
358 | */ |
||
359 | public function clear() |
||
370 | |||
371 | /** |
||
372 | * Sends the response headers to the client |
||
373 | */ |
||
374 | 15 | protected function sendHeaders() |
|
396 | |||
397 | /** |
||
398 | * Sends the cookies to the client. |
||
399 | */ |
||
400 | protected function sendCookies() |
||
420 | |||
421 | /** |
||
422 | * Sends the response content to the client |
||
423 | */ |
||
424 | 15 | protected function sendContent() |
|
425 | { |
||
426 | 15 | $body = $this->getBody(); |
|
427 | 15 | if (!$body->isReadable()) { |
|
428 | throw new \RuntimeException('Unable to send content: body stream is not readable.'); |
||
429 | } |
||
430 | |||
431 | 15 | set_time_limit(0); // Reset time limit for big files |
|
432 | 15 | $chunkSize = 8 * 1024 * 1024; // 8MB per chunk |
|
433 | |||
434 | 15 | if (is_array($this->bodyRange)) { |
|
435 | [$begin, $end] = $this->bodyRange; |
||
436 | |||
437 | if (!$body->isSeekable()) { |
||
438 | throw new \RuntimeException('Unable to send content in range: body stream is not seekable.'); |
||
439 | } |
||
440 | |||
441 | $body->seek($begin); |
||
442 | while (!$body->eof() && ($pos = $body->tell()) <= $end) { |
||
443 | if ($pos + $chunkSize > $end) { |
||
444 | $chunkSize = $end - $pos + 1; |
||
445 | } |
||
446 | echo $body->read($chunkSize); |
||
447 | flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit. |
||
448 | } |
||
449 | $body->close(); |
||
450 | } else { |
||
451 | 15 | if ($body->isSeekable()) { |
|
452 | 15 | $body->seek(0); |
|
453 | } |
||
454 | 15 | while (!$body->eof()) { |
|
455 | 15 | echo $body->read($chunkSize); |
|
456 | 15 | flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit. |
|
457 | } |
||
458 | 15 | $body->close(); |
|
459 | 15 | return; |
|
460 | } |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Sends a file to the browser. |
||
465 | * |
||
466 | * Note that this method only prepares the response for file sending. The file is not sent |
||
467 | * until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action. |
||
468 | * |
||
469 | * The following is an example implementation of a controller action that allows requesting files from a directory |
||
470 | * that is not accessible from web: |
||
471 | * |
||
472 | * ```php |
||
473 | * public function actionFile($filename) |
||
474 | * { |
||
475 | * $storagePath = Yii::getAlias('@app/files'); |
||
476 | * |
||
477 | * // check filename for allowed chars (do not allow ../ to avoid security issue: downloading arbitrary files) |
||
478 | * if (!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $filename) || !is_file("$storagePath/$filename")) { |
||
479 | * throw new \yii\web\NotFoundHttpException('The file does not exists.'); |
||
480 | * } |
||
481 | * return Yii::$app->response->sendFile("$storagePath/$filename", $filename); |
||
482 | * } |
||
483 | * ``` |
||
484 | * |
||
485 | * @param string $filePath the path of the file to be sent. |
||
486 | * @param string $attachmentName the file name shown to the user. If null, it will be determined from `$filePath`. |
||
487 | * @param array $options additional options for sending the file. The following options are supported: |
||
488 | * |
||
489 | * - `mimeType`: the MIME type of the content. If not set, it will be guessed based on `$filePath` |
||
490 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
491 | * meaning a download dialog will pop up. |
||
492 | * |
||
493 | * @return $this the response object itself |
||
494 | * @see sendContentAsFile() |
||
495 | * @see sendStreamAsFile() |
||
496 | * @see xSendFile() |
||
497 | */ |
||
498 | public function sendFile($filePath, $attachmentName = null, $options = []) |
||
499 | { |
||
500 | if (!isset($options['mimeType'])) { |
||
501 | $options['mimeType'] = FileHelper::getMimeTypeByExtension($filePath); |
||
502 | } |
||
503 | if ($attachmentName === null) { |
||
504 | $attachmentName = basename($filePath); |
||
505 | } |
||
506 | $handle = fopen($filePath, 'rb'); |
||
507 | $this->sendStreamAsFile($handle, $attachmentName, $options); |
||
508 | |||
509 | return $this; |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * Sends the specified content as a file to the browser. |
||
514 | * |
||
515 | * Note that this method only prepares the response for file sending. The file is not sent |
||
516 | * until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action. |
||
517 | * |
||
518 | * @param string $content the content to be sent. The existing [[content]] will be discarded. |
||
519 | * @param string $attachmentName the file name shown to the user. |
||
520 | * @param array $options additional options for sending the file. The following options are supported: |
||
521 | * |
||
522 | * - `mimeType`: the MIME type of the content. Defaults to 'application/octet-stream'. |
||
523 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
524 | * meaning a download dialog will pop up. |
||
525 | * |
||
526 | * @return $this the response object itself |
||
527 | * @throws RangeNotSatisfiableHttpException if the requested range is not satisfiable |
||
528 | * @see sendFile() for an example implementation. |
||
529 | */ |
||
530 | 1 | public function sendContentAsFile($content, $attachmentName, $options = []) |
|
558 | |||
559 | /** |
||
560 | * Sends the specified stream as a file to the browser. |
||
561 | * |
||
562 | * Note that this method only prepares the response for file sending. The file is not sent |
||
563 | * until [[send()]] is called explicitly or implicitly. The latter is done after you return from a controller action. |
||
564 | * |
||
565 | * @param resource $handle the handle of the stream to be sent. |
||
566 | * @param string $attachmentName the file name shown to the user. |
||
567 | * @param array $options additional options for sending the file. The following options are supported: |
||
568 | * |
||
569 | * - `mimeType`: the MIME type of the content. Defaults to 'application/octet-stream'. |
||
570 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
571 | * meaning a download dialog will pop up. |
||
572 | * - `fileSize`: the size of the content to stream this is useful when size of the content is known |
||
573 | * and the content is not seekable. Defaults to content size using `ftell()`. |
||
574 | * This option is available since version 2.0.4. |
||
575 | * |
||
576 | * @return $this the response object itself |
||
577 | * @throws RangeNotSatisfiableHttpException if the requested range is not satisfiable |
||
578 | * @see sendFile() for an example implementation. |
||
579 | */ |
||
580 | public function sendStreamAsFile($handle, $attachmentName, $options = []) |
||
581 | { |
||
582 | if (isset($options['fileSize'])) { |
||
583 | $fileSize = $options['fileSize']; |
||
584 | } else { |
||
585 | fseek($handle, 0, SEEK_END); |
||
586 | $fileSize = ftell($handle); |
||
587 | } |
||
588 | |||
589 | $range = $this->getHttpRange($fileSize); |
||
590 | if ($range === false) { |
||
591 | $this->setHeader('Content-Range', "bytes */$fileSize"); |
||
592 | throw new RangeNotSatisfiableHttpException(); |
||
593 | } |
||
594 | |||
595 | [$begin, $end] = $range; |
||
596 | if ($begin != 0 || $end != $fileSize - 1) { |
||
597 | $this->setStatusCode(206); |
||
598 | $this->setHeader('Content-Range', "bytes $begin-$end/$fileSize"); |
||
599 | } else { |
||
600 | $this->setStatusCode(200); |
||
601 | } |
||
602 | |||
603 | $mimeType = isset($options['mimeType']) ? $options['mimeType'] : 'application/octet-stream'; |
||
604 | $this->setDownloadHeaders($attachmentName, $mimeType, !empty($options['inline']), $end - $begin + 1); |
||
605 | |||
606 | $this->format = self::FORMAT_RAW; |
||
607 | $this->bodyRange = [$begin, $end]; |
||
608 | |||
609 | $body = new ResourceStream(); |
||
610 | $body->resource = $handle; |
||
611 | |||
612 | $this->setBody($body); |
||
613 | return $this; |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * Sets a default set of HTTP headers for file downloading purpose. |
||
618 | * @param string $attachmentName the attachment file name |
||
619 | * @param string $mimeType the MIME type for the response. If null, `Content-Type` header will NOT be set. |
||
620 | * @param bool $inline whether the browser should open the file within the browser window. Defaults to false, |
||
621 | * meaning a download dialog will pop up. |
||
622 | * @param int $contentLength the byte length of the file being downloaded. If null, `Content-Length` header will NOT be set. |
||
623 | * @return $this the response object itself |
||
624 | */ |
||
625 | 1 | public function setDownloadHeaders($attachmentName, $mimeType = null, $inline = false, $contentLength = null) |
|
653 | |||
654 | /** |
||
655 | * Determines the HTTP range given in the request. |
||
656 | * @param int $fileSize the size of the file that will be used to validate the requested HTTP range. |
||
657 | * @return array|bool the range (begin, end), or false if the range request is invalid. |
||
658 | */ |
||
659 | 1 | protected function getHttpRange($fileSize) |
|
660 | { |
||
661 | 1 | $rangeHeader = Yii::$app->getRequest()->getHeaderLine('Range'); |
|
662 | |||
663 | 1 | if (empty($rangeHeader) || $rangeHeader === '-') { |
|
664 | 1 | return [0, $fileSize - 1]; |
|
665 | } |
||
666 | if (!preg_match('/^bytes=(\d*)-(\d*)$/', $rangeHeader, $matches)) { |
||
667 | return false; |
||
668 | } |
||
669 | if ($matches[1] === '') { |
||
670 | $start = $fileSize - $matches[2]; |
||
671 | $end = $fileSize - 1; |
||
672 | } elseif ($matches[2] !== '') { |
||
673 | $start = $matches[1]; |
||
674 | $end = $matches[2]; |
||
675 | if ($end >= $fileSize) { |
||
676 | $end = $fileSize - 1; |
||
677 | } |
||
678 | } else { |
||
679 | $start = $matches[1]; |
||
680 | $end = $fileSize - 1; |
||
681 | } |
||
682 | if ($start < 0 || $start > $end) { |
||
683 | return false; |
||
684 | } |
||
685 | |||
686 | return [$start, $end]; |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * Sends existing file to a browser as a download using x-sendfile. |
||
691 | * |
||
692 | * X-Sendfile is a feature allowing a web application to redirect the request for a file to the webserver |
||
693 | * that in turn processes the request, this way eliminating the need to perform tasks like reading the file |
||
694 | * and sending it to the user. When dealing with a lot of files (or very big files) this can lead to a great |
||
695 | * increase in performance as the web application is allowed to terminate earlier while the webserver is |
||
696 | * handling the request. |
||
697 | * |
||
698 | * The request is sent to the server through a special non-standard HTTP-header. |
||
699 | * When the web server encounters the presence of such header it will discard all output and send the file |
||
700 | * specified by that header using web server internals including all optimizations like caching-headers. |
||
701 | * |
||
702 | * As this header directive is non-standard different directives exists for different web servers applications: |
||
703 | * |
||
704 | * - Apache: [X-Sendfile](http://tn123.org/mod_xsendfile) |
||
705 | * - Lighttpd v1.4: [X-LIGHTTPD-send-file](http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file) |
||
706 | * - Lighttpd v1.5: [X-Sendfile](http://redmine.lighttpd.net/projects/lighttpd/wiki/X-LIGHTTPD-send-file) |
||
707 | * - Nginx: [X-Accel-Redirect](http://wiki.nginx.org/XSendfile) |
||
708 | * - Cherokee: [X-Sendfile and X-Accel-Redirect](http://www.cherokee-project.com/doc/other_goodies.html#x-sendfile) |
||
709 | * |
||
710 | * So for this method to work the X-SENDFILE option/module should be enabled by the web server and |
||
711 | * a proper xHeader should be sent. |
||
712 | * |
||
713 | * **Note** |
||
714 | * |
||
715 | * This option allows to download files that are not under web folders, and even files that are otherwise protected |
||
716 | * (deny from all) like `.htaccess`. |
||
717 | * |
||
718 | * **Side effects** |
||
719 | * |
||
720 | * If this option is disabled by the web server, when this method is called a download configuration dialog |
||
721 | * will open but the downloaded file will have 0 bytes. |
||
722 | * |
||
723 | * **Known issues** |
||
724 | * |
||
725 | * There is a Bug with Internet Explorer 6, 7 and 8 when X-SENDFILE is used over an SSL connection, it will show |
||
726 | * an error message like this: "Internet Explorer was not able to open this Internet site. The requested site |
||
727 | * is either unavailable or cannot be found.". You can work around this problem by removing the `Pragma`-header. |
||
728 | * |
||
729 | * **Example** |
||
730 | * |
||
731 | * ```php |
||
732 | * Yii::$app->response->xSendFile('/home/user/Pictures/picture1.jpg'); |
||
733 | * ``` |
||
734 | * |
||
735 | * @param string $filePath file name with full path |
||
736 | * @param string $attachmentName file name shown to the user. If null, it will be determined from `$filePath`. |
||
737 | * @param array $options additional options for sending the file. The following options are supported: |
||
738 | * |
||
739 | * - `mimeType`: the MIME type of the content. If not set, it will be guessed based on `$filePath` |
||
740 | * - `inline`: boolean, whether the browser should open the file within the browser window. Defaults to false, |
||
741 | * meaning a download dialog will pop up. |
||
742 | * - xHeader: string, the name of the x-sendfile header. Defaults to "X-Sendfile". |
||
743 | * |
||
744 | * @return $this the response object itself |
||
745 | * @see sendFile() |
||
746 | */ |
||
747 | public function xSendFile($filePath, $attachmentName = null, $options = []) |
||
781 | |||
782 | /** |
||
783 | * Returns Content-Disposition header value that is safe to use with both old and new browsers |
||
784 | * |
||
785 | * Fallback name: |
||
786 | * |
||
787 | * - Causes issues if contains non-ASCII characters with codes less than 32 or more than 126. |
||
788 | * - Causes issues if contains urlencoded characters (starting with `%`) or `%` character. Some browsers interpret |
||
789 | * `filename="X"` as urlencoded name, some don't. |
||
790 | * - Causes issues if contains path separator characters such as `\` or `/`. |
||
791 | * - Since value is wrapped with `"`, it should be escaped as `\"`. |
||
792 | * - Since input could contain non-ASCII characters, fallback is obtained by transliteration. |
||
793 | * |
||
794 | * UTF name: |
||
795 | * |
||
796 | * - Causes issues if contains path separator characters such as `\` or `/`. |
||
797 | * - Should be urlencoded since headers are ASCII-only. |
||
798 | * - Could be omitted if it exactly matches fallback name. |
||
799 | * |
||
800 | * @param string $disposition |
||
801 | * @param string $attachmentName |
||
802 | * @return string |
||
803 | * |
||
804 | * @since 2.0.10 |
||
805 | */ |
||
806 | 1 | protected function getDispositionHeaderValue($disposition, $attachmentName) |
|
817 | |||
818 | /** |
||
819 | * Redirects the browser to the specified URL. |
||
820 | * |
||
821 | * This method adds a "Location" header to the current response. Note that it does not send out |
||
822 | * the header until [[send()]] is called. In a controller action you may use this method as follows: |
||
823 | * |
||
824 | * ```php |
||
825 | * return Yii::$app->getResponse()->redirect($url); |
||
826 | * ``` |
||
827 | * |
||
828 | * In other places, if you want to send out the "Location" header immediately, you should use |
||
829 | * the following code: |
||
830 | * |
||
831 | * ```php |
||
832 | * Yii::$app->getResponse()->redirect($url)->send(); |
||
833 | * return; |
||
834 | * ``` |
||
835 | * |
||
836 | * In AJAX mode, this normally will not work as expected unless there are some |
||
837 | * client-side JavaScript code handling the redirection. To help achieve this goal, |
||
838 | * this method will send out a "X-Redirect" header instead of "Location". |
||
839 | * |
||
840 | * If you use the "yii" JavaScript module, it will handle the AJAX redirection as |
||
841 | * described above. Otherwise, you should write the following JavaScript code to |
||
842 | * handle the redirection: |
||
843 | * |
||
844 | * ```javascript |
||
845 | * $document.ajaxComplete(function (event, xhr, settings) { |
||
846 | * var url = xhr && xhr.getResponseHeader('X-Redirect'); |
||
847 | * if (url) { |
||
848 | * window.location = url; |
||
849 | * } |
||
850 | * }); |
||
851 | * ``` |
||
852 | * |
||
853 | * @param string|array $url the URL to be redirected to. This can be in one of the following formats: |
||
854 | * |
||
855 | * - a string representing a URL (e.g. "http://example.com") |
||
856 | * - a string representing a URL alias (e.g. "@example.com") |
||
857 | * - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`). |
||
858 | * Note that the route is with respect to the whole application, instead of relative to a controller or module. |
||
859 | * [[Url::to()]] will be used to convert the array into a URL. |
||
860 | * |
||
861 | * Any relative URL that starts with a single forward slash "/" will be converted |
||
862 | * into an absolute one by prepending it with the host info of the current request. |
||
863 | * |
||
864 | * @param int $statusCode the HTTP status code. Defaults to 302. |
||
865 | * See <https://tools.ietf.org/html/rfc2616#section-10> |
||
866 | * for details about HTTP status code |
||
867 | * @param bool $checkAjax whether to specially handle AJAX (and PJAX) requests. Defaults to true, |
||
868 | * meaning if the current request is an AJAX or PJAX request, then calling this method will cause the browser |
||
869 | * to redirect to the given URL. If this is false, a `Location` header will be sent, which when received as |
||
870 | * an AJAX/PJAX response, may NOT cause browser redirection. |
||
871 | * Takes effect only when request header `X-Ie-Redirect-Compatibility` is absent. |
||
872 | * @return $this the response object itself |
||
873 | */ |
||
874 | 3 | public function redirect($url, $statusCode = 302, $checkAjax = true) |
|
875 | { |
||
876 | 3 | if (is_array($url) && isset($url[0])) { |
|
877 | // ensure the route is absolute |
||
878 | 2 | $url[0] = '/' . ltrim($url[0], '/'); |
|
879 | } |
||
880 | 3 | $url = Url::to($url); |
|
881 | 3 | if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { |
|
882 | 3 | $url = Yii::$app->getRequest()->getHostInfo() . $url; |
|
883 | } |
||
884 | |||
885 | 3 | if ($checkAjax) { |
|
886 | 3 | if (Yii::$app->getRequest()->getIsAjax()) { |
|
887 | if (Yii::$app->getRequest()->hasHeader('X-Ie-Redirect-Compatibility') && $statusCode === 302) { |
||
888 | // Ajax 302 redirect in IE does not work. Change status code to 200. See https://github.com/yiisoft/yii2/issues/9670 |
||
889 | $statusCode = 200; |
||
890 | } |
||
891 | if (Yii::$app->getRequest()->getIsPjax()) { |
||
892 | $this->setHeader('X-Pjax-Url', $url); |
||
893 | } else { |
||
894 | $this->setHeader('X-Redirect', $url); |
||
895 | } |
||
896 | } else { |
||
897 | 3 | $this->setHeader('Location', $url); |
|
898 | } |
||
899 | } else { |
||
900 | $this->setHeader('Location', $url); |
||
901 | } |
||
902 | |||
903 | 3 | $this->setStatusCode($statusCode); |
|
904 | |||
905 | 3 | return $this; |
|
906 | } |
||
907 | |||
908 | /** |
||
909 | * Refreshes the current page. |
||
910 | * The effect of this method call is the same as the user pressing the refresh button of his browser |
||
911 | * (without re-posting data). |
||
912 | * |
||
913 | * In a controller action you may use this method like this: |
||
914 | * |
||
915 | * ```php |
||
916 | * return Yii::$app->getResponse()->refresh(); |
||
917 | * ``` |
||
918 | * |
||
919 | * @param string $anchor the anchor that should be appended to the redirection URL. |
||
920 | * Defaults to empty. Make sure the anchor starts with '#' if you want to specify it. |
||
921 | * @return Response the response object itself |
||
922 | */ |
||
923 | public function refresh($anchor = '') |
||
927 | |||
928 | private $_cookies; |
||
929 | |||
930 | /** |
||
931 | * Returns the cookie collection. |
||
932 | * Through the returned cookie collection, you add or remove cookies as follows, |
||
933 | * |
||
934 | * ```php |
||
935 | * // add a cookie |
||
936 | * $response->cookies->add(new Cookie([ |
||
937 | * 'name' => $name, |
||
938 | * 'value' => $value, |
||
939 | * ]); |
||
940 | * |
||
941 | * // remove a cookie |
||
942 | * $response->cookies->remove('name'); |
||
943 | * // alternatively |
||
944 | * unset($response->cookies['name']); |
||
945 | * ``` |
||
946 | * |
||
947 | * @return CookieCollection the cookie collection. |
||
948 | */ |
||
949 | 36 | public function getCookies() |
|
956 | |||
957 | /** |
||
958 | * @return bool whether this response has a valid [[statusCode]]. |
||
959 | */ |
||
960 | 30 | public function getIsInvalid() |
|
964 | |||
965 | /** |
||
966 | * @return bool whether this response is informational |
||
967 | */ |
||
968 | public function getIsInformational() |
||
972 | |||
973 | /** |
||
974 | * @return bool whether this response is successful |
||
975 | */ |
||
976 | public function getIsSuccessful() |
||
980 | |||
981 | /** |
||
982 | * @return bool whether this response is a redirection |
||
983 | */ |
||
984 | 1 | public function getIsRedirection() |
|
988 | |||
989 | /** |
||
990 | * @return bool whether this response indicates a client error |
||
991 | */ |
||
992 | public function getIsClientError() |
||
996 | |||
997 | /** |
||
998 | * @return bool whether this response indicates a server error |
||
999 | */ |
||
1000 | public function getIsServerError() |
||
1004 | |||
1005 | /** |
||
1006 | * @return bool whether this response is OK |
||
1007 | */ |
||
1008 | public function getIsOk() |
||
1012 | |||
1013 | /** |
||
1014 | * @return bool whether this response indicates the current request is forbidden |
||
1015 | */ |
||
1016 | public function getIsForbidden() |
||
1020 | |||
1021 | /** |
||
1022 | * @return bool whether this response indicates the currently requested resource is not found |
||
1023 | */ |
||
1024 | public function getIsNotFound() |
||
1028 | |||
1029 | /** |
||
1030 | * @return bool whether this response is empty |
||
1031 | */ |
||
1032 | public function getIsEmpty() |
||
1036 | |||
1037 | /** |
||
1038 | * @return array the formatters that are supported by default |
||
1039 | */ |
||
1040 | 156 | protected function defaultFormatters() |
|
1058 | |||
1059 | /** |
||
1060 | * Prepares for sending the response. |
||
1061 | * The default implementation will convert [[data]] into [[content]] and set headers accordingly. |
||
1062 | * @throws InvalidConfigException if the formatter for the specified format is invalid or [[format]] is not supported |
||
1063 | */ |
||
1064 | 15 | protected function prepare() |
|
1065 | { |
||
1066 | 15 | if ($this->bodyRange !== null) { |
|
1067 | return; |
||
1068 | } |
||
1069 | |||
1070 | 15 | if (isset($this->formatters[$this->format])) { |
|
1071 | 14 | $formatter = $this->formatters[$this->format]; |
|
1072 | 14 | if (!is_object($formatter)) { |
|
1073 | 14 | $this->formatters[$this->format] = $formatter = Yii::createObject($formatter); |
|
1074 | } |
||
1075 | 14 | if ($formatter instanceof ResponseFormatterInterface) { |
|
1076 | 14 | $formatter->format($this); |
|
1077 | 14 | return; |
|
1078 | } |
||
1079 | throw new InvalidConfigException("The '{$this->format}' response formatter is invalid. It must implement the ResponseFormatterInterface."); |
||
1080 | 1 | } elseif ($this->format !== self::FORMAT_RAW) { |
|
1081 | throw new InvalidConfigException("Unsupported response format: {$this->format}"); |
||
1082 | } |
||
1083 | |||
1084 | 1 | if ($this->data !== null) { |
|
1085 | if (is_array($this->data)) { |
||
1086 | throw new InvalidArgumentException('Response raw data must not be an array.'); |
||
1087 | } elseif (is_object($this->data)) { |
||
1088 | if (method_exists($this->data, '__toString')) { |
||
1089 | $content = $this->data->__toString(); |
||
1090 | } else { |
||
1091 | throw new InvalidArgumentException('Response raw data must be a string or an object implementing ' |
||
1092 | . ' __toString().'); |
||
1093 | } |
||
1094 | } else { |
||
1095 | $content = $this->data; |
||
1096 | } |
||
1097 | |||
1098 | $body = new MemoryStream(); |
||
1099 | $body->write($content); |
||
1100 | $this->setBody($body); |
||
1101 | } |
||
1102 | 1 | } |
|
1103 | |||
1104 | /** |
||
1105 | * {@inheritdoc} |
||
1106 | */ |
||
1107 | public function __clone() |
||
1117 | } |
||
1118 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..