/**
  * Renders a view + optional layout
  *
  * @param string $view  The view to render
  * @param array $data   The data to render in the view
  * @access public
  * @return void
  */
 public function render($view, array $data = array())
 {
     $original_view = $this->view;
     if (!empty($data)) {
         $this->shared_data->merge($data);
     }
     $this->view = $view;
     if (null === $this->layout) {
         $this->yieldView();
     } else {
         require $this->layout;
     }
     if (false !== $this->response->chunked) {
         $this->response->chunk();
     }
     // restore state for parent render()
     $this->view = $original_view;
 }
Example #2
0
 public function invokeWithRequest($action, \Klein\Request $request, \Klein\Response $response)
 {
     $params = $request->params();
     $result = $this->invoke($action, $params);
     $result = $this->_formatResponse($action, $result, $request->format);
     $response->body($result);
 }
Example #3
0
 /**
  * Alias to set a response code, lock the response, and halt the route matching/dispatching
  *
  * @param int $code     Optional HTTP status code to send
  * @throws DispatchHaltedException To halt/skip the current dispatch loop
  * @access public
  * @return void
  */
 public function abort($code = null)
 {
     if (null !== $code) {
         $this->response->code($code);
     }
     // Disallow further response modification
     $this->response->lock();
     throw new DispatchHaltedException();
 }
Example #4
0
 public function testDumpArray()
 {
     $response = new Response();
     $this->assertEmpty($response->body());
     $response->dump(array('sure', 1, 10, 17, 'ok' => 'no'));
     $this->assertNotEmpty($response->body());
     $this->assertNotEquals('<pre></pre>', $response->body());
 }
 public function testRenderChunked()
 {
     $test_data = array('name' => 'trevor suarez', 'title' => 'about', 'verb' => 'woot');
     $response = new Response();
     $response->chunk();
     $this->klein_app->respond(function ($request, $response, $service) use($test_data) {
         // Set some data manually
         $service->sharedData()->set('name', 'should be overwritten');
         // Set our layout
         $service->layout(__DIR__ . '/views/layout.php');
         // Render our view, and pass some MORE data
         $service->render(__DIR__ . '/views/test.php', $test_data);
     });
     $this->klein_app->dispatch(null, $response);
     $this->expectOutputString('<h1>About</h1>' . PHP_EOL . 'My name is Trevor Suarez.' . PHP_EOL . 'WOOT!' . PHP_EOL . '<div>footer</div>' . PHP_EOL);
 }
Example #6
0
 /**
  * Handles an HTTP error exception through our HTTP error callbacks
  *
  * @param HttpExceptionInterface $http_exception    The exception that occurred
  * @param RouteCollection $matched                  The collection of routes that were matched in dispatch
  * @param array $methods_matched                    The HTTP methods that were matched in dispatch
  * @access protected
  * @return void
  */
 protected function httpError(HttpExceptionInterface $http_exception, RouteCollection $matched, $methods_matched)
 {
     if (!$this->response->isLocked()) {
         $this->response->code($http_exception->getCode());
     }
     if (count($this->httpErrorCallbacks) > 0) {
         foreach (array_reverse($this->httpErrorCallbacks) as $callback) {
             if ($callback instanceof Route) {
                 $this->handleRouteCallback($callback, $matched, $methods_matched);
             } elseif (is_callable($callback)) {
                 if (is_string($callback)) {
                     $callback($http_exception->getCode(), $this, $matched, $methods_matched, $http_exception);
                 } else {
                     call_user_func($callback, $http_exception->getCode(), $this, $matched, $methods_matched, $http_exception);
                 }
             }
         }
     }
     // Lock our response, since we probably don't want
     // anything else messing with our error code/body
     $this->response->lock();
 }
Example #7
0
 public function testDump()
 {
     $response = new Response();
     $this->assertEmpty($response->body());
     $response->dump('test');
     $this->assertContains('test', $response->body());
 }
Example #8
-5
 /**
  * Routes an exception through the error callbacks
  *
  * @param Exception $err    The exception that occurred
  * @access protected
  * @return void
  */
 protected function error(Exception $err)
 {
     $type = get_class($err);
     $msg = $err->getMessage();
     if (count($this->errorCallbacks) > 0) {
         foreach (array_reverse($this->errorCallbacks) as $callback) {
             if (is_callable($callback)) {
                 if (is_string($callback)) {
                     if ($callback($this, $msg, $type, $err)) {
                         return;
                     }
                 } else {
                     if (call_user_func($callback, $this, $msg, $type, $err)) {
                         return;
                     }
                 }
             } else {
                 if (null !== $this->service && null !== $this->response) {
                     $this->service->flash($err);
                     $this->response->redirect($callback);
                 }
             }
         }
     } else {
         $this->response->code(500);
         throw new UnhandledException($err);
     }
 }