Beispiel #1
0
 /**
  * Sends the HTTP response back to a HTTP client.
  *
  * This calls php's header() function and streams the body to php://output.
  *
  * @param ResponseInterface $response
  * @return void
  */
 static function sendResponse(ResponseInterface $response)
 {
     header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText());
     foreach ($response->getHeaders() as $key => $value) {
         foreach ($value as $k => $v) {
             if ($k === 0) {
                 header($key . ': ' . $v);
             } else {
                 header($key . ': ' . $v, false);
             }
         }
     }
     $body = $response->getBody();
     if (is_null($body)) {
         return;
     }
     $contentLength = $response->getHeader('Content-Length');
     if ($contentLength !== null) {
         $output = fopen('php://output', 'wb');
         if (is_resource($body) && get_resource_type($body) == 'stream') {
             stream_copy_to_stream($body, $output, $contentLength);
         } else {
             fwrite($output, $body, $contentLength);
         }
     } else {
         file_put_contents('php://output', $body);
     }
     if (is_resource($body)) {
         fclose($body);
     }
 }
 /**
  * index
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  */
 public function indexAction(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $name = $request->getAttribute("name", "Unknown");
     $template = new Template("layout.phtml");
     $response->getBody()->write($template->render(["name" => $name]));
     return $next($request, $response);
 }
Beispiel #3
0
 /**
  * Sends the HTTP response back to a HTTP client.
  *
  * This calls php's header() function and streams the body to php://output.
  *
  * @param ResponseInterface $response
  * @return void
  */
 static function sendResponse(ResponseInterface $response)
 {
     header('HTTP/' . $response->getHttpVersion() . ' ' . $response->getStatus() . ' ' . $response->getStatusText());
     foreach ($response->getHeaders() as $key => $value) {
         header($key . ': ' . $value);
     }
     file_put_contents('php://output', $response->getBody());
 }
Beispiel #4
0
 public function render(ResponseInterface $response, $template, array $data = [])
 {
     if (isset($data['template'])) {
         throw new \InvalidArgumentException("Duplicate template key found");
     }
     if (!is_file($this->templatePath . $template)) {
         throw new \RuntimeException("View cannot render `{$template}` because the template does not exist");
     }
     $render = function ($template, $data) {
         extract($data);
         include $template;
     };
     ob_start();
     $render($this->templatePath . $template, $data);
     $output = ob_get_clean();
     $response->getBody()->write($output);
     return $response;
 }
 public function assertQueryCount($query, $count)
 {
     $html = (string) $this->response->getBody();
     $crawler = new Crawler($html);
     $this->assertEquals($count, $crawler->filter($query)->count());
 }
 public function afterAction(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $response->getBody()->write("after");
     return $response;
 }
Beispiel #7
0
 /**
  * @param ResponseInterface $responseObj
  *
  * @return \stdClass
  *
  * @throws Exception
  */
 public function responseHandler($responseObj)
 {
     $httpResponseCode = $responseObj->getStatusCode();
     if ($httpResponseCode === 200) {
         $data = (string) $responseObj->getBody();
         $jsonResponseData = (array) json_decode($data, true);
         $result = new \stdClass();
         // return response data as json if possible, raw if not
         $result->httpResponseBody = $data && empty($jsonResponseData) ? $data : $jsonResponseData;
     } else {
         throw new \Exception(self::EXCEPTION_GENERIC_HTTP_ERROR . $this->getResponseExceptionMessage($responseObj), $httpResponseCode, $responseObj->getBody());
     }
     $result->httpResponseCode = $httpResponseCode;
     return $result;
 }