/** * 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 }
/** * write config * * @static * @return void */ public static function write() { $get = func_get_args(); if (count($get) == 1) { self::$config = array_merge(self::$config, $get[0]); } else { for ($i = 0; $i < count($get); $i += 2) { $name = $get[$i]; $value = $get[$i + 1]; $tmp = array($name => $value); self::$config = array_merge(self::$config, $value); } } }
/** * get self instanced object * * @return object */ public static function getInstance() { return empty(self::$_instance) && !self::$_instance instanceof self ? new self(Printemps_Config::read('database')) : self::$_instance; }
/** * compile template-file to executable-script * * @param string $filepath * @param string $controller * @param string $template * @param string $suffix * @return mixed */ public function compile($filepath, $controller, $template, $suffix) { /* try to read file */ $file = @file_get_contents($filepath); if (!$file) { Printemps_Exception::notice("Can not read template file : " . $filepath . "."); return false; } /* begin compiling */ $content = $file; $partten = array('/\\{(\\$.*?)\\}/', "/\\{include.*?path=['|\"](.*?)['|\"].*?suffix=['|\"'](.*?)['|\"]\\}/", "/\\{!if(.*?)\\}/", "/\\{!endif\\}/", "/\\{!else\\}/", "/\\{&repeat time=(.*?)\\}/", "/\\{&endrepeat\\}/", "/\\{&while condition=\\((.*?)\\) update=\\((.*?)\\)\\}/", "/\\{&endwhile\\}/", "/&&(.*?)&&/", "/\\{go=['|\"](.*?)['|\"]\\}/"); $replacement = array("<?php echo \$1;?>", "<?php Printemps_View::getInstance()->display(\"\$1\", \"\$2\"); ?>", "<?php if (\$1): ?>", "<?php endif; ?>", "<?php else: ?>", "<?php for (\$i = 0; \$i < \$1; \$i++) { ?>", "<?php } ?>", "<?php while (\$1): \$2 ?>", "<?php endwhile; ?>", "<?php \$1 ?>", "<?php echo Printemps_Response::generate(\"\$1\"); ?>"); $content = preg_replace($partten, $replacement, $content); $compiled = Printemps_Config::read("APP_CACHES") . $controller . '/' . $template . '.' . $suffix . '.php'; if (!is_dir(Printemps_Config::read("APP_CACHES") . $controller)) { mkdir(Printemps_Config::read("APP_CACHES") . $controller, 755); } $newfile = @fopen($compiled, "w+", true); if (!$newfile) { Printemps_Exception::notice("Can not create / open template file : " . $compiled . "."); return false; } $writer = @fwrite($newfile, $content); if (!$writer) { Printemps_Exception::notice("Can not write template file : " . $compiled . "."); return false; } return $compiled; //return compiled file address }
/** * 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; }