Ejemplo n.º 1
0
 /**
  * Finalize the request and send the response.
  *
  * @param  int   $code
  * @param  array $headers
  * @throws \Pop\Mvc\Exception
  * @return void
  */
 public function send($code = 200, array $headers = null)
 {
     if (null === $this->view) {
         throw new Exception('The view object is not defined.');
     }
     if (!$this->view instanceof View) {
         throw new Exception('The view object is not an instance of Pop\\Mvc\\View.');
     }
     if (null !== $this->project->logger()) {
         $this->project->log("Response [" . $code . "]", time());
     }
     $this->response->setCode($code);
     if (null !== $headers) {
         foreach ($headers as $name => $value) {
             $this->response->setHeader($name, $value);
         }
     }
     // Trigger any dispatch events, then send the response
     if (null !== $this->project->getEventManager()->get('dispatch')) {
         $this->project->log('[Event] Dispatch', time(), \Pop\Log\Logger::NOTICE);
     }
     $this->project->getEventManager()->trigger('dispatch', array('controller' => $this));
     $this->response->setBody($this->view->render(true));
     if (null !== $this->project->getEventManager()->get('dispatch.send')) {
         $this->project->log('[Event] Dispatch Send', time(), \Pop\Log\Logger::NOTICE);
     }
     $this->project->getEventManager()->trigger('dispatch.send', array('controller' => $this));
     $this->response->send();
 }
Ejemplo n.º 2
0
 public function send($code = 200, array $headers = null, $body = null)
 {
     $this->response->setCode($code);
     if (null !== $body) {
         $this->response->setBody($body);
     } else {
         if (null !== $this->view) {
             $this->response->setBody($this->view->render());
         }
     }
     $this->response->send($code, $headers);
 }
Ejemplo n.º 3
0
 /**
  * Send response
  *
  * @param  int    $code
  * @param  array  $headers
  * @param  string $body
  * @return void
  */
 public function send($code = 200, array $headers = null, $body = null)
 {
     $this->response->setCode($code);
     $this->application->trigger('app.send.pre', ['controller' => $this]);
     if (null !== $body) {
         $this->response->setBody($body);
     } else {
         if (null !== $this->view) {
             $this->response->setBody($this->view->render());
         }
     }
     $this->application->trigger('app.send.post', ['controller' => $this]);
     $this->response->send($code, $headers);
 }
Ejemplo n.º 4
0
 public function error()
 {
     $this->response->setBody(json_encode(['error' => 'Resource not found'], JSON_PRETTY_PRINT));
     $this->response->send(404);
 }
Ejemplo n.º 5
0
 /**
  * Error handler
  *
  * @param  \Exception $exception
  * @return void
  */
 public function error(\Exception $exception)
 {
     if ($exception instanceof \Phire\Exception && $exception->isInstallError()) {
         Response::redirect(BASE_PATH . APP_URI . '/install');
         exit;
     }
     // Load assets, if they haven't been loaded already
     $this->loadAssets($_SERVER['DOCUMENT_ROOT'] . APP_PATH . '/data/themes/default', 'default');
     $this->loadAssets(__DIR__ . '/../data/assets', 'phire');
     sort($this->assets['js']);
     sort($this->assets['css']['link']);
     sort($this->assets['css']['import']);
     // Load any custom/override assets
     $this->loadAssets(CONTENT_ABS_PATH . '/phire/assets', 'phire-custom', true);
     $view = new View(__DIR__ . '/../view/phire/exception.phtml');
     $view->title = 'Application Error';
     $view->systemTitle = 'Phire CMS';
     $view->assets = $this->assets;
     $view->phireUri = BASE_PATH . APP_URI;
     $view->basePath = BASE_PATH;
     $view->base_path = BASE_PATH;
     $view->contentPath = BASE_PATH . CONTENT_PATH;
     $view->content_path = BASE_PATH . CONTENT_PATH;
     $view->message = htmlentities(strip_tags($exception->getMessage()), ENT_QUOTES, 'UTF-8');
     $response = new Response();
     $response->setBody((string) $view);
     $response->send();
 }
Ejemplo n.º 6
0
 /**
  * Custom error handler method
  *
  * @param  \Exception $exception
  * @return void
  */
 public function error(\Exception $exception)
 {
     $view = new View(__DIR__ . '/../view/exception.phtml');
     $view->title = 'Application Error';
     $view->message = htmlentities(strip_tags($exception->getMessage()), ENT_QUOTES, 'UTF-8');
     if (file_exists(__DIR__ . '/../config/application.php')) {
         $config = (include __DIR__ . '/../config/application.php');
         $view->application_title = $config['application_title'];
     } else {
         $view->application_title = '';
     }
     $response = new Response();
     $response->setBody((string) $view);
     $response->send(500);
 }
Ejemplo n.º 7
0
 public function testParse()
 {
     $r = Response::parse('http://www.popphp.org/version');
     $r = Response::parse('http://www.popphp.org/version', array('header' => "Accept-language: en\r\n"));
     $this->assertEquals('200', $r->getCode());
     $this->assertEquals('OK', $r->getMessage());
     $this->assertEquals('1.7.0', trim($r->getBody()));
     $this->assertEquals('text/plain', $r->getHeader('Content-Type'));
     $this->assertTrue($r->isSuccessful());
     $this->assertTrue(is_array($r->getHeaders()));
     $this->assertFalse($r->isError());
     $this->assertFalse($r->isRedirect());
     $r = new Response(200, array('Content-Type' => 'text/plain'));
     $r->setBody('This is a test.');
     $response = $r->getHeadersAsString() . PHP_EOL . $r->getBody();
     $r = Response::parse($response);
     $this->assertEquals('200', $r->getCode());
     $this->assertEquals('OK', $r->getMessage());
     $this->assertEquals('This is a test.', trim($r->getBody()));
 }