Commits

xiaoai pu committed e8f3fa2

re write framework

Comments (0)

Files changed (17)

 final class configure
 {
 	public static $router = array(
-		'' => 'index',
-		'__404__' => 'page404'
+		'default' => 'path',
+		'rule' => array(
+			'' => 'index',
+			'__404__' => 'page404'
+		)
 	);
-	
 	public static $cache = array(
 		'file' => array(
 			'time' => 3600,
 <?php
-isset($_SERVER['REQUEST_TIME_FLOAT']) || ($_SERVER['REQUEST_TIME_FLOAT']=microtime(true));
-define('START_TIME', $_SERVER['REQUEST_TIME_FLOAT']);
+define('START_TIME', microtime(true));
 define('APP', '\app');
 
 require 'bootstrap.php';
 $router = \sys\router::getInstance();
 
 
-$controllername = $router->controller();
-$router->location($controllername);
-
+$action = $router->action();
+$action = $router->rewrite($action);
 $params = $router->params();
 // $router->filter($params);
 
-$controllerclass = (APP .'\\controller\\'. $controllername);
+$actionclass = (APP .'\\controller\\'. $action);
 
-$controller = new $controllerclass();
+$controller = new $actionclass;
 
 unset($controllerclass, $controllername);
 
 $controller->init($params);
 $controller->run();
-$controller->out();
+$controller->push();
 
 // $event->outputBefore = new \sys\module\debug;
 // $event->outputBefore = new \sys\module\gzip;
 
 // $event->outputBefore = $controller->output;
 // $controller->output  = $event->outputBefore;
-var_dump($controller->output);
+
 echo $controller->output;
 
 // $event->outputAfter  = new \sys\module\cron;

sys/cache/file.php

 
 final class file implements \sys\super\cache
 {
+	private $__value__ = array();
+	private $dir;
+
 	public function __construct(&$configure)
 	{
 		$this->dir = $configure['path'];
 	}
 	
-	public function get($name)
+	public function &__get($name)
 	{
-		$this->filename($name);
-		if( file_exists($name) ){
-			return file_get_contents($name);
+		$filename = $this->filename($name);
+		if( array_key_exists($name, $this->__value__) ){
+
+		}elseif( file_exists($filename) ){
+			$this->__value__[$name] = file_get_contents($filename);
 		}else{
-			return false;
+			$this->__value__[$name] = false;
 		}
+		return $this->__value__[$name];
 	}
 	
-	public function set($name, $value)
+	public function __set($name, $value)
 	{
-		$this->filename($name);
-		return file_put_contents($name, $value);
+		$filename = $this->filename($name);
+		return file_put_contents($filename, $value);
 	}
 	
 	private function filename(&$name)
 	{
-		$name = $this->dir.'/joanercache_'.dechex(crc32($name));
+		return $this->dir.'/joanercache_'.dechex(crc32($name));
 	}
-}
+}
 
 final class database implements \sys\super\factory
 {
-	protected static $_instance_;
+	protected static $instance;
+
 	public static function getInstance($name=null)
 	{
 		
 				$class = '\\PDO';
 		}
 		
-		self::$_instance_[$name] = new $class($dsn, $user, $pswd);
-		return self::$_instance_[$name];
+		self::$instance[$name] = new $class($dsn, $user, $pswd);
+		return self::$instance[$name];
 	}
-}
+}
 		return $this->__value__[$event];
 	}
 	
-}
+}
 namespace sys;
 
 final class router implements \sys\super\factory
-{
-	
-	private static $default = 'dispatch';
-	private static $param;
-	
-	
-	/**
-	 * 
-	 * @return object	new \sys\router\*
-	 */
+{	
 	public static function getInstance($name=null)
 	{
 		if( is_null($name) ){
 				break;
 				case 'cgi-fcgi':
 				default:
-					$name = 'dispatch';
+					$name =& \configure::$router['default'];
 			}
 		}
 		$classname = __CLASS__.'\\'.$name;
-		if( property_exists('\configure', __CLASS__) ){
-			self::$param =& \configure::${__CLASS__};
-		}
-		return new $classname(self::$param);
+		return new $classname;
 	}
-	
-
-}
+}

