Native PHP syntax to render the page while providing widget functionality
- /**
- * Get and Set configuration parameters support Batch Definition
- * If $key is an associative array, the configuration is written as K-v
- * If $key is a numeric index array, the corresponding configuration array is returned
- * @param string|array $key Configuration variables
- * @param array|null $value configuration values
- * @return Array|null
- */
- function C ($key, $value =null) {
- Static $_config = Array ();
- $args = Func_num_args ();
- if ($args = = 1) {
- if (is_string ($key)) {//If the key passed in is a string
- return Isset ($_config[$key])? $_config[$key]:null;
- }
- if (Is_array ($key)) {
- if (Array_keys ($key)!== range (0, COUNT ($key)-1)) {//If the key passed in is an associative array
- $_config = Array_merge ($_config, $key);
- }else{
- $ret = Array ();
- foreach ($key as $k) {
- $ret [$k] = Isset ($_config[$k])? $_config[$k]:null;
- }
- return $ret;
- }
- }
- }else{
- if (is_string ($key)) {
- $_config[$key] = $value;
- }else{
- Halt (' Incoming parameter is incorrect ');
- }
- }
- return null;
- }
- /**
- * Call Widget
- * @param string $name widget name
- * @param array $data The list of variables passed to the widget, key is the variable name, value is the variable
- * @return void
- */
- function W ($name, $data = Array ()) {
- $fullName = $name. ' Widgets ';
- if (!class_exists ($fullName)) {
- Halt (' Widgets '. $name. ' does not exist ');
- }
- $widget = new $fullName ();
- $widget->invoke ($data);
- }
- /**
- * Terminate program Run
- * @param string $str termination reason
- * @param BOOL $display Whether the call stack is displayed and not displayed by default
- * @return void
- */
- function Halt ($STR, $display =false) {
- Log::fatal ($str. ' Debug_backtrace: '. Var_export (Debug_backtrace (), true));
- Header ("content-type:text/html; Charset=utf-8 ");
- if ($display) {
- echo "
"; - Debug_print_backtrace ();
- echo "
";
- }
- Echo $str;
- Exit
- }
- /**
- * Get DB instance
- * @return DB
- */
- function M () {
- $dbConf = C (Array (' Db_host ', ' db_port ', ' db_user ', ' db_pwd ', ' db_name ', ' db_charset '));
- Return db::getinstance ($dbConf);
- }
- /**
- * If the file exists, include in
- * @param string $path file path
- * @return void
- */
- function Includeifexist ($path) {
- if (file_exists ($path)) {
- Include $path;
- }
- }
- /**
- * General Control class
- */
- Class Singlephp {
- /**
- * Controller
- * @var String
- */
- Private $c;
- /**
- * Action
- * @var String
- */
- Private $a;
- /**
- * Single case
- * @var singlephp
- */
- private static $_instance;
- /**
- * Constructor, initialize configuration
- * @param array $conf
- */
- Private function __construct ($conf) {
- C ($conf);
- }
- Private Function __clone () {}
- /**
- * Get a single case
- * @param array $conf
- * @return singlephp
- */
- public static function getinstance ($conf) {
- if (! ( Self::$_instance instanceof Self)) {
- Self::$_instance = new self ($conf);
- }
- return self::$_instance;
- }
- /**
- * Run an Application instance
- * @access Public
- * @return void
- */
- Public Function run () {
- if (C (' use_session ') = = True) {
- Session_Start ();
- }
- C (' App_full_path ', GETCWD (). ' /'. C (' App_path '). ' /');
- Includeifexist (C (' App_full_path '). ' /common.php ');
- $pathMod = C (' Path_mod ');
- $pathMod = Empty ($pathMod)? ' NORMAL ': $pathMod;
- Spl_autoload_register (Array (' singlephp ', ' autoload '));
- if (strcmp (Strtoupper ($pathMod), ' NORMAL ') = = = 0 | |!isset ($_server[' path_info ')) {
- $this->c = isset ($_get[' C ') "$_get[' C ']: ' Index ';
- $this->a = isset ($_get[' a ') "$_get[' A ']: ' Index ';
- }else{
- $pathInfo = isset ($_server[' path_info ') "$_server[' Path_info ']: ';
- $PATHINFOARR = explode ('/', trim ($pathInfo, '/'));
- if (isset ($pathInfoArr [0]) && $pathInfoArr [0]!== ') {
- $this->c = $pathInfoArr [0];
- }else{
- $this->c = ' Index ';
- }
- if (Isset ($PATHINFOARR [1])) {
- $this->a = $PATHINFOARR [1];
- }else{
- $this->a = ' Index ';
- }
- }
- if (!class_exists ($this->c. ' Controller ')) {
- Halt (' controller '. $this->c. ' Not present ');
- }
- $controllerClass = $this->c. ' Controller ';
- $controller = new $controllerClass ();
- if (!method_exists ($controller, $this->a. ' Action ')) {
- Halt (' method '. $this->a. ' Not present ');
- }
- Call_user_func (Array ($controller, $this->a. ' Action '));
- }
- /**
- * Auto Load function
- * @param string $class class name
- */
- public static function AutoLoad ($class) {
- if (substr ($class, -10) = = ' Controller ') {
- Includeifexist (C (' App_full_path '). ' /controller/'. $class. Class.php ');
- }elseif (substr ($class, -6) = = ' Widget ') {
- Includeifexist (C (' App_full_path '). ' /widget/'. $class. Class.php ');
- }else{
- Includeifexist (C (' App_full_path '). ' /lib/'. $class. Class.php ');
- }
- }
- }
- /**
- * Controller class
- */
- Class Controller {
- /**
- * View Instance
- * @var View
- */
- Private $_view;
- /**
- * constructor, initialize view instance, call Hook
- */
- Public Function __construct () {
- $this->_view = new View ();
- $this->_init ();
- }
- /**
- * Front Hook
- */
- protected function _init () {}
- /**
- * Render template and output
- * @param null|string $tpl template file path
- * The parameter is relative to the app/view/file and does not contain a suffix name, such as Index/index
- * If the parameter is empty, the $controller/$action is used by default. php
- * If the parameter does not contain "/", the $controller/is used by default $tpl
- * @return void
- */
- protected function display ($TPL = ") {
- if ($tpl = = =) {
- $trace = Debug_backtrace ();
- $controller = substr ($trace [1][' class '], 0,-10);
- $action = substr ($trace [1][' function '], 0,-6);
- $TPL = $controller. '/' . $action;
- }elseif (Strpos ($TPL, '/') = = = = False) {
- $trace = Debug_backtrace ();
- $controller = substr ($trace [1][' class '], 0,-10);
- $TPL = $controller. '/' . $TPL;
- }
- $this->_view->display ($TPL);
- }
- /**
- * Set a template variable for the view engine
- * @param string $name The name of the variable to be used in the template
- * @param the value corresponding to the variable name in the mixed $value template
- * @return void
- */
- protected function Assign ($name, $value) {
- $this->_view->assign ($name, $value);
- }
- /**
- * Output data in JSON format to the browser and stop code execution
- * @param array $data The data to be output
- */
- protected function Ajaxreturn ($data) {
- echo Json_encode ($data);
- Exit
- }
- /**
- * Redirect to the specified URL
- * @param string $url the URL to jump
- * @param void
- */
- protected function Redirect ($url) {
- Header ("Location: $url");
- Exit
- }
- }
- /**
- * View class
- */
- Class View {
- /**
- * View File directory
- * @var String
- */
- Private $_tpldir;
- /**
- * View File path
- * @var String
- */
- Private $_viewpath;
- /**
- * View variable List
- * @var Array
- */
- Private $_data = Array ();
- /**
- * List of variables to be used for tplinclude
- * @var Array
- */
- private static $tmpData;
- /**
- * @param string $tplDir
- */
- Public function __construct ($tplDir = ") {
- if ($tplDir = = ") {
- $this->_tpldir = './'. C (' App_path '). ' /view/';
- }else{
- $this->_tpldir = $tplDir;
- }
- }
- /**
- * Set a template variable for the view engine
- * @param string $key The name of the variable to be used in the template
- * @param the value corresponding to the variable name in the mixed $value template
- * @return void
- */
- Public function assign ($key, $value) {
- $this->_data[$key] = $value;
- }
- /**
- * Render template and output
- * @param null|string $tplFile template file path, relative to the app/view/file, does not contain a suffix name, such as Index/index
- * @return void
- */
- Public function display ($tplFile) {
- $this->_viewpath = $this->_tpldir. $tplFile. '. php ';
- Unset ($tplFile);
- Extract ($this->_data);
- Include $this->_viewpath;
- }
- /**
- * Used to include other templates in the template file
- * @param string $path Path relative to the view directory
- * @param array $data The list of variables passed to the child template, key is the variable name, value is the variable
- * @return void
- */
- public static function Tplinclude ($path, $data =array ()) {
- Self:: $tmpData = Array (
- ' path ' = = C (' App_full_path '). '/view/'. $path. '. php ',
- ' Data ' = $data,
- );
- Unset ($path);
- Unset ($data);
- Extract (self:: $tmpData [' data ']);
- Include self:: $tmpData [' path '];
- }
- }
- /**
- * Widget class
- * Inherit this class when used, override the Invoke method, and invoke the display in the Invoke method
- */
- Class Widget {
- /**
- * View Instance
- * @var View
- */
- protected $_view;
- /**
- * Widget Name
- * @var String
- */
- protected $_widgetname;
- /**
- * constructor, Initialize view instance
- */
- Public Function __construct () {
- $this->_widgetname = Get_class ($this);
- $dir = C (' App_full_path '). '/widget/tpl/';
- $this->_view = new View ($DIR);
- }
- /**
- * Processing Logic
- * @param mixed $data parameters
- */
- Public function Invoke ($data) {}
- /**
- * Render Template
- * @param string $tpl template path, if blank, use class name as template
- */
- protected function display ($TPL = ") {
- if ($tpl = = ") {
- $TPL = $this->_widgetname;
- }
- $this->_view->display ($TPL);
- }
- /**
- * Set a template variable for the view engine
- * @param string $name The name of the variable to be used in the template
- * @param the value corresponding to the variable name in the mixed $value template
- * @return void
- */
- protected function Assign ($name, $value) {
- $this->_view->assign ($name, $value);
- }
- }
- /**
- * Database Operation class
- * How to use:
- * Db::getinstance ($conf)->query (' SELECT * from table ');
- * Where $conf is an associative array, it needs to contain the following key:
- * Db_host Db_user db_pwd db_name
- * Ports and encodings can be specified using Db_port and Db_charset, default 3306 and UTF8
- */
- Class DB {
- /**
- * Database Link
- * @var Resource
- */
- Private $_db;
- /**
- * Save Last SQL
- * @var String
- */
- Private $_lastsql;
- /**
- * The number of rows affected by the last SQL statement
- * @var int
- */
- Private $_rows;
- /**
- * Last SQL Execution error
- * @var String
- */
- Private $_error;
- /**
- * Instance Array
- * @var Array
- */
- private static $_instance = Array ();
- /**
- * Constructor function
- * @param array $dbConf configuration
- */
- Private function __construct ($dbConf) {
- if (!isset ($dbConf [' Db_charset '])) {
- $dbConf [' db_charset '] = ' UTF8 ';
- }
- $this->_db = mysql_connect ($dbConf [' db_host ']. ': ' $dbConf [' Db_port '], $dbConf [' Db_user '], $dbConf [' db_pwd ']);
- if ($this->_db = = = False) {
- Halt (Mysql_error ());
- }
- $selectDb = mysql_select_db ($dbConf [' db_name '], $this->_db);
- if ($selectDb = = = False) {
- Halt (Mysql_error ());
- }
- Mysql_set_charset ($dbConf [' db_charset ']);
- }
- Private Function __clone () {}
- /**
- * Get DB Class
- * @param array $dbConf configuration
- * @return DB
- */
- static public Function getinstance ($dbConf) {
- if (!isset ($dbConf [' Db_port '])) {
- $dbConf [' db_port '] = ' 3306 ';
- }
- $key = $dbConf [' db_host ']. ': ' $dbConf [' db_port '];
- if (!isset (self::$_instance[$key]) | |! ( self::$_instance[$key] instanceof self)) {
- self::$_instance[$key] = new self ($dbConf);
- }
- return self::$_instance[$key];
- }
- /**
- * Escape string
- * @param string $str to be escaped
- * Strings escaped @return string
- */
- Public function Escape ($STR) {
- Return mysql_real_escape_string ($STR, $this->_db);
- }
- /**
- * Query, for SELECT statements
- * @param string $sql sql to query
- * @return Bool|array query successfully returned the corresponding array, failed to return false
- */
- Public Function Query ($sql) {
- $this->_rows = 0;
- $this->_error = ";
- $this->_lastsql = $sql;
- $this->logsql ();
- $res = mysql_query ($sql, $this->_db);
- if ($res = = = False) {
- $this->_error = mysql_error ($this->_db);
- $this->logerror ();
- return false;
- }else{
- $this->_rows = mysql_num_rows ($res);
- $result = Array ();
- if ($this->_rows >0) {
- while ($row = Mysql_fetch_array ($res, Mysql_assoc)) {
- $result [] = $row;
- }
- Mysql_data_seek ($res, 0);
- }
- return $result;
- }
- }
- /**
- * Query, for INSERT/UPDATE/DELETE statements
- * @param string $sql sql to query
- * @return Bool|int Query successfully returns the number of records affected, failure returns false
- */
- Public function Execute ($sql) {
- $this->_rows = 0;
- $this->_error = ";
- $this->_lastsql = $sql;
- $this->logsql ();
- $result = mysql_query ($sql, $this->_db);
- if (false = = = $result) {
- $this->_error = mysql_error ($this->_db);
- $this->logerror ();
- return false;
- } else {
- $this->_rows = mysql_affected_rows ($this->_db);
- return $this->_rows;
- }
- }
- /**
- * Get the number of records affected by the last query
- * Number of records affected by @return int
- */
- Public Function getRows () {
- return $this->_rows;
- }
- /**
- * Gets the self-increment ID generated after the last insert
- * @return int self-increment ID
- */
- Public Function Getinsertid () {
- Return mysql_insert_id ($this->_db);
- }
- /**
- * Get SQL for the last query
- * @return String sql
- */
- Public Function Getlastsql () {
- return $this->_lastsql;
- }
- /**
- * Get error information for the last query
- * @return String Error message
- */
- Public Function GetError () {
- return $this->_error;
- }
- /**
- * Log SQL to file
- */
- Private Function Logsql () {
- Log::sql ($this->_lastsql);
- }
- /**
- * Record error log to file
- */
- Private Function LogError () {
- $str = ' [SQL ERR] '. $this->_error. ' SQL: '. $this->_lastsql;
- Log::warn ($STR);
- }
- }
- /**
- * Log Class
- * How to use: Log::fatal (' Error msg ');
- * Save path to App/log, store by day
- * Fatal and warning will be recorded in the. log.wf file
- */
- Class log{
- /**
- * Log, support SAE environment
- * @param string $msg log content
- * @param string $level log level
- * @param bool $WF is an error log
- */
- public static function Write ($msg, $level = ' DEBUG ', $wf =false) {
- if (function_exists (' Sae_debug ')) {//If it is SAE, use the Sae_debug function to log
- $msg = "[{$level}]". $msg;
- Sae_set_display_errors (FALSE);
- Sae_debug (Trim ($msg));
- Sae_set_display_errors (TRUE);
- }else{
- $msg = Date (' [y-m-d h:i:s] '). " [{$level}] ". $msg." \ r \ n ";
- $logPath = C (' App_full_path '). ' /log/'. Date (' Ymd '). Log ';
- if ($WF) {
- $logPath. = '. WF ';
- }
- File_put_contents ($logPath, $msg, file_append);
- }
- }
- /**
- * Print Fatal Log
- * @param string $msg log information
- */
- public static function Fatal ($MSG) {
- Self::write ($msg, ' FATAL ', true);
- }
- /**
- * Print Warning Log
- * @param string $msg log information
- */
- public static function warn ($msg) {
- Self::write ($msg, ' WARN ', true);
- }
- /**
- * Print Notice Log
- * @param string $msg log information
- */
- public static function notice ($msg) {
- Self::write ($msg, ' NOTICE ');
- }
- /**
- * Print Debug Log
- * @param string $msg log information
- */
- public static function debug ($msg) {
- Self::write ($msg, ' DEBUG ');
- }
- /**
- * Print SQL Log
- * @param string $msg log information
- */
- public static function SQL ($MSG) {
- Self::write ($msg, ' SQL ');
- }
- }
- /**
- * Extexception class, record additional exception information
- */
- Class Extexception extends exception{
- /**
- * @var Array
- */
- protected $extra;
- /**
- * @param string $message
- * @param array $extra
- * @param int $code
- * @param null $previous
- */
- Public function __construct ($message = "", $extra = Array (), $code = 0, $previous = null) {
- $this->extra = $extra;
- Parent::__construct ($message, $code, $previous);
- }
- /**
- * Get additional exception information
- * @return Array
- */
- Public Function Getextra () {
- return $this->extra;
- }
- }
Copy Code |