withFragment() public method

public withFragment ( $fragment )
示例#1
0
 /**
  * Taken from Mink\BrowserKitDriver
  *
  * @param Response $response
  *
  * @return \Symfony\Component\BrowserKit\Response
  */
 protected function createResponse(Psr7Response $response)
 {
     $body = (string) $response->getBody();
     $headers = $response->getHeaders();
     $contentType = null;
     if (isset($headers['Content-Type'])) {
         $contentType = reset($headers['Content-Type']);
     }
     if (!$contentType) {
         $contentType = 'text/html';
     }
     if (strpos($contentType, 'charset=') === false) {
         if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) {
             $contentType .= ';charset=' . $matches[1];
         }
         $headers['Content-Type'] = [$contentType];
     }
     $status = $response->getStatusCode();
     if ($status < 300 || $status >= 400) {
         $matches = [];
         $matchesMeta = preg_match('/\\<meta[^\\>]+http-equiv="refresh" content="(\\d*)\\s*;?\\s*url=(.*?)"/i', $body, $matches);
         if (!$matchesMeta && isset($headers['Refresh'])) {
             // match by header
             preg_match('~(\\d*);?url=(.*)~', (string) reset($headers['Refresh']), $matches);
         }
         if (!empty($matches) && (empty($matches[1]) || $matches[1] < $this->refreshMaxInterval)) {
             $uri = new Psr7Uri($this->getAbsoluteUri($matches[2]));
             $currentUri = new Psr7Uri($this->getHistory()->current()->getUri());
             if ($uri->withFragment('') != $currentUri->withFragment('')) {
                 $status = 302;
                 $headers['Location'] = $matchesMeta ? htmlspecialchars_decode($uri) : (string) $uri;
             }
         }
     }
     return new BrowserKitResponse($body, $status, $headers);
 }