Laravel响应准备publicfunctionprepareResponse($request,$response){if($responseinstanceofPsrResponseInterface){$response=(newHttpFoundationFactory)->createResponse($response);}//若不是\Symfony\Component\HttpFoundation\SymfonyResponse的对象,则构造成这种对象elseif(!$responseinstanceofSymfonyResponse){$response=newResponse($response);}return$response->prepare($request);}PsrResponseInterface类型publicfunctioncreateResponse(ResponseInterface$psrResponse){$response=newResponse($psrResponse->getBody()->__toString(),$psrResponse->getStatusCode(),$psrResponse->getHeaders());$response->setProtocolVersion($psrResponse->getProtocolVersion());foreach($psrResponse->getHeader('Set-Cookie')as$cookie){$response->headers->setCookie($this->createCookie($cookie));}return$response;}本质就是使用的Response类Response构建publicfunction__construct($content='',$status=200,$headers=array()){$this->headers=newResponseHeaderBag($headers);$this->setContent($content);$this->setStatusCode($status);$this->setProtocolVersion('1.0');//弃用$class=get_class($this);如果($thisinstanceof\PHPUnit_Framework_MockObject_MockObject||$thisinstanceof\Prophecy\Doubler\DoubleInterface){$class=get_parent_class($class);}if(isset(self::$deprecationsTriggered[$class])){返回;}self::$deprecationsTriggered[$class]=true;foreach(self::$deprecatedMethodsas$method){$r=new\ReflectionMethod($class,$method);}if(__CLASS__!==$r->getDeclaringClass()->getName()){@trigger_error(sprintf('Extending%s::%s()in%sisdeprecatedsinceversion3.2andwon\'tbesupported在4.0中不再存在,因为它将是最终版本。',__CLASS__,$method,$class),E_USER_DEPRECATED);}}}publicfunctionsetContent($content){if(null!==$content&&!is_string($content)&&!is_numeric($content)&&!is_callable(array($content,'__toString'))){抛出新的\UnexpectedValueException(sprintf('响应内容必须是一个字符串或对象实现__toString(),"%s"given.',gettype($content)));}$this->content=(string)$content;返回$this;}publicfunctionsetStatusCode($code,$text=null){$this->statusCode=$code=(int)$code;if($this->isInvalid()){thrownew\InvalidArgumentException(sprintf('HTTP状态代码“%s”无效。',$code));}if(null===$text){$this->statusText=isset(self::$statusTexts[$code])?self::$statusTexts[$code]:'未知状态';返回$这个;}if(false===$text){$this->statusText='';返回$这个;}$this->statusText=$text;返回$this;}公共函数tionisInvalid(){返回$this->statusCode<100||$this->statusCode>=600;}publicfunctionsetProtocolVersion($version){$this->version=$version;return$this;}响应准备主要是设置响应头publicfunctionprepare(Request$request){$headers=$this->headers;//信息类if($this->isInformational()||$this->isEmpty()){$this->setContent(null);$headers->remove('内容类型');$headers->remove('内容长度');}else{if(!$headers->has('Content-Type')){$format=$request->getRequestFormat();#static::$formats=array(#'html'=>array('text/html','application/xhtml+xml'),#'txt'=>array('text/plain'),#'js'=>array('application/javascript','application/x-javascript','text/javascript'),#'css'=>array('text/css'),#'json'=>array('应用程序/json','一个应用/x-json'),#'xml'=>array('text/xml','application/xml','application/x-xml'),#'rdf'=>array('application/rdf+xml'),#'atom'=>array('application/atom+xml'),#'rss'=>array('application/rss+xml'),#'form'=>array('application/x-www-form-urlencoded'),#);if(null!==$format&&$mimeType=$request->getMimeType($format)){$headers->set('Content-Type',$mimeType);}}//设置编码格式$charset=$this->charset?:'UTF-8';如果(!$headers->has('Content-Type')){$headers->set('Content-Type','text/html;charset='.$charset);}elseif(0===stripos($headers->get('Content-Type'),'text/')&&false===stripos($headers->get('Content-Type'),'charset')){$headers->set('Content-Type',$headers->get('Content-Type').';字符集='.$字符集);}//如果使用chunked编码传输,则去掉Content-Length,因为chunked编码可以用来知道什么时候传输完成if($headers->has('Transfer-Encoding')){$headers->remove('内容长度');}if($request->isMethod('HEAD')){$length=$headers->get('Content-Length');$this->setContent(null);如果($length){$headers->set('Content-Length',$length);}}}if('HTTP/1.0'!=$request->server->get('SERVER_PROTOCOL')){$this->setProtocolVersion('1.1');}if('1.0'==$this->getProtocolVersion()&&false!==strpos($this->headers->get('Cache-Control'),'no-cache')){$this->headers->set('pragma','no-cache');$this->headers->set('expires',-1);}$this->ensureIEOverSSLCompatibility($request);返回$this;}公共函数isInformational(){返回$this->statusCode>=100&&$this->statusCode<200;}受保护函数ensureIEOverSSLCompatibility(Request$request){if(false!==stripos($this->headers->get('Content-Disposition'),'attachment')&&preg_match('/MSIE(.*?);/i',$request->server->get('HTTP_USER_AGENT'),$match)==1&&true===$request->isSecure()){if((int)preg_replace('/(MSIE)(.*?);/','$2',$match[0])<9){$this->headers->remove('Cache-Control');}}}
