function smarty_block_app($params, $body, &$smarty) { if (empty($body)) { return; } if (!array_key_exists('name', $params)) { Error::info('Tag param error:', 'app标签必须有参数name!'); return; } if (array_key_exists('func', $params)) { $func = $params['func']; unset($params['func']); } else { $func = 'getList'; } $fields = implode(',', SwooleTemplate::get_fields($body)); global $php; $php->createModel($model); if (array_key_exists('titlelen', $params)) { $titlelen = $params['titlelen']; unset($params['titlelen']); $php->model->{$model}->select = str_replace('title', "substring( title, 1, {$titlelen} ) AS title", $fields); } else { $php->model->{$model}->select = $fields; } $data = call_user_func(array($php->model->{$model}, $func), $params); return SwooleTemplate::parse_loop($data, $body, $fields); }
/** * 错误对象 * @param $error 如果为INT,则读取错误信息字典,否则设置错误字符串 * @param $stop 是否终止程序运行(exit) * @return unknown_type */ function __construct($error) { if (is_numeric($error)) { if (empty($this->error_id)) { include LIBPATH . '/data/text/error_code.php'; //错误ID $this->error_id = (int) $error; //错误信息 if (!isset(self::$error_code[$this->error_id])) { $this->error_msg = self::$error_code[$this->error_id]; parent::__construct($this->error_msg, $error); } } } else { $this->error_id = 0; $this->error_msg = $error; parent::__construct($error); } global $php; //如果定义了错误监听程序 if (isset($php->error_call[$this->error_id])) { call_user_func($php->error_call[$this->error_id], $error); } if (self::$stop) { exit(Error::info('Swoole Error', $this->error_msg)); } }
/** * 获取缓存对象 * @param $scheme * @return cache object */ static function create($config) { if (empty(self::$backends[$config['type']])) { return Error::info('Cache Error', "cache backend: {$config['type']} no support"); } $backend = "\\Swoole\\Cache\\" . self::$backends[$config['type']]; return new $backend($config); }
function __construct($params, $backend = 'FileLog') { if (!in_array($backend, $this->backends)) { Error::info('Log backend Error', "Log backend <b>{$backend}</b> not no support!"); } import('#log.' . $backend); $this->backend = new $backend($params); }
/** * 执行一个SQL语句 * @param $sql 执行的SQL语句 */ function query($sql) { parent::real_escape_string($sql); $res = parent::query($sql); if (!$res) { echo Error::info("SQL Error", $this->error . "<hr />{$sql}"); } return new MySQLiRecord($res); }
/** * 获取缓存对象 * @param $scheme * @return cache object */ static function get_cache($config) { if (empty(self::$backends[$config['protocol']])) { return Error::info('Cache Error', "cache backend:{$config['protocol']} no support"); } $backend = self::$backends[$config['protocol']]; import('#cache.' . $backend); return new $backend($config); }
/** * 执行一个SQL语句 * @param $sql 执行的SQL语句 */ function query($sql) { mysql_real_escape_string($sql, $this->conn); $res = mysql_query($sql, $this->conn); if (!$res) { echo Error::info("SQL Error", mysql_error($this->conn) . "<hr />{$sql}"); } return new MySQLRecord($res); }
/** * 执行一个SQL语句 * @param $sql 执行的SQL语句 */ public final function query($sql) { if ($this->debug) { echo "{$sql}<br />\n<hr />"; } parent::quote($sql); $res = parent::query($sql) or Error::info("SQL Error", implode(", ", $this->errorInfo()) . "<hr />{$sql}"); return $res; }
/** * 写入字典内容 * @param $dictname */ static function set($dictname, $dict) { if (!self::$swoole->cache) { Error::info('SiteDict Cache Error', 'Please load Cache!'); } $filename = self::$data_dir . '/' . $dictname . '.php'; self::write($dictname, $dict, $filename); self::delete($dictname); }
/** * 导入插件 * @param $plugin_name * @return unknown_type */ function load($plugin_name, $plugin_param = null) { $path = WEBPATH . '/swoole_plugin/' . $plugin_name . '/Swoole.plugin.php'; if (file_exists($path)) { require_once $path; $this->plugins[$plugin_name] = $swoole_plugin; } else { Error::info('Plugin not Found!', "Plugin file <b>{$path}</b> not exists!"); } }
/** * render template file, then display it. * @param string $tpl_file */ function display($tpl_file = '') { if (empty($tpl_file)) { $tpl_file = $this->swoole->env['mvc']['controller'] . '/' . $this->swoole->env['mvc']['view'] . '.php'; } if (!is_file($this->template_dir . $tpl_file)) { Error::info('template error', "template file[" . $this->template_dir . $tpl_file . "] not found"); } extract($this->tpl_var); include $this->template_dir . $tpl_file; }
function getContent(&$attrs) { $select = new SelectDB($this->db); $select->call_by = 'smarty'; if (isset($attrs['typeid'])) { $attrs['catid'] = $attrs['typeid']; unset($attrs['typeid']); if (is_numeric($attrs['typeid'])) { $cate = $this->swoole->createModel('Category'); $category = $cate->get($attrs['fid']); $select->from(TABLE_PREFIX . '_' . $category['modelname']); } elseif (isset($attrs['app'])) { $app = $this->swoole->createModel('App'); $app_config = $app->getConfig($attrs['app']); $select->from($app_config['tablename']); unset($attrs['app']); } else { Error::info('Tpl Error', 'Nested structure must have `app` param!'); } } elseif (isset($attrs['app'])) { $app = $this->swoole->createModel('App'); $app_config = $app->getConfig($attrs['app']); $select->from($app_config['tablename']); unset($attrs['app']); } else { Error::info('Param Error', 'Get content must have param `typeid` or `app` !'); } if (array_key_exists('select', $attrs)) { $select->select = $attrs['select']; } if (array_key_exists('titlelen', $attrs)) { $titlelen = $attrs['titlelen']; unset($attrs['titlelen']); $select->select = str_replace('title', "substring( title, 1, {$titlelen} ) AS title,title as title_full", $attrs['select']); } $select->limit(isset($attrs['row']) ? $attrs['row'] : 10); unset($attrs['row']); $select->order(isset($attrs['order']) ? $attrs['order'] : 'id desc'); unset($attrs['order']); unset($attrs['get'], $attrs['name'], $attrs['key'], $attrs['func']); $select->put($attrs); if (array_key_exists('page', $attrs)) { $select->paging(); $php->env['start'] = 10 * intval($attrs['page'] / 10); if ($select->pages > 10 and $attrs['page'] < $start) { $php->env['more'] = 1; } $php->env['end'] = $select->pages - $php->env['start']; $php->env['end'] = $select->pages; $php->env['end'] = $select->page_size; $php->env['end'] = $select->num; } return $select->getsql(); }
function run() { if (!empty($_GET['action'])) { $this->action = $_GET['action']; } $this->action = Validate::word($this->action); $method = self::$method_prefix . '_' . $this->action; if (method_exists($this, $method)) { call_user_func(array($this, $method)); } else { Error::info('GeneralView Error!', "View <b>{$this->app_name}->{$method}</b> Not Found!"); } }
public static function connect() { // One connection for the app if (self::$cont === null) { try { self::$cont = new PDO("mysql:host=" . HOST . ";" . "dbname=" . DATABASE, USER, PASSWORD); } catch (PDOException $e) { echo "Error with PDO " . Error::info(); echo $e->message(); die; } } return self::$cont; }
/** * 加载一个模型对象 * @param $model_name 模型名称 * @return $model_object 模型对象 */ static function loadModel($model_name) { if (isset(self::$_objects['model'][$model_name])) { return self::$_objects['model'][$model_name]; } else { $model_file = APPSPATH . '/models/' . $model_name . '.model.php'; if (!file_exists($model_file)) { Error::info('MVC错误', "不存在的模型, <b>{$model_name}</b>"); } require $model_file; self::$_objects['model'][$model_name] = new $model_name(self::$swoole); return self::$_objects['model'][$model_name]; } }
/** * 采用URL rewrite后的URL映射方式 */ function url_process_mvc_rewrite() { $array = array('controller' => 'page', 'view' => 'index', 'segs' => ''); if (empty($_GET['q'])) { return $array; } $request = explode('/', $_GET['q'], 3); if (count($request) !== 3) { header("HTTP/1.1 404 Not Found"); Error::info('URL Error', "HTTP 404!Page Not Found!<p>Error request:<b>{$_SERVER['REQUEST_URI']}</b>"); } $array['controller'] = $request[1]; $array['view'] = $request[2]; return $array; }
/** * 引发一个事件 * @param $event_type 事件类型 * @return NULL */ function raise() { $params = func_get_args(); /** * 同步,直接在引发事件时处理 */ if ($this->mode == 'sync') { if (!isset($this->_handles[$params[0]]) or !function_exists($this->_handles[$params[0]])) { if (empty($handle)) { Error::info('SwooleEvent Error', 'Event handle not found!'); } } return call_user_func_array($this->_handles[$params[0]], array_slice($params, 1)); } else { $this->_queue->put($params); } }
function url_process_mvc() { $array = array('controller' => 'page', 'view' => 'index'); if (!empty($_GET["c"])) { $array['controller'] = $_GET["c"]; } if (!empty($_GET["v"])) { $array['view'] = $_GET["v"]; } if (!empty($_GET['mvc'])) { $request = explode('/', $_GET['mvc'], 3); if (count($request) !== 3) { Error::info('URL Error', "HTTP 404!Page Not Found!<P>Error request:<B>{$_SERVER['REQUEST_URI']}</B>"); } $array['controller'] = $request[1]; $array['view'] = $request[2]; } return $array; }
/** * @param $method * @param $param * @return bool */ protected function _call($method, $param) { if ($method == 'update' or $method == 'delete' or $method == 'insert') { return false; } //调用对应的方法 if (method_exists($this, $method)) { if (is_array($param)) { call_user_func_array(array($this, $method), $param); } else { $this->{$method}($param); } } else { $param = $this->db->quote($param); if ($this->call_by == 'func') { $this->where($method . '="' . $param . '"'); } elseif ($this->call_by == 'smarty') { if (strpos($param, '$') === false) { $this->where($method . "='" . $param . "'"); } else { $this->where($method . "='{" . $param . "}'"); } } else { Error::info('Error: SelectDB 错误的参数', "<pre>参数{$method}={$param}</pre>"); } } return true; }
/** * 执行数据库更新操作,参数为主键ID,值$data,必须是键值对应的 * @param $id 主键ID * @param $data 数据 * @param $table 表名 * @param $where 其他字段 * @return $n SQL语句的返回值 */ public function update($id, $data, $table, $where = 'id') { if (func_num_args() < 3) { echo Error::info('SelectDB param error', 'Update must have 3 paramers ($id,$data,$table) !'); return false; } $this->db_apt->init(); $this->db_apt->from($table); $this->db_apt->where("{$where}='{$id}'"); $this->write_times += 1; return $this->db_apt->update($data); }
/** * 删除键值,并删除数据中的记录 * @param $key * @return true */ function delete($key) { $keys = explode('_', $key, 2); $root = $keys[0]; if (strpos($this->roots, $root) !== false) { $table = $this->db_prefix . '_' . $this->table_name . '_' . $root; } else { Error::info('SwooleKDB Error!', 'Key "' . $root . '" not in kdb_roots!'); } $db_key = $keys[1]; $this->cache->delete($key); $this->db->delete($db_key, $table, 'keyname'); return true; }
/** * 设定类中指定变量名的值,如果改变量不属于这个类,将throw一个exception * * @param string $var * @param string $value */ function set($var, $value) { if (in_array($var, get_object_vars($this))) { $this->{$var} = $value; } else { Error::info(__FUNCTION__, $var . " does not belong to PB_Page!"); } }
private function parseTemplateFile($templateFile) { if ('' == $templateFile) { $templateFile = $this->swoole->env['mvc']['controller'] . '_' . $this->swoole->env['mvc']['view'] . '.html'; } $templateFile = $this->template_dir . $templateFile; if (!file_exists($templateFile)) { Error::info('View Error!', 'Template file not exists! <b>' . $templateFile . '</b>'); } return $templateFile; }
/** * 运行MVC处理模型 * @param $url_processor * @return None */ function runMVC($url_processor) { $url_func = 'url_process_' . $url_processor; if (!function_exists($url_func)) { return Error::info('MVC Error!', "Url Process function not found!<p>\nFunction:{$url_func}"); } $mvc = $url_func(); if (!preg_match('/^[a-z0-9_]+$/i', $mvc['controller'])) { return Error::info('MVC Error!', "controller name incorrect.Regx: /^[a-z0-9_]+\$/i"); } if (!preg_match('/^[a-z0-9_]+$/i', $mvc['view'])) { return Error::info('MVC Error!', "view name incorrect.Regx: /^[a-z0-9_]+\$/i"); } if (isset($mvc['app']) and !preg_match('/^[a-z0-9_]+$/i', $mvc['app'])) { return Error::info('MVC Error!', "app name incorrect.Regx: /^[a-z0-9_]+\$/i"); } $this->env['mvc'] = $mvc; //支持app+controller+view三级映射 if (isset($mvc['app'])) { $controller_path = self::$app_path . "/{$mvc['app']}/controllers/{$mvc['controller']}.php"; } else { $controller_path = self::$app_path . "/controllers/{$mvc['controller']}.php"; } if (!is_file($controller_path)) { header("HTTP/1.1 404 Not Found"); Error::info('MVC Error', "Controller <b>{$mvc['controller']}</b> not exist!"); } else { require_once $controller_path; } if (!class_exists($mvc['controller'])) { Error::info('MVC Error', "Controller Class <b>{$mvc['controller']}</b> not exist!"); } $controller = new $mvc['controller']($this); if (!is_callable(array($controller, $mvc['view']))) { header("HTTP/1.1 404 Not Found"); Error::info('MVC Error!' . $mvc['view'], "View <b>{$mvc['controller']}->{$mvc['view']}</b> Not Found!"); } if (empty($mvc['param'])) { $param = null; } else { $param = $mvc['param']; } $method = $mvc['view']; $return = $controller->{$method}($param); if ($controller->is_ajax) { header('Cache-Control: no-cache, must-revalidate'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Content-type: application/json'); echo json_encode($return); } else { $return; } }
function http_error($code, $response, $content = '') { $response->send_http_status($code); $response->head['Content-Type'] = 'text/html'; $response->body = Error::info(Response::$HTTP_HEADERS[$code], "<p>{$content}</p><hr><address>{$this->config['server']['software']} Server at {$this->server->host} Port {$this->server->port}</address>"); }
/** * 错误信息输出处理 */ function swoole_error_handler($errno, $errstr, $errfile, $errline) { $level = 'Error'; $info = ''; switch ($errno) { case E_USER_ERROR: $level = 'User Error'; break; case E_USER_WARNING: $level = 'Warnning'; break; case E_USER_NOTICE: $level = 'Notice'; break; default: return; } $title = 'Swoole ' . $level; $info .= '<b>File:</b> ' . $errfile . "<br />\n"; $info .= '<b>Line:</b> ' . $errline . "<br />\n"; $info .= '<b>Info:</b> ' . $errstr . "<br />\n"; $info .= '<b>Code:</b> ' . $errno . "<br />\n"; echo Error::info($title, $info); }
<?php require LIBPATH . '/system/SwooleEvent.php'; if (!defined('EVENT_MODE') or EVENT_MODE == 'sync') { $event = new SwooleEvent('sync'); } else { $queue_url = ''; $queue_type = 'CacheQueue'; if (defined('EVENT_QUEUE')) { $queue_url = EVENT_QUEUE; } if (defined('EVENT_QUEUE_TYPE')) { $queue_type = EVENT_QUEUE_TYPE; } $event = new SwooleEvent('async', $queue_url, $queue_type); } if (defined('EVENT_HANDLE')) { require EVENT_HANDLE; if (empty($handle)) { Error::info('SwooleEvent Error', 'Event handles not be empty!'); } $event->set_listens($handle); }