Example #1
0
 /**
  * Singleton method
  * @return Hush_Debug
  */
 public static function getInstance()
 {
     if (self::$_debug === null) {
         self::$_debug = new Hush_Debug();
     }
     return self::$_debug;
 }
Example #2
0
 /**
  * Assign prepared data into template and display
  * Could be called by some class which need do page process manually
  * @see Hush_App_Dispatcher
  * @param string $tpl_name Passed template name
  * @return unknown
  */
 public function __done()
 {
     // page execute time
     $this->end_time = microtime(true);
     $this->_rtime = $this->end_time - $this->start_time;
     // whether display
     if (Hush_Debug::showDebug('time')) {
         $this->debug($this->_rtime, '<span style="color:red">Service Execute Time >>></span>', Hush_Debug::INFO);
     }
     // print debug msg
     $this->_debug->write();
 }
Example #3
0
 /**
  * Assign prepared data into template and display
  * Could be called by some class which need do page process manually
  * @see Hush_App_Dispatcher
  * @param string $tpl_name Passed template name
  * @return unknown
  */
 public function __display($tpl_name = null)
 {
     // display setted template
     if ($this->getTemplate()) {
         $tpl_name = $this->getTemplate();
     }
     // display passed template
     // TODO : Smarty 3 bug in caching
     if ($tpl_name && $this->view->templateExists($tpl_name)) {
         $this->view->display($tpl_name);
     }
     // page execute time
     if (Hush_Debug::showDebug('time')) {
         $this->end_time = microtime(true);
         $this->debug(__TPL_ENGINE . '_' . $this->view->getVersion(), '<span style="color:red">Template Engine Version >>></span>', Hush_Debug::INFO);
         $this->debug($this->end_time - $this->start_time, '<span style="color:red">Page Execute Time >>></span>', Hush_Debug::INFO);
         $this->debug($this->view->isCached($tpl_name), '<span style="color:red">Page Cached >>></span>', Hush_Debug::INFO);
     }
     // print debug msg
     $this->_debug->write();
 }
Example #4
0
 /**
  * Overload the query method of Zend Db
  * So we can debug the sql message
  * @see Hush_Debug
  * @param string $sql
  * @param array $bind
  * @return Zend_Db_Statement_Interface
  */
 public function query($sql, $bind = array())
 {
     if ($this->_debug) {
         require_once 'Hush/Debug.php';
         $debug = Hush_Debug::getInstance();
         $debug->setWriter(new Hush_Debug_Writer_Html());
         // default can be override
         if (!$debug instanceof Hush_Debug) {
             require_once 'Zend/Db/Adapter/Exception.php';
             throw new Zend_Db_Adapter_Exception("Can not initialize 'Hush_Debug' instance");
         }
         if ($sql instanceof Zend_Db_Select) {
             $sql = $sql->__toString();
         }
         if (sizeof($bind) > 0) {
             $label = 'Prepared Sql >>>';
         } else {
             $label = 'Query Sql >>>';
         }
         $debug->debug($sql, '<font style="color:red">' . $label . '</font>');
     }
     return parent::query($sql, $bind);
 }
Example #5
0
 /**
  * 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);
         }
     }
 }
Example #6
0
 /**
  * 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);
         }
     }
 }