Пример #1
0
 protected function debugResponse(ResponseInterface $response)
 {
     $this->printDebug(AbstractMessage::getStartLineAndHeaders($response));
     $body = (string) $response->getBody();
     $contentType = $response->getHeader('Content-Type');
     if ($contentType == 'application/json' || strpos($contentType, '+json') !== false) {
         $data = json_decode($body);
         if ($data === null) {
             // invalid JSON!
             $this->printDebug($body);
         } else {
             // valid JSON, print it pretty
             $this->printDebug(json_encode($data, JSON_PRETTY_PRINT));
         }
     } else {
         // the response is HTML - see if we should print all of it or some of it
         $isValidHtml = strpos($body, '</body>') !== false;
         if ($isValidHtml) {
             $this->printDebug('');
             $crawler = new Crawler($body);
             // very specific to Symfony's error page
             $isError = $crawler->filter('#traces-0')->count() > 0 || strpos($body, 'looks like something went wrong') !== false;
             if ($isError) {
                 $this->printDebug('There was an Error!!!!');
                 $this->printDebug('');
             } else {
                 $this->printDebug('HTML Summary (h1 and h2):');
             }
             // finds the h1 and h2 tags and prints them only
             foreach ($crawler->filter('h1, h2')->extract(array('_text')) as $header) {
                 // avoid these meaningless headers
                 if (strpos($header, 'Stack Trace') !== false) {
                     continue;
                 }
                 if (strpos($header, 'Logs') !== false) {
                     continue;
                 }
                 // remove line breaks so the message looks nice
                 $header = str_replace("\n", ' ', trim($header));
                 // trim any excess whitespace "foo   bar" => "foo bar"
                 $header = preg_replace('/(\\s)+/', ' ', $header);
                 if ($isError) {
                     $this->printErrorBlock($header);
                 } else {
                     $this->printDebug($header);
                 }
             }
             $profilerUrl = $response->getHeader('X-Debug-Token-Link');
             if ($profilerUrl) {
                 $fullProfilerUrl = $response->getHeader('Host') . $profilerUrl;
                 $this->printDebug('');
                 $this->printDebug(sprintf('Profiler URL: <comment>%s</comment>', $fullProfilerUrl));
             }
             // an extra line for spacing
             $this->printDebug('');
         } else {
             $this->printDebug($body);
         }
     }
 }