Example #1
0
 /**
  * start router distributing
  * 
  * @return void
  */
 public static function dispatch()
 {
     /* load custom router rules */
     self::$custom = Printemps_Config::read("router");
     $mode = APP_ENTRY_MODE;
     switch ($mode) {
         case 1:
             $request = $_SERVER['QUERY_STRING'];
             break;
         case 2:
         case 3:
             isset($_SERVER['PATH_INFO']) ? $request = $_SERVER['PATH_INFO'] : ($request = '');
             break;
     }
     $request = explode("/", $request);
     array_splice($request, 0, 1);
     //remove first element of request array
     if (isset($request[0]) && !empty($request[0])) {
         self::$requested['controller'] = isset(self::$custom['class'][$request[0]]) ? self::$custom['class'][$request[0]] : $request[0];
         if (isset($request[1]) && !empty($request[1])) {
             self::$requested['method'] = isset(self::$custom['method'][self::$requested['controller'] . ':' . $request[1]]) ? self::$custom['method'][self::$requested['controller'] . ':' . $request[1]] : $request[1];
             /** Write params */
             if (isset($request[2]) && $request[2] != '') {
                 for ($i = 2; $i < count($request) - 1; $i += 2) {
                     $name = $request[$i];
                     $value = isset($request[$i + 1]) ? $request[$i + 1] : "";
                     Printemps_Config::setGlobal($name, $value);
                 }
             }
         } else {
             self::$requested['method'] = "index";
         }
     } else {
         self::$requested['controller'] = "index";
         self::$requested['method'] = "index";
     }
     self::$requested['fullname'] = self::$requested['controller'] . 'Controller';
     /* check if controller doesn't exist */
     if (!class_exists(self::$requested['fullname'])) {
         Printemps_Exception::halt("Requested Controller : " . self::$requested['fullname'] . " doesn't exist.");
     }
     $called = new ReflectionClass(self::$requested['fullname']);
     //create a ReflectionClass
     $called = $called->newInstance();
     //Instance a ReflectionClass
     /* check if method doesn't exist */
     if (!method_exists($called, self::$requested['method'])) {
         Printemps_Exception::halt("Requested Method : " . self::$requested['method'] . "() doesn't exist.");
     }
     call_user_func(array($called, self::$requested['method']));
     //Call requested method
 }
 /**
  * Printemps Framework App 初始化
  * @param  array  $config 用户配置
  * @return none
  */
 public static function Init($initialize = array())
 {
     /** 对Printemps Framework 做必须的初始化 */
     //设置错误拾取函数
     set_error_handler("Printemps_Error", E_CORE_ERROR ^ E_USER_ERROR);
     set_error_handler("Printemps_Notice", E_WARNING ^ E_NOTICE);
     /** 是否开始SESSION会话 */
     $startSession = isset($initialize['session']) ? $initialize['session'] ? true : false : false;
     if ($startSession) {
         session_start();
     }
     /** 是否立刻开始路由分发 */
     $startRouter = isset($initialize['router']) ? $initialize['router'] ? true : false : true;
     if ($startRouter) {
         Printemps_Router::Dispatch();
     }
 }
Example #3
0
 /**
  * Printemps initialization function
  * 
  * @param  array  $cfg config options
  * @return bool  if Printemps has succeessfully inited, return true
  */
 public static function init($cfg = array())
 {
     $basepath = dirname(__FILE__);
     /* default config file */
     $_default = array("APP_ROOT_DIR" => $basepath, "APP_DEPENDENCE" => $basepath . '/dependence/', "APP_VIEWS" => $basepath . '/dependence/views/', "APP_CACHES" => $basepath . '/dependence/caches/', "APP_PUBLIC" => $basepath . '/public', "APP_ASSETS" => $basepath . '/public/assets/', "APP_NAME" => "Printemps Application", "APP_VERSION" => "1", "APP_DEBUG_MODE" => false, "APP_ENTRY_MODE" => 1, "APP_ERROR_HANDLER" => false);
     foreach ($_default as $key => $value) {
         if (isset($cfg['initial'][$key])) {
             define($key, $cfg['initial'][$key]);
         } else {
             define($key, $value);
         }
     }
     /** Register Autoload  method*/
     self::registerAutoload();
     if (APP_ERROR_HANDLER) {
         set_error_handler(APP_ERROR_HANDLER, E_CORE_ERROR ^ E_USER_ERROR);
     } else {
         set_error_handler(array("Printemps_Exception", "halt"), E_CORE_ERROR ^ E_USER_ERROR);
     }
     set_error_handler(array("Printemps_Exception", "notice"), E_WARNING ^ E_NOTICE);
     //for notice / warning
     set_exception_handler(array("Printemps_Exception", "halt"));
     //for exception
     Printemps_Config::write($_default);
     Printemps_Config::write($cfg);
     isset($cfg['router']) && $cfg['router'] ? Printemps_Router::dispatch() : false;
     $_printemps = Printemps::getInstance();
     return $_printemps;
 }