/** * @param \Zend\View\ViewEvent $e * @return \BedRest\Framework\Zend2\View\Renderer\ContentNegotiationRenderer */ public function selectRenderer(ViewEvent $e) { if (!$e->getModel() instanceof ViewModel) { return; } return $this->renderer; }
/** * Inject the response * * Injects the response with the rendered content, and sets the content * type based on the detection that occurred during renderer selection. * * @param ViewEvent $e */ 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; } $model = $e->getModel(); $contentType = $this->contentType; $response = $e->getResponse(); if ($model instanceof ApiProblemModel) { $contentType = 'application/problem+json'; } elseif ($model instanceof HalJsonModel && ($model->isCollection() || $model->isEntity())) { $contentType = 'application/hal+json'; } /** @var Response $response */ $response->setContent($result); $headers = $response->getHeaders(); $headers->addHeaderLine('content-type', $contentType); }
/** * Inject the response * * Injects the response with the rendered content, and sets the content * type based on the detection that occurred during renderer selection. * * @param ViewEvent $e */ 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; } $model = $e->getModel(); $contentType = $this->contentType; $response = $e->getResponse(); if ($this->renderer->isApiProblem()) { $contentType = 'application/api-problem+json'; $statusCode = $this->getStatusCodeFromApiProblem($this->renderer->getApiProblem()); $response->setStatusCode($statusCode); } elseif ($model instanceof RestfulJsonModel && $model->isApiProblem()) { $contentType = 'application/api-problem+json'; $statusCode = $this->getStatusCodeFromApiProblem($model->getPayload()); $response->setStatusCode($statusCode); } elseif ($model instanceof RestfulJsonModel && ($model->isHalCollection() || $model->isHalResource())) { $contentType = 'application/hal+json'; } // Populate response $response->setContent($result); $headers = $response->getHeaders(); $headers->addHeaderLine('content-type', $contentType); }
/** * 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 * * @return null * * @throws \Zend\Config\Exception\RuntimeException * @throws \Zend\Http\Exception\InvalidArgumentException */ public function injectResponse(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof ApiProblemModel) { // Model is not an ApiProblemModel; we cannot handle it here return null; } /** @var Request $request */ $request = $e->getRequest(); /** @var Accept $accept */ $accept = $request->getHeader('Accept'); if (!($accept instanceof Accept && $accept->hasMediaType('text/xml'))) { return null; } $problem = $model->getApiProblem(); $statusCode = $this->getStatusCodeFromApiProblem($problem); $contentType = 'text/xml'; /** @var Response $response */ $response = $e->getResponse(); $problemData = $problem->toArray(); $xmlWriter = new XmlWriter(); $output = $xmlWriter->processConfig($problemData); $response->setStatusCode($statusCode); $response->setContent($output); $headers = $response->getHeaders(); $headers->addHeaderLine('Content-Type', $contentType); }
public function selectRenderer(ViewEvent $e) { if (!$this->renderer->canRender($e->getModel())) { return false; } return $this->renderer; }
/** * Detect if we should use the JsonRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof Model\JsonModel) { // JsonModel 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/json, application/javascript')) == false) { return; } if ($match->getTypeString() == 'application/json') { // application/json Accept header found return $this->renderer; } if ($match->getTypeString() == 'application/javascript') { // application/javascript Accept header found if (false != ($callback = $request->getQuery()->get('callback'))) { $this->renderer->setJsonpCallback($callback); } return $this->renderer; } }
/** * Inject the response with the feed 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) && !$result instanceof Feed) { // We don't have a string, and thus, no feed return; } // If the result is a feed, export it if ($result instanceof Feed) { $result = $result->export($renderer->getFeedType()); } // Get the content-type header based on feed type $feedType = $renderer->getFeedType(); $feedType = 'rss' == $feedType ? 'application/rss+xml' : 'application/atom+xml'; $model = $e->getModel(); $charset = ''; if ($model instanceof Model\FeedModel) { $feed = $model->getFeed(); $charset = '; charset=' . $feed->getEncoding() . ';'; } // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); $headers->addHeaderLine('content-type', $feedType . $charset); }
/** * Detect if we should use the JsonRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof Model\JsonModel) { // JsonModel 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')) { $accept = $headers->get('Accept'); foreach ($accept->getPrioritized() as $mediaType) { if (0 === strpos($mediaType, 'application/json')) { // application/json Accept header found return $this->renderer; } if (0 === strpos($mediaType, 'application/javascript')) { // application/javascript Accept header found if (false != ($callback = $request->getQuery()->get('callback'))) { $this->renderer->setJsonpCallback($callback); } return $this->renderer; } } } // Not matched! return; }
/** * Detect if we should use the NewsletterRenderer based on model type * * @param ViewEvent $e * @return null|NewsletterRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof NewsletterModel) { return $this->renderer; } return false; }
/** * Determine if the renderer can load the requested template. * * @param ViewEvent $e * @return bool|LatteRenderer */ public function selectRenderer(ViewEvent $e) { $tplPath = $this->resolver->resolve($e->getModel()->getTemplate(), $this->renderer); if ($tplPath) { return $this->renderer; } return false; }
public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof YamlModel) { return $this->renderer; } return null; }
/** * * @param ViewEvent $e * @return RendererInterface|null */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof BlockInterface) { return; } return $this->renderer; }
/** * @param ViewEvent $ev * @return SmartyRenderer */ public function selectRenderer(ViewEvent $ev) { if ($this->renderer->canRender($ev->getModel())) { return $this->renderer; } else { return null; } }
/** * Detect if we should use the IcalendarRenderer based on model type and/or * Accept header * * @param \Zend\View\ViewEvent $oEvent * * @return null|\Phpug\Mvc\Renderer\IcalendarRenderer */ public function selectRenderer(\Zend\View\ViewEvent $oEvent) { $oModel = $oEvent->getModel(); // No IcalendarModel; do nothing if (!$oModel instanceof IcalendarModel) { return; } return $this->renderer; }
/** * Returns the YamlRenderer if current model is a YamlModel * @param ViewEvent $e * @return YamlRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof YamlModel) { return; } // JsonModel found return $this->renderer; }
/** * Detect if we should use the UploaderRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null | TextAreaRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof UploaderModelInterface) { return null; } // JsonModel found return $this->renderer; }
/** * Detect if we should use the FeedRenderer based on model type and/or * Accept header * * @param ViewEvent $e * * @return null|WorkflowDescriptorApiRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!($model instanceof WorkflowDescriptorApiModel || $model instanceof HalEntity)) { return null; } $this->renderer->setViewEvent($e); return $this->renderer; }
/** * Detect if we should use the ResourceJsonRenderer based on model type * * @param ViewEvent $e * @return null|ResourceJsonRenderer|\Zend\View\Renderer\JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof ResourceJsonModel) { // Not a Resource Json Model return; } return $this->renderer; }
/** * @param ViewEvent $e * @return null|JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof ViewModel) { return; } $this->model = $model; return $this->renderer; }
/** * @param ViewEvent $e * @return null|JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof ApiBlueprintModel) { return; } $this->renderer->setRequestUri($e->getRequest()->getUri()); $this->model = $model; return $this->renderer; }
/** * Detect if we should use the UploaderRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null | RendererInterface */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof UploaderModelInterface) { return null; } $this->renderer = $this->rendererFactory->createRenderer($e->getRequest()->getQuery()); // JsonModel found return $this->renderer; }
/** * Sets the response based on image returned by the renderer * * @param ViewEvent $e * @return void */ public function injectResponse(ViewEvent $e) { $model = $e->getModel(); if ($model instanceof ImageModel) { $result = $e->getResult(); $response = $e->getResponse(); $response->setContent($result); $response->getHeaders()->addHeaderLine('Content-type', $this->getMimeType($model->getFormat())); } }
/** * Detect if we should use the XmlRenderer based on model type * * @param ViewEvent $e * @return null|XmlRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof XmlModel) { // no XmlModel; do nothing return null; } // XmlModel found return $this->renderer; }
/** * Detect if we should use the JsonRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof Model\JsonModel) { // no JsonModel; do nothing return; } // JsonModel found return $this->renderer; }
/** * Determine if the renderer can load the requested template. * * @param ViewEvent $e * @return bool|TwigRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof ViewModel) { return false; } if ($this->renderer->canRender($model->getTemplate())) { return $this->renderer; } return false; }
public function renderer(ViewEvent $event) { $model = $event->getModel(); if ($model instanceof IcalendarViewModel) { $renderer = new IcalendarRenderer(); if (null !== ($ignoreTimezone = $model->getOption('ignoreTimezone'))) { $renderer->ignoreTimezone($ignoreTimezone); } return $renderer; } }
/** * Detect if we should use the JsonRenderer based on model type and/or * Accept header * * @param ViewEvent $e * @return null|JsonRenderer */ public function selectRenderer(ViewEvent $e) { $model = $e->getModel(); $name = $model->getTemplate(); if (array_key_exists($name, $this->layouts)) { $name = $this->layouts[$name]; } $last4 = substr($name, strlen($name) - 4); if ($last4 == '.tpl') { return $this->renderer; } return null; }
public function injectResponse(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof \Stjornvisi\View\Model\IcalModel) { // no JsonModel; do nothing return; } $result = $e->getResult(); // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); $headers->addHeaderLine('content-type', 'text/calendar; charset=utf-8'); }
public function injectResponse(ViewEvent $e) { $model = $e->getModel(); if (!$model instanceof \Stjornvisi\View\Model\CsvModel) { // no JsonModel; do nothing return; } $csv = $model->getData(); $result = $e->getResult(); // Populate response $response = $e->getResponse(); $response->setContent($result); $headers = $response->getHeaders(); $headers->addHeaderLine('content-type', 'text/csv; charset=utf-8')->addHeaderLine('Content-Disposition', sprintf("attachment; filename=\"%s\"", $csv->getName())); }