sys/router/command.php

 namespace sys\router;
 
 
-final class command extends \sys\super\router
+final class command implements \sys\super\router
 {
-	public function controller()
+	public function action()
 	{
 		global $argv;
 		
 		return array_slice($argv, 3);
 	}
 	
-}
+	public function rewrite($from, $to)
+	{
+
+	}
+}

sys/router/dispatch.php

-<?php
-namespace sys\router;
-
-
-final class dispatch extends \sys\super\router
-{
-	public function controller()
-	{
-		$path = $this->getPath();
-		return $path;
-	}
-
-	public function params()
-	{
-		return $_GET;
-	}
-	
-	
-	private function getPath()
-	{
-		$path = $_SERVER['REQUEST_URI'];
-		if(strpos($path, PATH) === 0 ){
-			$path = substr($path, strlen(PATH));
-		}
-		$path = trim($path, '/');
-		return $path;
-	}
-
-}

sys/router/path.php

+<?php
+namespace sys\router;
+
+
+final class path implements \sys\super\router
+{
+	public function action()
+	{
+		$path = $this->getPath();
+		return $path;
+	}
+
+	public function params()
+	{
+		return $_GET;
+	}
+	
+	public function rewrite($action)
+	{
+		if( array_key_exists($action, \configure::$router['rule']) ){
+			$action = \configure::$router['rule'][$action];
+		}
+		return $action;
+	}	
+
+	private function getPath()
+	{
+		$path = $_SERVER['REQUEST_URI'];
+		if(strpos($path, PATH) === 0 ){
+			$path = substr($path, strlen(PATH));
+		}
+		$path = trim($path, '/');
+		return $path;
+	}
+
+}

sys/super/cache.php

 interface cache
 {
 	public function __construct(&$configure);
-	public function get($name);
-	public function set($name, $value);
-}
+	public function &__get($name);
+	public function __set($name, $value);
+}

sys/super/controller.php

 	
 	public function init($params=null)
 	{
-		
 	}
 	
 	abstract function run();
 	
-	public function out()
+	public function push()
 	{
 		if( ! ($this->view instanceof \sys\super\view) ){
-			throw new Exception('The view class must extends \sys\super\view');
+			throw new CodeException();
 		}
 		$this->output = (string)$this->view;
 	}
-}
+}

sys/super/factory.php

 interface factory
 {
 	public static function getInstance($name=null);
-}
+}

sys/super/library.php

+<?php
+namspace sys\super;
+
+interface library
+{
+	
+}

sys/super/model.php

 
 abstract class model
 {
-	public static $expiretime = 60;
+	// cache expire time for second
+	const expiretime = 60;
+	// param data: "eventname => param"
 	private $__value__ = array();
-	
-	public function __construct()
-	{
-	}
+
 	
 	public function __call($name, $param)
 	{
 		return $data;
 	}
 
-}
+}

sys/super/module.php

 <?php
 namespace sys\super;
 
-abstract class module
+interface module
 {
-	
-}
+	/**
+	 * @return boolean
+	 */
+	public static function check();
+
+	/**
+	 * @return boolean
+	 */
+	public function run(&$data, $event);
+}

sys/super/router.php

 <?php
 namespace sys\super;
 
-abstract class router
+interface router
 {
-	/**
-	 * return the controller class name
-	 * @return string
-	 */
-	abstract function controller();
-	
-	/**
-	 * return the controller params
-	 * @return array
-	 */
-	abstract function params();
-	
-	
-	public function location(&$controller)
-	{
-		if( array_key_exists($controller, \configure::$router) ){
-			$controller = \configure::$router[$controller];
-		}
-	}
-	
+	// @return string : controller name
+	public function action();
+	// @return array : request param
+	public function params();
+	// @return string : controller name
+	public function rewrite($action);	
 }

sys/super/view.php

 			}
 			return $this->data[$name];
 		}else{
-			exit('View: '. $name .' Not found');
+			echo ('View: '. $name .' Not found');
 		}
 	}
 	
 		return $str;
 	}
 	
-	public function encode($html)
-	{
-		return htmlspecialchars($html);
-	}
 	
 	/**
 	abstract function title();
 	abstract function article();
 	abstract function footer();
 	*/
-}
+}