/** * Determine whether or not the type can format a given response. * * @param Response $response the response to check * * @return bool true if the formatter can format the response, otherwise false. */ public function canFormat(Response $response) { $request = $response->getRequest(); if ($this->matchFileExtension($request)) { return true; } else { if ($this->matchAcceptTypes($request)) { return true; } else { return false; } } }
/** * MimeType the given request response * * @param Response $response the response to format * * @return string the formatted response data */ public function format(Response $response) { $request = $response->getRequest(); $controller = $request->getAction()->getController(); $params = array('response' => $this, 'format' => \Yii::app()->getComponent('format')); if ($response->data instanceof \CModel) { $params['model'] = $response->data; } else { if ($response->data instanceof \CDataProvider) { $params['dataProvider'] = $response->data; } else { if ($response->data instanceof \Exception) { $params['error'] = $response->data; } else { if (is_array($response->data) && count($response->data) && !isset($response->data[0])) { $params = \CMap::mergeArray($params, $response->data); } else { $params['data'] = $response->data; } } } } try { if ($request->getIsPjaxRequest()) { if (isset($controller->pjaxLayout)) { $controller->layout = $controller->pjaxLayout; } else { $controller->layout = '//layouts/pjax'; } return $controller->render($response->getViewName(), $params, true); } else { if ($request->getIsAjaxRequest()) { return $controller->renderPartial($response->getViewName(), $params, true); } else { return $controller->render($response->getViewName(), $params, true); } } } catch (\Exception $e) { echo '<pre>' . print_r($e, true) . '</pre>'; return null; } }
/** * MimeType the given request response * * @param Response $response the response to format * * @return string the formatted response data */ public function format(Response $response) { $prepared = $this->prepare($response->data); $callback = $this->getRequestCallbackName($response->getRequest()); return $callback . '(' . json_encode($prepared, $response->prettyPrint ? JSON_PRETTY_PRINT : 0) . ')'; }