/** * Inject the response with the JSON payload and appropriate Content-Type header * * @param ViewEvent $e * @return void */ public function injectResponse(ViewEvent $e) { $renderer = $e->getRenderer(); if ($renderer !== $this->renderer) { // Discovered renderer is not ours; do nothing return; } $result = $e->getResult(); if (!is_string($result)) { // We don't have a string, and thus, no JSON return; } // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); if ($this->renderer->hasJsonpCallback()) { $contentType = 'application/javascript'; } else { $contentType = 'application/json'; } $contentType .= '; charset=' . $this->charset; $headers->addHeaderLine('content-type', $contentType); if (in_array(strtoupper($this->charset), $this->multibyteCharsets)) { $headers->addHeaderLine('content-transfer-encoding', 'BINARY'); } }
public function testSetHasJsonpCallback() { $this->assertFalse($this->renderer->hasJsonpCallback()); $this->renderer->setJsonpCallback(0); $this->assertFalse($this->renderer->hasJsonpCallback()); $this->renderer->setJsonpCallback('callback'); $this->assertTrue($this->renderer->hasJsonpCallback()); }
/** * Inject the response with the JSON payload and appropriate Content-Type header * * @param ViewEvent $e * @return void */ public function injectResponse(ViewEvent $e) { $renderer = $e->getRenderer(); if ($renderer !== $this->renderer) { // Discovered renderer is not ours; do nothing return; } $result = $e->getResult(); if (!is_string($result)) { // We don't have a string, and thus, no JSON return; } // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); if ($this->renderer->hasJsonpCallback()) { $headers->addHeaderLine('content-type', 'application/javascript'); } else { $headers->addHeaderLine('content-type', 'application/json'); } }