Complex classes like THttpResponse 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 THttpResponse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 66 | class THttpResponse extends \Prado\TModule implements \Prado\IO\ITextWriter |
||
| 67 | { |
||
| 68 | const DEFAULT_CONTENTTYPE = 'text/html'; |
||
| 69 | const DEFAULT_CHARSET = 'UTF-8'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var The differents defined status code by RFC 2616 {@link http://www.faqs.org/rfcs/rfc2616} |
||
| 73 | */ |
||
| 74 | private static $HTTP_STATUS_CODES = array( |
||
| 75 | 100 => 'Continue', 101 => 'Switching Protocols', |
||
| 76 | 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', |
||
| 77 | 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', |
||
| 78 | 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', |
||
| 79 | 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported' |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var boolean whether to buffer output |
||
| 84 | */ |
||
| 85 | private $_bufferOutput=true; |
||
| 86 | /** |
||
| 87 | * @var boolean if the application is initialized |
||
| 88 | */ |
||
| 89 | private $_initialized=false; |
||
| 90 | /** |
||
| 91 | * @var THttpCookieCollection list of cookies to return |
||
| 92 | */ |
||
| 93 | private $_cookies=null; |
||
| 94 | /** |
||
| 95 | * @var integer response status code |
||
| 96 | */ |
||
| 97 | private $_status=200; |
||
| 98 | /** |
||
| 99 | * @var string reason correspond to status code |
||
| 100 | */ |
||
| 101 | private $_reason='OK'; |
||
| 102 | /** |
||
| 103 | * @var string HTML writer type |
||
| 104 | */ |
||
| 105 | private $_htmlWriterType='\Prado\Web\UI\THtmlWriter'; |
||
| 106 | /** |
||
| 107 | * @var string content type |
||
| 108 | */ |
||
| 109 | private $_contentType=null; |
||
| 110 | /** |
||
| 111 | * @var string|boolean character set, e.g. UTF-8 or false if no character set should be send to client |
||
| 112 | */ |
||
| 113 | private $_charset=''; |
||
| 114 | /** |
||
| 115 | * @var THttpResponseAdapter adapter. |
||
| 116 | */ |
||
| 117 | private $_adapter; |
||
| 118 | /** |
||
| 119 | * @var boolean whether http response header has been sent |
||
| 120 | */ |
||
| 121 | private $_httpHeaderSent; |
||
| 122 | /** |
||
| 123 | * @var boolean whether content-type header has been sent |
||
| 124 | */ |
||
| 125 | private $_contentTypeHeaderSent; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Destructor. |
||
| 129 | * Flushes any existing content in buffer. |
||
| 130 | */ |
||
| 131 | public function __destruct() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param THttpResponseAdapter response adapter |
||
| 139 | */ |
||
| 140 | public function setAdapter(THttpResponseAdapter $adapter) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return THttpResponseAdapter response adapter, null if not exist. |
||
| 147 | */ |
||
| 148 | public function getAdapter() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return boolean true if adapter exists, false otherwise. |
||
| 155 | */ |
||
| 156 | public function getHasAdapter() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Initializes the module. |
||
| 163 | * This method is required by IModule and is invoked by application. |
||
| 164 | * It starts output buffer if it is enabled. |
||
| 165 | * @param TXmlElement module configuration |
||
| 166 | */ |
||
| 167 | public function init($config) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return integer time-to-live for cached session pages in minutes, this has no effect for nocache limiter. Defaults to 180. |
||
| 177 | */ |
||
| 178 | public function getCacheExpire() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param integer time-to-live for cached session pages in minutes, this has no effect for nocache limiter. |
||
| 185 | */ |
||
| 186 | public function setCacheExpire($value) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string cache control method to use for session pages |
||
| 193 | */ |
||
| 194 | public function getCacheControl() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string cache control method to use for session pages. Valid values |
||
| 201 | * include none/nocache/private/private_no_expire/public |
||
| 202 | */ |
||
| 203 | public function setCacheControl($value) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string content type, default is text/html |
||
| 210 | */ |
||
| 211 | public function setContentType($type) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return string current content type |
||
| 220 | */ |
||
| 221 | public function getContentType() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return string|boolean output charset. |
||
| 228 | */ |
||
| 229 | public function getCharset() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string|boolean output charset. |
||
| 236 | */ |
||
| 237 | public function setCharset($charset) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return boolean whether to enable output buffer |
||
| 244 | */ |
||
| 245 | public function getBufferOutput() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param boolean whether to enable output buffer |
||
| 252 | * @throws TInvalidOperationException if session is started already |
||
| 253 | */ |
||
| 254 | public function setBufferOutput($value) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return integer HTTP status code, defaults to 200 |
||
| 264 | */ |
||
| 265 | public function getStatusCode() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Set the HTTP status code for the response. |
||
| 272 | * The code and its reason will be sent to client using the currently requested http protocol version (see {@link THttpRequest::getHttpProtocolVersion}) |
||
| 273 | * Keep in mind that HTTP/1.0 clients might not understand all status codes from HTTP/1.1 |
||
| 274 | * |
||
| 275 | * @param integer HTTP status code |
||
| 276 | * @param string HTTP status reason, defaults to standard HTTP reasons |
||
| 277 | */ |
||
| 278 | public function setStatusCode($status, $reason=null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string HTTP status reason |
||
| 300 | */ |
||
| 301 | public function getStatusReason() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return THttpCookieCollection list of output cookies |
||
| 307 | */ |
||
| 308 | public function getCookies() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Outputs a string. |
||
| 317 | * It may not be sent back to user immediately if output buffer is enabled. |
||
| 318 | * @param string string to be output |
||
| 319 | */ |
||
| 320 | public function write($str) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Sends a file back to user. |
||
| 330 | * Make sure not to output anything else after calling this method. |
||
| 331 | * @param string file name |
||
| 332 | * @param string content to be set. If null, the content will be read from the server file pointed to by $fileName. |
||
| 333 | * @param string mime type of the content. |
||
| 334 | * @param array list of headers to be sent. Each array element represents a header string (e.g. 'Content-Type: text/plain'). |
||
| 335 | * @param boolean force download of file, even if browser able to display inline. Defaults to 'true'. |
||
| 336 | * @param string force a specific file name on client side. Defaults to 'null' means auto-detect. |
||
| 337 | * @param integer size of file or content in bytes if already known. Defaults to 'null' means auto-detect. |
||
| 338 | * @throws TInvalidDataValueException if the file cannot be found |
||
| 339 | */ |
||
| 340 | public function writeFile($fileName,$content=null,$mimeType=null,$headers=null,$forceDownload=true,$clientFileName=null,$fileSize=null) |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Redirects the browser to the specified URL. |
||
| 402 | * The current application will be terminated after this method is invoked. |
||
| 403 | * @param string URL to be redirected to. If the URL is a relative one, the base URL of |
||
| 404 | * the current request will be inserted at the beginning. |
||
| 405 | */ |
||
| 406 | public function redirect($url) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Redirect the browser to another URL and exists the current application. |
||
| 416 | * This method is used internally. Please use {@link redirect} instead. |
||
| 417 | * |
||
| 418 | * @since 3.1.5 |
||
| 419 | * You can set the set {@link setStatusCode StatusCode} to a value between 300 and 399 before |
||
| 420 | * calling this function to change the type of redirection. |
||
| 421 | * If not specified, StatusCode will be 302 (Found) by default |
||
| 422 | * |
||
| 423 | * @param string URL to be redirected to. If the URL is a relative one, the base URL of |
||
| 424 | * the current request will be inserted at the beginning. |
||
| 425 | */ |
||
| 426 | public function httpRedirect($url) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Reloads the current page. |
||
| 461 | * The effect of this method call is the same as user pressing the |
||
| 462 | * refresh button on his browser (without post data). |
||
| 463 | **/ |
||
| 464 | public function reload() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Flush the response contents and headers. |
||
| 471 | */ |
||
| 472 | public function flush($continueBuffering = true) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Ensures that HTTP response and content-type headers are sent |
||
| 482 | */ |
||
| 483 | public function ensureHeadersSent() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Outputs the buffered content, sends content-type and charset header. |
||
| 491 | * This method is used internally. Please use {@link flush} instead. |
||
| 492 | * @param boolean whether to continue buffering after flush if buffering was active |
||
| 493 | */ |
||
| 494 | public function flushContent($continueBuffering = true) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Ensures that the HTTP header with the status code and status reason are sent |
||
| 519 | */ |
||
| 520 | protected function ensureHttpHeaderSent() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Send the HTTP header with the status code (defaults to 200) and status reason (defaults to OK) |
||
| 528 | */ |
||
| 529 | protected function sendHttpHeader() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Ensures that the HTTP header with the status code and status reason are sent |
||
| 545 | */ |
||
| 546 | protected function ensureContentTypeHeaderSent() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Sends content type header with optional charset. |
||
| 554 | */ |
||
| 555 | protected function sendContentTypeHeader() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns the content in the output buffer. |
||
| 575 | * The buffer will NOT be cleared after calling this method. |
||
| 576 | * Use {@link clear()} is you want to clear the buffer. |
||
| 577 | * @return string output that is in the buffer. |
||
| 578 | */ |
||
| 579 | public function getContents() |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Clears any existing buffered content. |
||
| 587 | */ |
||
| 588 | public function clear() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param integer|null Either {@link CASE_UPPER} or {@link CASE_LOWER} or as is null (default) |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | public function getHeaders($case=null) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Sends a header. |
||
| 621 | * @param string header |
||
| 622 | * @param boolean whether the header should replace a previous similar header, or add a second header of the same type |
||
| 623 | */ |
||
| 624 | public function appendHeader($value, $replace=true) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Writes a log message into error log. |
||
| 632 | * This method is simple wrapper of PHP function error_log. |
||
| 633 | * @param string The error message that should be logged |
||
| 634 | * @param integer where the error should go |
||
| 635 | * @param string The destination. Its meaning depends on the message parameter as described above |
||
| 636 | * @param string The extra headers. It's used when the message parameter is set to 1. This message type uses the same internal function as mail() does. |
||
| 637 | * @see http://us2.php.net/manual/en/function.error-log.php |
||
| 638 | */ |
||
| 639 | public function appendLog($message,$messageType=0,$destination='',$extraHeaders='') |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Sends a cookie. |
||
| 646 | * Do not call this method directly. Operate with the result of {@link getCookies} instead. |
||
| 647 | * @param THttpCookie cook to be sent |
||
| 648 | */ |
||
| 649 | public function addCookie($cookie) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Deletes a cookie. |
||
| 680 | * Do not call this method directly. Operate with the result of {@link getCookies} instead. |
||
| 681 | * @param THttpCookie cook to be deleted |
||
| 682 | */ |
||
| 683 | public function removeCookie($cookie) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @return string the type of HTML writer to be used, defaults to THtmlWriter |
||
| 698 | */ |
||
| 699 | public function getHtmlWriterType() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string the type of HTML writer to be used, may be the class name or the namespace |
||
| 706 | */ |
||
| 707 | public function setHtmlWriterType($value) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Creates a new instance of HTML writer. |
||
| 714 | * If the type of the HTML writer is not supplied, {@link getHtmlWriterType HtmlWriterType} will be assumed. |
||
| 715 | * @param string type of the HTML writer to be created. If null, {@link getHtmlWriterType HtmlWriterType} will be assumed. |
||
| 716 | */ |
||
| 717 | public function createHtmlWriter($type=null) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Create a new html writer instance. |
||
| 729 | * This method is used internally. Please use {@link createHtmlWriter} instead. |
||
| 730 | * @param string type of HTML writer to be created. |
||
| 731 | * @param ITextWriter text writer holding the contents. |
||
| 732 | */ |
||
| 733 | public function createNewHtmlWriter($type, $writer) |
||
| 737 | } |
||
| 738 | |||
| 739 |