All pastes #611629 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-07-10 00:41 UTC
rendered paste body
<?php

if(!APP) exit;

class Controller
{
	public $Database;
	public $Request;
	public $Response;
	
	public $Controller;
	
	public $action;
	public $page;
	public $path;
	public $route_data;
	public $controller_dir;
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $Database
	 * @param unknown_type $Request
	 * @param unknown_type $Response
	 */
	final public function __construct($Database, $Request, $Response)
	{
		$this->Request = $Request;
		
		$this->action = 'index';
		$this->route_data = array();
		$this->controller_dir = CONTROLLER_DIR;
		
		$this->getPath();
		$this->getController();
		$this->getRoutes();
		$this->run();
	}
	
	/**
	 * Enter description here...
	 *
	 */
	final public function getPath()
	{
		$path = $this->Request['path'];
		$path = preg_split('~/~', $path, -1, PREG_SPLIT_NO_EMPTY);
		
		$i = -1;
		while($path[++$i]) {
			if(is_dir($this->controller_dir . $path[$i])) {
				$this->controller_dir .= array_shift($path) . DS;
			} else {
				break;
			}
		}
		
		$this->page = empty($path) ? 'index' : array_shift($path);
		$this->path = $path;
	}
	
	final public function getController()
	{
		$page = $this->page;
		
		$controller_file = $this->controller_dir . "{$page}.php";
		$controller_class = ucfirst($page) . 'Controller';
	
		if(!file_exists($controller_file)) {
			throw new Exception();
		}
				
		require_once($controller_file);
		
		$this->Controller = new $controller_class($this->Database, $this->Request, $this->Response);
	}
	
	final public function getRoutes()
	{
		$Controller = $this->Controller;
		$routes = $Controller->routes;
		$path = $this->path;
		$action = $path[0];
		
		if(isset($path) && isset($routes[$action])) {
			$route = $routes[$action];
			
			if(substr_count($route, '(') != substr_count($route, ')')) {
				throw new Exception();
			}
			
			$route = preg_split("~/~", $route, -1, PREG_SPLIT_NO_EMPTY);
			
			$route_size = count($route);
			$path_size = count($path);
			
			if($route_size > $path_size) {
				$path = array_pad($path, $path_size + ($route_size - $path_size), null);
			}
			
			array_splice($path, $route_size);
			
			$route = array_combine($route, $path);
			
			array_shift($route);
			
			foreach($route as $key => $value) {
				preg_match("~\((?P<type>.*)\)(?P<key>.*)~", $key, $match);
				
				$this->route_data[$match['key']] = typecast($value, $match['type']);
			}
		}
		
		$this->action = ($this->action) ? $action : $this->action;
	}
	
	public function run()
	{
		if(!is_callable(array($this->Controller, $this->action))) {
			throw new Exception();
		}

		call_user_method_array($this->action, $this->Controller, $this->route_data);
	}
	
}

class PageController
{
	public $Database;
	public $Request;
	public $Response;
	
	public function __construct($Database, $Request, $Response)
	{
		$this->Database = $Database;
		$this->Request = $Request;
		$this->Response = $Response;
	}
}

?>