/** * request response view * * @param string $property * @return Response|Request|View|Config|null */ function __get($property) { switch ($property) { case 'config': return $this->config = $this->delegate->getConfig(); case 'request': return $this->request = Request::getInstance(); case 'response': return $this->response = Response::getInstance(); case 'view': return $this->view = $this->initView(); } return null; }
/** * @return Response */ function getResponse() { return Response::getInstance(); }
/** * cli模式下的异常处理 * * @param Exception $e * @return string */ function cliErrorHandler(Exception $e) { $trace = $e->getTrace(); $trace_table = array(); if (!empty($trace)) { foreach ($trace as &$t) { if (isset($t['file'])) { $t['file'] = $this->hiddenFileRealPath($t['file']); } foreach ($t as $t_key => $t_info) { switch ($t_key) { case 'file': case 'line': case 'function': $t_info_length = max(strlen($t_key), strlen($t_info)); if (!isset($trace_table[$t_key]) || $t_info_length > $trace_table[$t_key]) { if ($t_info_length % 2 != 0) { $t_info_length += 9; } else { $t_info_length += 8; } $trace_table[$t_key] = $t_info_length; } break; } } } } $result['line'] = $e->getLine(); $result['file'] = $this->hiddenFileRealPath($e->getFile()); $result['trace'] = $trace; $result['trace_table'] = $trace_table; return Response::getInstance()->display($result, __DIR__ . '/_tpl/cli_error.tpl.php'); }
function errorHandler(Exception $e) { $cp_error = $this->cpExceptionSource($e); $code = $e->getCode() ? $e->getCode() : 200; return Response::getInstance()->setResponseStatus($code)->display($cp_error, __DIR__ . '/_tpl/front_error.tpl.php'); }
/** * 输出XML * * @param $data * @param string $root_name */ function XML($data, $root_name = 'root') { Response::getInstance()->setContentType('xml'); $xml = Array2XML::createXML($root_name, $data); echo $xml->saveXML(); }
/** * 输出结果 * * @param Closure $process_closure * @param array $params * @throws CoreException */ private function response(Closure $process_closure, $params) { $ref = new ReflectionFunction($process_closure); if (count($ref->getParameters()) > count($params)) { $need_params = ''; foreach ($ref->getParameters() as $r) { if (!isset($params[$r->name])) { $need_params .= sprintf('$%s ', $r->name); } } throw new CoreException(sprintf('所需参数: %s 未指定', $need_params)); } if (!is_array($params)) { $params = array($params); } $rep = call_user_func_array($process_closure, $params); if (null != $rep) { Response::getInstance()->display($rep); } }
/** * 设置Response * * @param $config */ private function setResponseConfig($config) { if (isset($config['content_type'])) { Response::getInstance()->setContentType($config['content_type']); } if (isset($config['status'])) { Response::getInstance()->setResponseStatus($config['status']); } }
/** * 发送一个包含文件名的下载头 * * @param null $file_name * @param array $add_header * @param bool $only_add_header */ protected function sendDownloadHeader($file_name = null, $add_header = array(), $only_add_header = false) { if (null === $file_name) { $file_name = parent::getController() . '_' . parent::getAction(); } $download_header = array("Pragma: public", "Expires: 0", "Cache-Control:must-revalidate, post-check=0, pre-check=0", "Content-Type: application/force-download", "Content-Type: application/octet-stream", "Content-Type: application/download", "Content-Disposition:attachment;filename={$file_name}", "Content-Transfer-Encoding:binary"); if (!empty($add_header)) { if (true === $only_add_header) { $download_header = $add_header; } else { $download_header = array_merge($download_header, $add_header); } } Response::getInstance()->setHeader($download_header); }