<?php
class Easy_Controller{
	/*
	@var object
	*/
	public $load = null;
	/*
	@var string session
	*/
	public $namesapce;
	/*
	@var string $domain 站点域名
	*/
	public $domain;
	/*
	@var string $cookiepath cookie保存的路径
	*/
	public $cookiepath;
	/*
	@var object $_Controller 控制器对象
	*/
	public $_Controller;
	/*
	@var string $model 模块的名称
	*/
	public $model = null ;
	/*
	@var string $action 动作的名称
	*/
	public $action = null;
	/*
	@var string $viewPath 模板保存的目录
	*/
	public $viewPath = 'www/templates';		//模板的保存路径
	public $controllerPath = 'app/admin';		//应用程序的路径
	public $ViewSuffix = '.htm';	//文件模板的后缀名
	public $_Request;
	public $langauge;
	public $basedir;	//按照目录安装
	public $locale;
	public $defaultControl = 'index';	//默认控制器名
	public $Rewrite = false;	//是否开启URL重写
	public $installdir = '';
	public $defaultAction = 'index';
	public $indexName;
	function __construct(){
		$this->_Request = new Easy_Controller_Request();
		$this->namescape = 'show';
	}
	/*
	@param string $lang 当前语言
	*/
	public function setLangauge($lang='zh-cn'){
		$this->language = $lang;
	}
	public function setLocale($path='language'){
		$this->locale = $path;
	}
	public function setIndex($name=null){
		if (is_null($name)===false){
			$this->indexName = $name;
		}
	}
	public function setControllerPath($path){
		$this->controllerPath = $path;
	}
	public function setViewPath($path){
		$this->viewPath = $path;
	}
	public function setViewSuffix($name){
		$this->ViewSuffix = $name;
	}
	public function setBaseDir($path=null){
		//echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		if (is_null($path) === false){
			$this->basedir = $path;
			//echo $_SERVER['REQUEST_URI'];
			if ($path == '/'){
				$this->uri = str_replace('/'.$this->indexName, '', $_SERVER['REQUEST_URI']);
			}else{
				$this->uri = str_replace(array($path.$this->indexName, $path), '',$_SERVER['REQUEST_URI']);
			}
			//echo $this->uri;
			//$this->uri = preg_replace('/([\/]+)/is', '/', $this->uri);
		}else{
			$this->basedir = '/';
			$this->uri = $_SERVER['REQUEST_URI'];
		}
		if ($this->Rewrite === true){
			$this->installdir = $this->basedir;
		}else{
			$this->installdir = $this->basedir.$this->indexName.'/';
		}
		define('INSTALLDIR', $this->installdir);
		$this->setRequest();
	}
	/*设置默认动作名*/
	public function getDefaultAction(){
		return $this->defaultAction;
	}
	/*设置默认动作名*/
	public function setDefaultAction($name='index'){
		$this->defaultAction = $name;
	}
	/*获取默认控制器名*/
	public function getDefaultControl(){
		return $this->defaultControl;
	}
	/*获取默认控制器名*/
	public function setDefaultControl($name = 'index'){
		$this->defaultControl = $name;
	}
	public function setRequest(){
		$pos = stripos($this->uri, '?');
		if (stripos($this->uri, '/')!==0){
			$start = 0;
		}else{
			$start = 1;
		}
		if ($pos!==false){
			$uri = substr($this->uri, $start, $pos-1);
		}else{
			$uri = substr($this->uri, $start);
		}
		$array = explode('/', urldecode($uri));
		//unset($_GET);
		$_GET['controller'] = empty($array[0])===false ? ucfirst(strtolower($array[0])) : $this->getDefaultControl();
		$_GET['action'] = empty($array[1])===false ? strtolower($array[1]) : $this->getDefaultAction();
		for($i=1;$i<count($array); $i++){
			if ($i % 2 == 0){
				if (empty($array[$i+1])===false || is_numeric($array[$i+1])){
					$_GET[strtolower($array[$i])] = $array[$i+1];
				}
				$i++;
			}
		}
		unset($array);
		$_REQUEST = array_merge($_POST, $_GET);
	}
	public function run(){
		$this->controller = $this->_Request->getParam('controller');
		$this->Action = $this->_Request->getParam('action');
		$class = $this->controller.'Controller';
		$Action = $this->Action.'Action';
		$this->_View = new Easy_Controller_View();
		$this->_View->viewPath = $this->viewPath;	//设置视图路径
		$this->_View->basedir = $this->basedir;
		$this->_View->installdir = $this->installdir;
		$this->_View->ViewSuffix= $this->ViewSuffix;	// 设置视图文件最缀
		$this->_View->_Request = $this->_Request;
		$this->_View->viewFile = $this->controller.DIRECTORY_SEPARATOR.$this->controller.'_'.$this->Action;	//设置视图文件路径
		//echo $this->controller;
		if (is_array($this->load)){
			$this->_Controller = new $class(array_merge(array('_View' => &$this->_View), $this->load));
		}else{
			$this->_Controller = new $class(array('_View' => $this->_View));
		}
		//print_r($this->_Controller->_View);
		//echo $this->_Controller;
		
		//$this->_Controller->_Request = $this->_Request;
		unset($action, $class);
		$this->_Controller->$Action();
		print($this->_Controller->_View->render($this->_Controller->_View->viewFile));
	}
	/**/
	public function register($class){
		$this->load = $class;
	}
}