Преглед изворни кода

* Switch to passing the request to handlers
* Remove url from response

Nathaniel van Diepen пре 8 година
родитељ
комит
ba5c3231d0
4 измењених фајлова са 55 додато и 33 уклоњено
  1. 20 12
      app.class.php
  2. 19 6
      request.class.php
  3. 1 4
      response.class.php
  4. 15 11
      router.class.php

+ 20 - 12
app.class.php

@@ -2,6 +2,7 @@
 	require_once('base.class.php');
 	require_once('router.class.php');
 	require_once('response.class.php');
+	require_once('request.class.php');
 	require_once('uri.class.php');
 	require_once('events.interface.php');
 	class App extends Base implements Events {
@@ -10,12 +11,12 @@
 		private $routers;
 		private $router;
 		public $_onerror;
-		public function __construct($name){
+		public function __construct($name, Callable $fn = null){
 			$this->name = $name;
 			$this->router = new Router();
 			$this->routers = array();
 			$this->domains = array();
-			$this->_onerror = function($res, $error){
+			$this->_onerror = function($req, $res, $error){
 				$o = json_encode(array(
 					'message'=>$error->getMessage(),
 					'code'=>$error->getCode(),
@@ -34,6 +35,9 @@
 				}
 			};
 			static::$apps[] = $this;
+			if(is_callable($fn)){
+				$fn($this);
+			}
 		}
 		public function __destruct(){
 			$this->routers = array();
@@ -72,33 +76,37 @@
 		public static function shutdown_error($error){
 			foreach(static::$apps as $k => $app){
 				if(is_callable($app->onerror)){
-					$app->onerror(null, $error);
+					$app->onerror(null, null, $error);
 				}
 			}
 		}
-		public function handle($verb, $url, $data){
-			$res = new Response((string)new Uri($url));
+		public function handle($verb, $url, $data, $headers = null){
+			if(is_null($headers)){
+				$headers = getallheaders();
+			}
+			$res = new Response();
+			$req = new Request($url, $headers, $data);
 			$self = $this;
 			$onerror = function($res, $error) use($self){
-				$self->onerror($res, $error);
+				$self->onerror($req, $res, $error);
 			};
 			$handled = false;
 			// Domain routers
 			foreach($this->domains as $host => $router){
 				if($url['host'] == $host){
-					$router->handle($url["path"], $res, null, $onerror);
+					$router->handle($url["path"], $req, $res, null, $onerror);
 					$handled = $handled || $router->handled;
 				}
 			}
 			// Prefixed path routers
 			foreach($this->routers as $prefix => $router){
-				$router->handle($url["path"], $res, null, $onerror);
+				$router->handle($url["path"], $req, $res, null, $onerror);
 				$handled = $handled || $router->handled;
 			}
 			// Base router for non-prefixed paths
-			$this->router->handle($url["path"], $res, function($res, $url) use($handled, $self){
+			$this->router->handle($url["path"], $req, $res, function($req, $res) use($handled, $self){
 				if(!$handled){
-					$self->onerror($res,new Error("Not Found", 404));
+					$self->onerror($req, $res,new Error("Not Found", 404));
 				}
 			}, $onerror);
 			return $res;
@@ -125,10 +133,10 @@
 			$fn($this->domains[$host]);
 			return $this;
 		}
-		public function onerror($res, $error){
+		public function onerror($req, $res, $error){
 			$fn = $this->_onerror;
 			if(is_callable($fn)){
-				$fn($res, $error);
+				$fn($req, $res, $error);
 			}
 		}
 	}

+ 19 - 6
request.class.php

@@ -3,28 +3,41 @@
 		private $url;
 		private $headers;
 		private $body;
-		public function __construct($url, $headers, $body){
-			if(is_string($url) || $url instanceof Array){
+		public function __construct($url, $headers = array(), $body = ''){
+			if(is_string($url) || is_array($url)){
 				$this->url = new Uri($url);
 			}elseif($url instanceof Uri){
 				$this->url = $url;
 			}else{
 				throw new Exception("Invalid url {$url}");
 			}
-			if($headers instanceof Array){
+			if(is_array($headers)){
 				$this->headers = $headers;
 			}else{
 				$this->headers = array();
 			}
 			$this->body = $body;
 		}
-		public header($name){
+		public function __get($name){
+			switch($name){
+				case 'headers':
+					return $this->headers;
+				break;
+				case 'url':
+					return $this->url;
+				break;
+				case 'body':
+					return $this->body;
+				break;
+			}
+		}
+		public function header($name){
 			return $this->headers[$name];
 		}
-		public json(){
+		public function json(){
 			return json_decode($this->body, true);
 		}
-		public text(){
+		public function text(){
 			return $this->body;
 		}
 	}

+ 1 - 4
response.class.php

@@ -2,13 +2,10 @@
 	class Response {
 		public $output = '';
 		public $body = '';
-		public $url;
 		private $code = 200;
 		public $headers = array();
 		protected $open = true;
-		public function __construct($url){
-			$this->url = $url;
-		}
+		public function __construct(){}
 		public function __toString(){
 			return $this->body;
 		}

+ 15 - 11
router.class.php

@@ -1,6 +1,7 @@
 <?php
 	require_once('path.class.php');
 	require_once('response.class.php');
+	require_once('request.class.php');
 	class Router {
 		private $_paths = array();
 		private $_routers = array();
@@ -77,32 +78,35 @@
 		public function clear(){
 			$this->_paths = array();
 		}
-		public function handle($url,$res = null,Callable $fn = null, Callable $onerror = null){
-			if(strpos($url,$this->base) !== false){
-				$url = substr($url,strpos($url,$this->base)+strlen($this->base));
-				if($url[0] != '/'){
-					$url = '/'.$url;
+		public function handle($path, $req = null,$res = null,Callable $fn = null, Callable $onerror = null){
+			if(strpos($path, $this->base) !== false){
+				$path = substr($path,strpos($path,$this->base)+strlen($this->base));
+				if($path[0] != '/'){
+					$path = '/'.$path;
+				}
+				if(is_null($req)){
+					$req = new Request();
 				}
 				if(is_null($res)){
-					$res = new Response($url);
+					$res = new Response();
 				}
 				if(!in_array($res,$this->responses)){
 					array_push($this->responses,$res);
 				}
 				$handled = false;
 				foreach($this->_routers as $prefix => $router){
-					$router->handle($url, $res);
+					$router->handle($path, $req, $res);
 					$handled = $handled ||$router->handled;
 				}
 				ob_start();
 				foreach($this->_paths as $k => $p){
-					if($p->matches($url)){
+					if($p->matches($path)){
 						$handled = true;
 						try{
-							$p($res,$p->args($url));
+							$p($req, $res, $p->args($path));
 						}catch(Exception $e){
 							if(!is_null($onerror)){
-								$onerror($res,$e);
+								$onerror($req, $res,$e);
 							}else{
 								throw $e;
 							}
@@ -111,7 +115,7 @@
 				}
 				$this->_handled = $handled;
 				if(!$handled && !is_null($fn)){
-					$fn($res,$url);
+					$fn($req, $res);
 				}
 				$res->output .= ob_get_contents();
 				ob_end_clean();