Browse Source

* Add request class
* Add uri class
* Begin adding domain handling

Nathaniel van Diepen 8 years ago
parent
commit
1b2afad6ca
4 changed files with 137 additions and 32 deletions
  1. 26 1
      app.class.php
  2. 31 0
      request.class.php
  3. 28 31
      router.class.php
  4. 52 0
      uri.class.php

+ 26 - 1
app.class.php

@@ -1,6 +1,7 @@
 <?php
 	require_once('base.class.php');
 	require_once('router.class.php');
+	require_once('response.class.php');
 	require_once('events.class.php');
 	class App implements Base, Events {
 		private static $apps = array();
@@ -39,7 +40,25 @@
 			}
 		}
 		public function handle($verb, $url, $data){
-
+			$res = new Response();
+			$self = $this;
+			$onerror = function($res, $error){
+				$self->error($error);
+			};
+			foreach($this->routers as $type => $routers){
+				foreach($routers as $router){
+					switch($type){
+						case 'prefix':
+							$router->handle($url["path"], $res, null, $onerror);
+						break;
+						case 'domain':
+							$router->handle($url["host"], $res, null, $onerror);
+						break;
+					}
+				}
+			}
+			$res->url = $url;
+			return $res;
 		}
 		public function error($error){
 
@@ -68,6 +87,12 @@
 				$fn($router);
 			});
 		}
+		public function domain($prefix, Callable $fn){
+			$this->route('domain', $prefix, function($router){
+				$router->base($prefix);
+				$fn($router);
+			});
+		}
 	}
 	error_reporting(E_ALL);
 	ini_set('display_errors', 'On');

+ 31 - 0
request.class.php

@@ -0,0 +1,31 @@
+<?php
+	class Request {
+		private $url;
+		private $headers;
+		private $body;
+		public function __construct($url, $headers, $body){
+			if(is_string($url) || $url instanceof Array){
+				$this->url = new Uri($url);
+			}elseif($url instanceof Uri){
+				$this->url = $url;
+			}else{
+				throw new Exception("Invalid url {$url}");
+			}
+			if($headers instanceof Array){
+				$this->headers = $headers;
+			}else{
+				$this->headers = array();
+			}
+			$this->body = $body;
+		}
+		public header($name){
+			return $this->headers[$name];
+		}
+		public json(){
+			return json_decode($this->body, true);
+		}
+		public text(){
+			return $this->body;
+		}
+	}
+?>

+ 28 - 31
router.class.php

@@ -51,41 +51,38 @@
 			$this->_paths = array();
 		}
 		public function handle($url,$res = null,$fn = null,$onerror = null){
-			if(strpos($url,$this->_base) !== false){
-				$url = substr($url,strpos($url,$this->_base)+strlen($this->_base));
-				if($url[0] != '/'){
-					$url = '/'.$url;
-				}
-				if(is_null($res)){
-					$res = new Response($url);
-				}else{
-					$res->url = $url;
-				}
-				if(!in_array($res,$this->responses)){
-					array_push($this->responses,$res);
-				}
-				ob_start();
-				$handled = false;
-				foreach($this->_paths as $k => $p){
-					if($p->matches($url)){
-						$handled = true;
-						try{
-							$p($res,$p->args($url));
-						}catch(Exception $e){
-							if(!is_null($onerror)){
-								$onerror($res,$e);
-							}else{
-								throw $e;
-							}
+			if($url[0] != '/'){
+				$url = '/'.$url;
+			}
+			if(is_null($res)){
+				$res = new Response($url);
+			}else{
+				$res->url = $url;
+			}
+			if(!in_array($res,$this->responses)){
+				array_push($this->responses,$res);
+			}
+			ob_start();
+			$handled = false;
+			foreach($this->_paths as $k => $p){
+				if($p->matches($url)){
+					$handled = true;
+					try{
+						$p($res,$p->args($url));
+					}catch(Exception $e){
+						if(!is_null($onerror)){
+							$onerror($res,$e);
+						}else{
+							throw $e;
 						}
 					}
 				}
-				if(!$handled && !is_null($fn)){
-					$fn($res,$url);
-				}
-				$res->output = ob_get_contents();
-				ob_end_clean();
 			}
+			if(!$handled && !is_null($fn)){
+				$fn($res,$url);
+			}
+			$res->output = ob_get_contents();
+			ob_end_clean();
 			return $res;
 		}
 	}

+ 52 - 0
uri.class.php

@@ -0,0 +1,52 @@
+<?php
+	class Uri{
+		private $url;
+		public __construct($url){
+			if($url instanceof Array){
+				$this->url = $url;
+			}else if(is_string($url)){
+				$this->url = parse_url($url);
+			}else{
+				throw new Exception("Invalid Url");
+			}
+		}
+		public __get($name){
+			if(isset($this->url[$name])){
+				return $this->url[$name];
+			}else{
+				switch($name){
+					case 'variables':
+						parse_str((string)$this, $output);
+						return $output;
+					break;
+				}
+			}
+		}
+		public __set($name, $value){
+			if(isset($this->url[$name])){
+				$this->url[$name] = $value;
+			}
+		}
+		public __toString(){
+			$port = $this->port
+			if($this->scheme == 'http'){
+				$port = $port == 80?"":":{$port}"
+			}elseif($this->scheme = 'https'){
+				$port = $port == 443?"":":{$port}"
+			}
+			$auth = $this->user;
+			if($auth){
+				$auth = $this->pass?"{$auth}:{$this->pass}@":"{$auth}@";
+			}
+			$query = $this->query;
+			if($query){
+				$query = "?{$query}";
+			}
+			$fragmanet = $this->fragmanet;
+			if($fragmanet){
+				$fragmanet = "#{$fragmanet}";
+			}
+			return "{$this->scheme}://{$auth}{$this->host}{$port}/{$this->path}{$query}{$fragmanet}";
+		}
+	}
+?>