/** * Detect if we should use the FeedRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|FeedRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof Model\FeedModel) { // FeedModel found return $this->renderer; } $request = $e->getRequest(); if (!$request instanceof HttpRequest) { // Not an HTTP request; cannot autodetermine return; } $headers = $request->getHeaders(); if (!$headers->has('accept')) { return; } $accept = $headers->get('accept'); if (($match = $accept->match('application/rss+xml, application/atom+xml')) == false) { return; } if ($match->getTypeString() == 'application/rss+xml') { $this->renderer->setFeedType('rss'); return $this->renderer; } if ($match->getTypeString() == 'application/atom+xml') { $this->renderer->setFeedType('atom'); return $this->renderer; } }
/** * Detect if we should use the FeedRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|FeedRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof Model\FeedModel) { // FeedModel found return $this->renderer; } $request = $e->getRequest(); if (!$request instanceof HttpRequest) { // Not an HTTP request; cannot autodetermine return; } $headers = $request->headers(); if ($headers->has('accept')) { $accept = $headers->get('accept'); foreach ($accept->getPrioritized() as $mediaType) { if (0 === strpos($mediaType, 'application/rss+xml')) { // application/rss+xml Accept header found $this->renderer->setFeedType('rss'); return $this->renderer; } if (0 === strpos($mediaType, 'application/atom+xml')) { // application/atom+xml Accept header found $this->renderer->setFeedType('atom'); return $this->renderer; } } } // Not matched! return; }
/** * @param ViewEvent $e The ViewEvent instance * @return void */ public function injectResponse($e) { $renderer = $e->getRenderer(); $response = $e->getResponse(); $result = $e->getResult(); if ($renderer === $this->jsonRenderer) { // JSON Renderer; set content-type header $headers = $response->getHeaders(); $headers->addHeaderLine('content-type', 'application/json'); } elseif ($renderer === $this->feedRenderer) { // Feed Renderer; set content-type header, and export the feed if // necessary $feedType = $this->feedRenderer->getFeedType(); $headers = $response->getHeaders(); $mediatype = 'application/' . ('rss' == $feedType ? 'rss' : 'atom') . '+xml'; $headers->addHeaderLine('content-type', $mediatype); // If the $result is a feed, export it if ($result instanceof Feed) { $result = $result->export($feedType); } } elseif ($renderer !== $this->phpRenderer) { // Not a renderer we support, therefor not our strategy. Return return; } // Inject the content $response->setContent($result); }