| Total Complexity | 41 |
| Total Lines | 395 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 |
||
| 17 | class Response |
||
|
1 ignored issue
–
show
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * 原始数据 |
||
| 21 | * @var mixed |
||
| 22 | */ |
||
| 23 | protected $data; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * 当前contentType |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $contentType = 'text/html'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * 字符集 |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $charset = 'utf-8'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * 状态码 |
||
| 39 | * @var integer |
||
| 40 | */ |
||
| 41 | protected $code = 200; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * 是否允许请求缓存 |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $allowCache = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * 输出参数 |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $options = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * header参数 |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $header = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * 输出内容 |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $content = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Cookie对象 |
||
| 69 | * @var Cookie |
||
| 70 | */ |
||
| 71 | protected $cookie; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Session对象 |
||
| 75 | * @var Session |
||
| 76 | */ |
||
| 77 | protected $session; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * 架构函数 |
||
| 81 | * @access public |
||
| 82 | * @param mixed $data 输出数据 |
||
| 83 | * @param int $code |
||
| 84 | */ |
||
| 85 | public function __construct($data = '', int $code = 200) |
||
| 86 | { |
||
| 87 | $this->data($data); |
||
| 88 | $this->code = $code; |
||
| 89 | |||
| 90 | $this->contentType($this->contentType, $this->charset); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * 创建Response对象 |
||
| 95 | * @access public |
||
| 96 | * @param mixed $data 输出数据 |
||
| 97 | * @param string $type 输出类型 |
||
| 98 | * @param int $code |
||
| 99 | * @return Response |
||
| 100 | */ |
||
| 101 | public static function create($data = '', string $type = '', int $code = 200): Response |
||
| 102 | { |
||
| 103 | $class = false !== strpos($type, '\\') ? $type : '\\think\\response\\' . ucfirst(strtolower($type)); |
||
| 104 | |||
| 105 | if (class_exists($class)) { |
||
| 106 | return Container::getInstance()->invokeClass($class, [$data, $code]); |
||
| 107 | } |
||
| 108 | |||
| 109 | return new static($data, $code); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * 设置Cookie对象 |
||
| 114 | * @access public |
||
| 115 | * @param Cookie $cookie Cookie对象 |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function setCookie(Cookie $cookie) |
||
| 119 | { |
||
| 120 | $this->cookie = $cookie; |
||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * 设置Session对象 |
||
| 126 | * @access public |
||
| 127 | * @param Session $session Session对象 |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function setSession(Session $session) |
||
| 131 | { |
||
| 132 | $this->session = $session; |
||
| 133 | return $this; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * 发送数据到客户端 |
||
| 138 | * @access public |
||
| 139 | * @return void |
||
| 140 | * @throws \InvalidArgumentException |
||
| 141 | */ |
||
| 142 | public function send(): void |
||
| 143 | { |
||
| 144 | // 处理输出数据 |
||
| 145 | $data = $this->getContent(); |
||
| 146 | |||
| 147 | if (!headers_sent() && !empty($this->header)) { |
||
| 148 | // 发送状态码 |
||
| 149 | http_response_code($this->code); |
||
| 150 | // 发送头部信息 |
||
| 151 | foreach ($this->header as $name => $val) { |
||
| 152 | header($name . (!is_null($val) ? ':' . $val : '')); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $this->cookie->save(); |
||
| 157 | |||
| 158 | $this->sendData($data); |
||
| 159 | |||
| 160 | if (function_exists('fastcgi_finish_request')) { |
||
| 161 | // 提高页面响应 |
||
| 162 | fastcgi_finish_request(); |
||
| 163 | } |
||
| 164 | |||
| 165 | // 清空当次请求有效的数据 |
||
| 166 | if (!($this instanceof RedirectResponse)) { |
||
| 167 | $this->session->flush(); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * 处理数据 |
||
| 173 | * @access protected |
||
| 174 | * @param mixed $data 要处理的数据 |
||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | protected function output($data) |
||
| 178 | { |
||
| 179 | return $data; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * 输出数据 |
||
| 184 | * @access protected |
||
| 185 | * @param string $data 要处理的数据 |
||
|
1 ignored issue
–
show
|
|||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | protected function sendData(string $data): void |
||
| 189 | { |
||
| 190 | echo $data; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * 输出的参数 |
||
| 195 | * @access public |
||
| 196 | * @param mixed $options 输出参数 |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function options(array $options = []) |
||
| 200 | { |
||
| 201 | $this->options = array_merge($this->options, $options); |
||
| 202 | |||
| 203 | return $this; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * 输出数据设置 |
||
| 208 | * @access public |
||
| 209 | * @param mixed $data 输出数据 |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | public function data($data) |
||
| 213 | { |
||
| 214 | $this->data = $data; |
||
| 215 | |||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * 是否允许请求缓存 |
||
| 221 | * @access public |
||
| 222 | * @param bool $cache 允许请求缓存 |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function allowCache(bool $cache) |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * 是否允许请求缓存 |
||
| 234 | * @access public |
||
| 235 | * @return $this |
||
| 236 | */ |
||
| 237 | public function isAllowCache() |
||
| 238 | { |
||
| 239 | return $this->allowCache; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * 设置响应头 |
||
| 244 | * @access public |
||
| 245 | * @param array $header 参数 |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function header(array $header = []) |
||
| 249 | { |
||
| 250 | $this->header = array_merge($this->header, $header); |
||
| 251 | |||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * 设置页面输出内容 |
||
| 257 | * @access public |
||
| 258 | * @param mixed $content |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function content($content) |
||
| 262 | { |
||
| 263 | if (null !== $content && !is_string($content) && !is_numeric($content) && !is_callable([ |
||
| 264 | $content, |
||
| 265 | '__toString', |
||
| 266 | ]) |
||
| 267 | ) { |
||
| 268 | throw new \InvalidArgumentException(sprintf('variable type error: %s', gettype($content))); |
||
| 269 | } |
||
| 270 | |||
| 271 | $this->content = (string) $content; |
||
| 272 | |||
| 273 | return $this; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * 发送HTTP状态 |
||
| 278 | * @access public |
||
| 279 | * @param integer $code 状态码 |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function code(int $code) |
||
| 283 | { |
||
| 284 | $this->code = $code; |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * LastModified |
||
| 291 | * @access public |
||
| 292 | * @param string $time |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | public function lastModified(string $time) |
||
| 296 | { |
||
| 297 | $this->header['Last-Modified'] = $time; |
||
| 298 | |||
| 299 | return $this; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Expires |
||
| 304 | * @access public |
||
| 305 | * @param string $time |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function expires(string $time) |
||
| 309 | { |
||
| 310 | $this->header['Expires'] = $time; |
||
| 311 | |||
| 312 | return $this; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * ETag |
||
| 317 | * @access public |
||
| 318 | * @param string $eTag |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | public function eTag(string $eTag) |
||
| 322 | { |
||
| 323 | $this->header['ETag'] = $eTag; |
||
| 324 | |||
| 325 | return $this; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * 页面缓存控制 |
||
| 330 | * @access public |
||
| 331 | * @param string $cache 状态码 |
||
| 332 | * @return $this |
||
| 333 | */ |
||
| 334 | public function cacheControl(string $cache) |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * 页面输出类型 |
||
| 343 | * @access public |
||
| 344 | * @param string $contentType 输出类型 |
||
| 345 | * @param string $charset 输出编码 |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function contentType(string $contentType, string $charset = 'utf-8') |
||
| 349 | { |
||
| 350 | $this->header['Content-Type'] = $contentType . '; charset=' . $charset; |
||
| 351 | |||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * 获取头部信息 |
||
| 357 | * @access public |
||
| 358 | * @param string $name 头部名称 |
||
| 359 | * @return mixed |
||
| 360 | */ |
||
| 361 | public function getHeader(string $name = '') |
||
| 362 | { |
||
| 363 | if (!empty($name)) { |
||
| 364 | return $this->header[$name] ?? null; |
||
| 365 | } |
||
| 366 | |||
| 367 | return $this->header; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * 获取原始数据 |
||
| 372 | * @access public |
||
| 373 | * @return mixed |
||
| 374 | */ |
||
| 375 | public function getData() |
||
| 376 | { |
||
| 377 | return $this->data; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * 获取输出数据 |
||
| 382 | * @access public |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function getContent(): string |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * 获取状态码 |
||
| 406 | * @access public |
||
| 407 | * @return integer |
||
| 408 | */ |
||
| 409 | public function getCode(): int |
||
| 410 | { |
||
| 411 | return $this->code; |
||
| 412 | } |
||
| 413 | } |
||
| 414 |