/** * Main dispatch process * @param array $app_dir * @param array $tpl_dir * @return unknown */ public function dispatch($app_dir, $tpl_dir) { // app dispatch time if (Hush_Debug::showDebug('time')) { $this->start_time = microtime(true); } $this->_formatPath(); $mapper_class = null; $mapper = $this->getMapper(); /* MAIN PROCESS * Parse mapping file * Get mapped class */ if ($mapper) { // prepare mapping $page_map = $mapper->getPageMap(); $path_raw = $this->_request->getPathInfo(); // do mapping loop foreach ((array) $page_map as $pattern => $class) { // handle REWRITE rules if (strpos($class, '/') === 0) { $pattern = preg_quote($pattern, '/'); $pattern = str_replace('\\*', '(.*?)', $pattern); $replacement = str_replace('*', '$1', $class); // not preg format $path_raw = preg_replace('/^' . $pattern . '$/i', $replacement, $path_raw); continue; } // handle NOT REGEXP rules if (strpos($pattern, '*') === false) { if (!strcasecmp($path_raw, $pattern) || !strcasecmp($this->_path, $pattern)) { $mapper_class = $class; break; } continue; } // handle REGEXP rules if (true) { $pattern = preg_quote($pattern, '/'); $pattern = str_replace('\\*', '(.*?)', $pattern); if (preg_match('/^' . $pattern . '$/i', $path_raw, $path_args)) { $mapper_class = $class; // fill params foreach ($path_args as $k => $v) { $_REQUEST['$' . $k] = $v; } break; } continue; } } } /* MAIN PROCESS * Get class & action name */ if ($mapper_class) { // get mapped class $className = $this->getMapPageClass($mapper_class); // get action from mapping rule if (strpos($mapper_class, '*') === false) { $actionName = $this->getMapPageAction($mapper_class); // get action from url path } else { $actionName = $this->getDefaultPageAction(); } } else { // get default class & action from url $className = $this->getDefaultPageClass(); $actionName = $this->getDefaultPageAction(); } /* MAIN PROCESS * Get action args */ $actionArgs = $this->getActionArgs(); /* MAIN PROCESS * Get template name */ $templateName = $this->getTemplateName($className, $actionName); // app dispatch time if (Hush_Debug::showDebug('time')) { $this->end_time = microtime(true); $debug = Hush_Debug::getInstance(); $debug->setWriter(new Hush_Debug_Writer_Html()); $debug->debug($this->end_time - $this->start_time, '<span style="color:red">Hush App Dispatch Time >>></span>', Hush_Debug::INFO); } /* MAIN PROCESS * find page by url */ try { // load page class @Zend_Loader::loadClass($className, $app_dir); // debug should be closed if (!class_exists($className)) { echo 1; require_once 'Hush/App/Exception.php'; throw new Hush_App_Exception('Can not find definition for class \'' . $className . '\''); } } catch (Exception $e) { require_once 'Hush/Util.php'; Hush_Util::HTTPStatus(404); if (!$this->_debug) { if (file_exists($this->_epage[404])) { include_once $this->_epage[404]; exit; } } else { $this->_printDebugInfo(array('className' => $className, 'actionName' => $actionName, 'actionArgs' => $actionArgs, 'templateName' => templateName), $e); } } /* MAIN PROCESS * display page by url */ try { // close auto-load for page view class if (self::$pageViewClass) { require_once 'Hush/Page.php'; Hush_Page::closeAutoLoad(); // close page autoload mechanism } // create page $page = new $className(); /* USE PAGE VIEW PROCESS * set template for page view class */ if (self::$pageViewClass) { if ($tpl_dir) { $page->setTemplateDir($tpl_dir); } $page->__prepare(); } // set page's debug level if ($this->_debugLevel) { $page->setDebugLevel($this->_debugLevel); } // callback method implemented in page class if (method_exists($page, '__init')) { $page->__init(); } // call page action method $page->{$actionName}($actionArgs); // callback method implemented in page class if (method_exists($page, '__done')) { $page->__done(); } /* USE PAGE VIEW PROCESS * display template for page view class */ if (self::$pageViewClass) { $page->__display($templateName); } } catch (Exception $e) { require_once 'Hush/Util.php'; Hush_Util::HTTPStatus(500); if (!$this->_debug) { if (file_exists($this->_epage[500])) { include_once $this->_epage[500]; exit; } } else { $this->_printDebugInfo(array('className' => $className, 'actionName' => $actionName, 'actionArgs' => $actionArgs, 'templateName' => templateName), $e); } } }
/** * Main dispatch process * @param array $app_dir * @param array $tpl_dir * @return unknown */ public function dispatch($app_dir, $tpl_dir) { // app dispatch time if (Hush_Debug::showDebug('time')) { $this->start_time = microtime(true); } $this->_formatPath(); $mapper_class = array(); $mapper = $this->getMapper(); /* MAIN PROCESS * Parse mapping file * Get mapped class */ if ($mapper) { // prepare mapping $page_map = $mapper->getPageMap(); $path_raw = $this->_request->getPathInfo(); // map with raw url path if (array_key_exists($path_raw, $page_map)) { $mapper_class = $page_map[$path_raw]; } elseif (array_key_exists($this->_path, $page_map)) { $mapper_class = $page_map[$this->_path]; } else { foreach ((array) $page_map as $pattern => $class) { // escape not REGEXP rules if (strpos($pattern, '*') === false) { continue; } // url matching $pattern = preg_quote($pattern, '/'); $pattern = str_replace('\\*', '(.*?)', $pattern); if (preg_match('/^' . $pattern . '$/i', $path_raw)) { $mapper_class = $class; break; } } } } /* MAIN PROCESS * Get class & action name */ if ($mapper_class) { // get mapped class $className = $this->getMapPageClass($mapper_class); // get action from mapping rule if (strpos($mapper_class, '*') === false) { $actionName = $this->getMapPageAction($mapper_class); // get action from url path } else { $actionName = $this->getDefaultPageAction(); } } else { // get default class & action from url $className = $this->getDefaultPageClass(); $actionName = $this->getDefaultPageAction(); } /* MAIN PROCESS * Get action args */ $actionArgs = $this->getActionArgs(); /* MAIN PROCESS * Get template name */ $tplName = $this->getTemplateName($className, $actionName); // app dispatch time if (Hush_Debug::showDebug('time')) { $this->end_time = microtime(true); $debug = Hush_Debug::getInstance(); $debug->addWriter(new Hush_Debug_Writer_Html()); $debug->debug($this->end_time - $this->start_time, '<span style="color:red">Hush App Dispatch Time >>></span>', Hush_Debug::INFO); } /* MAIN PROCESS * Enter page scope */ try { // load page class @Zend_Loader::loadClass($className, $app_dir); // debug should be closed if (!class_exists($className)) { require_once 'Hush/App/Exception.php'; throw new Hush_App_Exception('Can not find definition for class \'' . $className . '\''); } /* USE PAGE VIEW PROCESS * close auto-load for page view class */ if (self::$pageViewClass) { require_once 'Hush/Page.php'; Hush_Page::closeAutoLoad(); // close page autoload mechanism } // create page $page = new $className(); /* USE PAGE VIEW PROCESS * set template for page view class */ if (self::$pageViewClass) { if ($tpl_dir) { $page->setTemplateDir($tpl_dir); } $page->__prepare(); } // set page's debug level if ($this->_debugLevel) { $page->setDebugLevel($this->_debugLevel); } // callback method implemented in page class if (method_exists($page, '__init')) { $page->__init(); } // call page action method $page->{$actionName}($actionArgs); // callback method implemented in page class if (method_exists($page, '__done')) { $page->__done(); } /* USE PAGE VIEW PROCESS * display template for page view class */ if (self::$pageViewClass) { $page->__display($tplName); } } catch (Exception $e) { require_once 'Hush/Util.php'; if (!$this->_debug) { Hush_Util::HTTPStatus(404); if (file_exists($this->_epage)) { include_once $this->_epage; exit; } } else { echo '<b>Dispatch Debug Info >>></b>' . "<br/>\n" . "<br/>\n"; echo 'Class Name : ' . $className . "<br/>\n"; echo 'Action Name : ' . $actionName . "<br/>\n"; echo 'Action Args : ' . json_encode($actionArgs) . "<br/>\n"; echo 'Template Name : ' . $tplName . "<br/>\n" . "<br/>\n"; echo '<b>Dispatch Exception Info >>></b>' . "<br/>\n"; Hush_Util::trace($e); } } }