MVC scheduler and template class-PHP source code

Source: Internet
Author: User
The MVC scheduler and template class adjust the parameter setting method.

New common routing mode

The pathinfo mode supports pseudo-static

Pathinfo now supports custom routing

 TVal = array ();}/*** set the template file directory * @ param string $ dir */public static function setTemplateDir ($ dir) {self :: $ tDir = $ dir;}/*** whether to compile in real time * @ param bool $ real */public static function setReal ($ real) {self: $ real = (bool) $ real;}/*** temporary file directory * @ param string $ dir */public static function setTmpDir ($ dir) {if (! File_exists ($ dir) {if (! Mkdir ($ dir, 0, true) die ("tmp dir $ dir can't to mkdir");} self: $ tTmpDir = realpath ($ dir );} /*** URL scheduler * @ param Dispatcher $ dispatcher */public static function setU (& $ dispatcher) {if (is_object ($ dispatcher) & method_exists ($ dispatcher, 'U') {self ::$ uDispatcher = $ dispatcher ;}} /*** variable value assignment * @ param mixed $ name * @ param mixed $ value */public function assign ($ name, $ value) {$ this-> tVal [$ name] = $ Value;}/*** get the template variable * @ param string $ name */public function getVal ($ name) {if (isset ($ this-> tVal [$ name]) {return $ this-> tVal [$ name];} else return false ;} /*** save the running content to an html file * @ param string $ tFile * @ param string $ html */public function saveHtml ($ tFile, $ html) {ob_start (); $ this-> display ($ tFile); $ buffer = ob_get_contents (); ob_end_clean (); file_put_contents ($ html, $ buffer );} /*** run and display the template * @ Param string $ tfile */public function display ($ tFile) {$ this-> tFile = $ this-> parseTemplatePath ($ tFile); if (! Self: $ real) {if (! File_exists ($ this-> getTmpFile () $ this-> parse (); elseif (filemtime ($ this-> tFile)> filemtime ($ this-> getTmpFile () $ this-> parse ();} else $ this-> parse (); extract ($ this-> tVal, EXTR_OVERWRITE); include $ this-> getTmpFile ();}/*** compiled file * @ return string $ filepath */private function getTmpFile () {$ basename = basename ($ this-> tFile); $ pos = strrpos ($ basename ,'. '); $ tmp = 'tpl _'. substr ($ basename, 0, $ p OS ). '. php '; return self: $ tTmpDir. '/'. $ tmp;} private function parse () {$ this-> tContent = file_get_contents ($ this-> tFile); $ this-> parseInclude (); $ this-> parseSection (); $ this-> parseVal (); $ this-> parseEval (); file_put_contents ($ this-> getTmpFile (), $ this-> tContent);} private function parseInclude () {$ this-> tContent = preg_replace ("/{templates + ([a-zA-z0-9. _] +)}/ies "," $ this-> subtemplate ('$ 1') ", $ th Is-> tContent);}/*** get template only * @ param string $ file */private function subtemplate ($ file) {return file_get_contents ($ this-> parseTemplatePath ($ file ));} /*** resolution template path * @ param string $ file * @ return string $ filepath */private function parseTemplatePath ($ tFile) contains too tfile.w.'.html '; $ tFile = self: $ tDir? Self: $ tDir. '/'. $ tFile: $ tFile; if (! File_exists ($ tFile) {die ("No template file $ tFile");} else {$ tFile = realpath ($ tFile);} return $ tFile ;} /*** resolve variable */private function parseVal () {$ this-> tContent = preg_replace ("/{(\ $ S + ?)} /Is ","
 ", $ This-> tContent);}/*** parse section */private function parseSection () {// Logic $ this-> tContent = preg_replace ("/{elseifs + (. + ?)} /Ies "," $ this-> stripvtags ('
 ', '')", $ This-> tContent); $ this-> tContent = preg_replace ("/{else}/is ","
 ", $ This-> tContent); $ this-> tContent = preg_replace ("/{U (. + ?))} /Ies "," $ this-> parseUrl ('$ 1') ", $ this-> tContent); // loop for ($ I = 0; $ I <6; $ I ++) {$ this-> tContent = preg_replace ("/{loops + (S +) s + (S + )}(. + ?) {/Loop}/ies "," $ this-> str1_tags ('
 ',' \ 3
 ') ", $ This-> tContent); $ this-> tContent = preg_replace ("/{loops + (S +) s + (S +) s + (S + )}(. + ?) {/Loop}/ies "," $ this-> str1_tags ('
 \ 3) {?> ',' \ 4
 ') ", $ This-> tContent); $ this-> tContent = preg_replace ("/{ifs + (. + ?)} (. + ?) {/If}/ies "," $ this-> str1_tags ('
 ',' \ 2
 ') ", $ This-> tContent) ;}} private function str1_tags ($ expr, $ statement ='') {$ expr = str_replace ("\"", ", preg_replace ("/
 /S "," \ 1 ", $ expr); $ statement = str_replace (" \ "", "", $ statement); return $ expr. $ statement;}/*** parse PHP statement */private function parseEval () {$ this-> tContent = preg_replace ("/{evals + (. + ?)} /Is ","
 ", $ This-> tContent);}/*** parse URL */private function parseUrl ($ url) {if (is_object (self ::$ uDispatcher )) {return self: $ uDispatcher-> U ($ url) ;}else {return $ url ;}}?>


 Find the location of the Module directory * DEFAULT_MODULE => default Module * DEFAULT_ACTION => default Action * DEBUG => enable debugging (true | false) * URL_MODEL => routing mode (0: Normal mode, 1: pathinfo mode) * URL_DELIMITER => The parameter delimiter pathinfo mode uses * URL_HTML_SUFFIX => 'File suffixes 'pathinfo mode pseudo-static use * ENTRY_INDEX => Entry file * URL_ROUTER_ON => Enable custom routing * normal URL mode U (module name/operation name? Parameter 1 = value 1 & parameter 2 = value 2) * routing mode U (route name @? Parameter 1 = value 1 & parameter 2 = value 2) */class Dispatcher {private static $ instance; private static $ _ SGLOBAL; // scheduling configuration private static $ route = array (); // private function _ construct () {self: initConfig ();} public static function getInstance () {if (! Self: $ instance instanceof self) {self: $ instance = new self ();} return self: $ instance;} private function _ clone () {}/*** run controller */public function run () {$ route = array (); if (self :: $ _ SGLOBAL ['url _ model'] = 1) {$ route = $ this-> pathInfoRoute ();} else {$ route = $ this-> generalRoute ();} $ modulefile = self: $ _ SGLOBAL ['Module _ path']. "/{$ route ['module']}. class. php "; if (file_exists ($ modulefile) {include $ modulefile; if (class_exists ($ route ['module']) {$ class = new $ route ['module']; if (method_exists ($ class, $ route ['action']) {call_user_func (array (& $ class, $ route ['action']);} else die ("in{$ Route ['module']}Module no this{$ Route ['action']}Action ");} else die (" no this{$ Route ['module']}Module ");} else {die (" no this{$ Route ['module']}Module ");} self: $ _ SGLOBAL ['endtime'] = microtime (true); $ this-> debugInfo ();} /*** output debugging information */private function debugInfo () {if (self: $ _ SGLOBAL ['debug']) {$ exectime = self :: $ _ SGLOBAL ['endtime']-self: $ _ SGLOBAL ['starttime']; $ debuginfo = <. Dispatcher_debug_table th ,. dispatcher_debug_table td {padding: 5px ;}. dispatcher_debug_table th {border-top: 1px solid red; border-left: 1px solid red; background-color: # ccc ;}. dispatcher_debug_table td {border-top: 1px solid red; border-left: 1px solid red; border-right: 1px solid red ;}. dispatcher_debug_table_last td ,. dispatcher_debug_table_last th {border-bottom: 1px solid red ;}. dispatcher_debug_table_title {border-right: 1px solid red ;} 
 
 "; $ Debuginfo. =" 
   "; $ Debuginfo. =" 
  
Debug Info
Execute Time $ Exectime s
Include File HTML; foreach (get_included_files () as $ file) {$ debuginfo. = $ file ."
";}$ Debuginfo. ="
Server Info "; $ Debuginfo. =" Host: ". $ _ SERVER ['http _ host']."
"; $ Debuginfo. =" PHP_Version: ". PHP_VERSION ."
"; $ Debuginfo. =" Server_Version: ". $ _ SERVER ['server _ soft']."
"; $ Debuginfo. ="
Client Info "; $ Debuginfo. =" Remote_Addr: ". $ _ SERVER ['remote _ ADDR ']."
"; $ Debuginfo. =" User_Agent: ". $ _ SERVER ['http _ USER_AGENT ']."
"; $ Debuginfo. ="
"; Echo $ debuginfo ;}} private function generalRoute () {$ route = array (); $ route ['module'] =! Empty ($ _ GET ['M'])? $ _ GET ['M']: self: $ _ SGLOBAL ['default _ module']; $ route ['action'] =! Empty ($ _ GET ['A'])? $ _ GET ['A']: self: $ _ SGLOBAL ['default _ action']; $ route ['module']. = 'action'; unset ($ _ GET ['M']); unset ($ _ GET ['A']); return $ route ;} /*** route scheduling in PATHINFO form * supports pseudo-static */private function pathInfoRoute () {$ route = array (); // pseudo-static if (self :: $ _ SGLOBAL ['url _ HTML_SUFFIX ']) {$ pos = strlen ($ _ SERVER ['path _ info'])-strlen (self :: $ _ SGLOBAL ['url _ HTML_SUFFIX ']); $ _ SERVER ['path _ info'] = substr ($ _ SERVER ['path _ info'], 0, $ p OS);} if (! $ _ SERVER ['path _ info'] | $ _ SERVER ['path _ info'] = '/') {$ route = array ('module' => self: $ _ SGLOBAL ['default _ module'], 'Action' => self :: $ _ SGLOBAL ['default _ action']);} else {$ _ SERVER ['path _ info'] = substr ($ _ SERVER ['path _ info'], 1); $ pathinfo = explode (self: $ _ SGLOBAL ['url _ DELIMITER '], $ _ SERVER ['path _ info']); // custom route if (self: $ _ SGLOBAL ['url _ ROUTER_ON '] & in_array ($ pathinfo [0], array_keys (self :: $ Route) {$ route ['module'] = self: $ route [$ pathinfo [0] [0]; $ route ['action'] = self:: $ route [$ pathinfo [0] [1]; $ c = explode (',', self: $ route [$ pathinfo [0] [2]); array_shift ($ pathinfo); foreach ($ c as $ r) {_ _ GET [$ r] = array_shift ($ pathinfo) ;}} else {if (count ($ pathinfo) <2) {$ route ['module'] = $ pathinfo [0]. self: $ _ SGLOBAL ['Module _ SUFFIX ']; $ route ['action'] = self: $ _ SGLOBAL ['default _ action']. Self: $ _ SGLOBAL ['action _ SUFFIX '];} else {$ route ['module'] = array_shift ($ pathinfo ). self: $ _ SGLOBAL ['Module _ SUFFIX ']; $ route ['action'] = array_shift ($ pathinfo ). self: $ _ SGLOBAL ['action _ SUFFIX '] ;}} if (count ($ pathinfo) >=2) {for ($ I = 0, $ cnt = count ($ pathinfo); $ I <$ cnt; $ I ++) {if (isset ($ pathinfo [$ I + 1]) {$ _ GET [$ pathinfo [$ I] = $ pathinfo [++ $ I] ;}}$ route ['module']. = 'action'; $ _ REQUE ST = array_merge ($ _ GET, $ _ POST); return $ route;}/*** url address combination * format: Module/Action? Get = par (module name/action name? Get parameter) * @ param string $ url * @ return string $ url */public static function U ($ url) {$ pathinfo = parse_url ($ url ); $ path = ''; $ get = array (); $ inroute = false; // user-defined route if (isset ($ pathinfo ['query']) {$ query = explode ('&', $ pathinfo ['query']); foreach ($ query as $ q) {list ($ k, $ v) = explode ('=', $ q); $ get [$ k] = $ v ;}} if (! Self ::$ _ SGLOBAL) {self: initConfig () ;}// url if (self :: $ _ SGLOBAL ['url _ model'] = 1) {if (self :: $ _ SGLOBAL ['url _ ROUTER_ON '] & strpos ($ pathinfo ['path'],' @ ')! = False) {// retrieve all custom routes $ routeNames = array_keys (self: $ route); $ p = substr ($ pathinfo ['path'], 0, -1); if (in_array ($ p, $ routeNames) {$ inroute = true; $ path. = '/'. $ p; $ c = explode (',', self: $ route [$ p] [2]); foreach ($ c as $ v) {if (isset ($ get [$ v]) {$ path. = self: $ _ SGLOBAL ['url _ DELIMITER ']. $ get [$ v]; unset ($ get [$ v]) ;}}} if (! $ Inroute) {if (isset ($ pathinfo ['path']) {list ($ module, $ action) = explode ('/', $ pathinfo ['path']); $ module = $ module? $ Module: self ::$ _ SGLOBAL ['default _ module']; $ action = $ action? $ Action: self ::$ _ SGLOBAL ['default _ action'];} else {$ module = self ::$ _ SGLOBAL ['default _ module']; $ action = self: $ _ SGLOBAL ['default _ action'];} $ path = "/$ module ". self: $ _ SGLOBAL ['url _ DELIMITER ']. $ action;} if (! Empty ($ get) {foreach ($ get as $ k => $ v) {$ path. = self: $ _ SGLOBAL ['url _ DELIMITER ']. $ k. self: $ _ SGLOBAL ['url _ DELIMITER ']. $ v ;}// url pseudo-static if (self: $ _ SGLOBAL ['url _ HTML_SUFFIX ']) {$ path. = self: $ _ SGLOBAL ['url _ HTML_SUFFIX '] ;}} elseif (self: $ _ SGLOBAL ['url _ model'] = 0) {$ url = parse_url ($ url); if (isset ($ url ['path']) {list ($ module, $ action) = explode ('/', $ url ['path']); $ module = $ module? $ Module: self ::$ _ SGLOBAL ['default _ module']; $ action = $ action? $ Action: self ::$ _ SGLOBAL ['default _ action'];} else {$ module = self ::$ _ SGLOBAL ['default _ module']; $ action = self: $ _ SGLOBAL ['default _ action'];} $ path. = "? M = $ module & a = $ action "; if ($ url ['query']) {$ path. = '&'. $ url ['query'] ;}} if (! Self: $ _ SGLOBAL ['url _ rewrite']) $ path = '/'. self: $ _ SGLOBAL ['entry _ INDEX']. $ path; return $ path;}/*** initialization configuration information */private static function initConfig () {if (defined ('app _ path ')) {// Default MODULE directory self: $ _ SGLOBAL ['Module _ path'] = APP_PATH. '/action';} self: $ _ SGLOBAL ['default _ action'] = 'index'; // DEFAULT action self :: $ _ SGLOBAL ['default _ module'] = 'index'; // default module // DEFAULT url routing mode; 1: pathinfo mode; 0: Normal mode self :: $ _ SGLO BAL ['url _ model'] = 1; self: $ _ SGLOBAL ['url _ DELIMITER '] ='/'; // parameter separator self :: $ _ SGLOBAL ['entry _ INDEX'] = 'index. php '; self: $ _ SGLOBAL ['url _ HTML_SUFFIX'] = null; // URL pseudo static self: $ _ SGLOBAL ['url _ rewrite'] = false; // URL Rewrite self: $ _ SGLOBAL ['starttime'] = microtime (true); self: $ _ SGLOBAL ['url _ ROUTER_ON '] = false; // whether to enable the routing function self: $ _ SGLOBAL ['debug'] = false;}/*** set the option conditions, you can set * MODULE_PATH => to locate the module directory * DEF. AULT_MODULE => default Module * DEFAULT_ACTION => default Action * DEBUG => enable debugging (true | false) * URL_DELIMITER => parameter separator * URL_MODEL => routing mode (0: Normal mode, 1: pathinfo mode) * URL_HTML_SUFFIX => 'File suffixes 'pathinfo mode: use * ENTRY_INDEX => Entry file * URL_ROUTER_ON => Enable custom routing * @ param array $ option */public function setOption ($ option) {$ o = array ('Module _ path', 'default _ module', 'default _ action', 'debug', 'URL _ DELIMITER ', 'URL _ model ', 'URL _ HTML _ SUFFIX ', 'entry _ INDEX', 'URL _ rewrite', 'URL _ ROUTER_ON'); foreach ($ option as $ k => $ v) {if (in_array ($ k, $ o) {self: $ _ SGLOBAL [$ k] = $ v ;}}} /*** set route * array ('Route '=> array ('Module name', 'Operation name', 'parameter 1, parameter 2, parameter 3 ')) * @ param array $ route */public function setRoute ($ route) {self ::$ route = $ route ;}}?>


 1, 'URL _ model' => 1, 'URL _ DELIMITER '=>'/', 'default _ action' => 'home ', 'URL _ rewrite' => 1, 'URL _ ROUTER_ON '=> true, 'URL _ HTML_SUFFIX' => '.html '); // custom Generic Routing $ router = array ('space' => array ('space', 'index', 'uid ')); $ dispatcher-> setOption ($ option); $ dispatcher-> setRoute ($ router); Template: setU ($ dispatcher); Template: setReal (true); Template :: setTemplateDir ('Template/default'); template: setTmpDir ('runtime/tpl '); $ Dispatcher-> run ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.