/** * Test Messages with empty container */ public function testMessagesEmpty() { $this->assertEquals(0, Proxy\Messages::count()); $this->assertNull(Proxy\Messages::pop(Messages::TYPE_ERROR)); $this->assertNull(Proxy\Messages::pop(Messages::TYPE_NOTICE)); $this->assertNull(Proxy\Messages::pop(Messages::TYPE_SUCCESS)); }
/** * Response as Json */ public function process() { // override response code so javascript can process it $this->response->setHeader('Content-Type', 'application/json'); // setup messages if (Messages::count()) { $this->response->setHeader('Bluz-Notify', json_encode(Messages::popAll())); } // prepare body if ($body = $this->response->getBody()) { // convert to JSON $body = json_encode($body); // setup content length $this->response->setHeader('Content-Length', strlen($body)); // prepare to JSON output $this->response->setBody($body); } }
/** * send * * @throws NotAcceptableException */ public function send() { $body = $this->getBody(); $this->sendCookies(); switch (true) { case 'CLI' == $this->type: // CLI response $response = new CliResponse($body->render('CLI'), $this->getStatusCode()); break; case is_null($body): case 204 == $this->getStatusCode(): $response = new EmptyResponse($this->getStatusCode(), $this->getHeaders()); break; case 301 == $this->getStatusCode(): case 302 == $this->getStatusCode(): $response = new RedirectResponse($this->getHeader('Location'), $this->getStatusCode(), $this->getHeaders()); break; case 'JSON' == $this->type: // JSON response // setup messages if (Messages::count()) { $this->setHeader('Bluz-Notify', json_encode(Messages::popAll())); } // encode body data to JSON $response = new JsonResponse($body, $this->getStatusCode(), $this->getHeaders()); break; case 'HTML' == $this->type: default: // HTML response $response = new HtmlResponse((string) $body, $this->getStatusCode(), $this->getHeaders()); break; } $emitter = new SapiEmitter(); $emitter->emit($response); }