Kaynağa Gözat

dev 2020-10

Max F 5 yıl önce
ebeveyn
işleme
48f65a6757

+ 16 - 5
src/App/Request.php

@@ -4,13 +4,17 @@ namespace KarmaFW\App;
 
 use \KarmaFW\Routing\Route;
 
+// TODO: a remplacer par ou rendre compatible avec GuzzleHttp\Psr7\Request
 
 class Request
 {
-	protected $url = null;
 	protected $method = null;
+	protected $url = null;
+	protected $protocol = null;
+
 	protected $client_ip = null;
 	protected $route = null;
+
 	public $GET = null;
 	public $POST = null;
 	public $COOKIE = null;
@@ -20,10 +24,13 @@ class Request
 	public $SERVER = null;
 
 
-	public function __construct($url=null, $method=null)
+	//public function __construct($url=null, $method=null)
+	public function __construct($method, $url, array $headers=[], $body=null, $version='1.1')
 	{
 		$this->url = $url;
-		$this->method = $method;
+		$this->method = strtoupper($method);
+		$this->protocol = $version;
+		//$this->setHeaders($headers);
 
 		//print_r($_SERVER); exit;
 	}
@@ -52,10 +59,14 @@ class Request
 
 	public static function createFromGlobals()
 	{
-		$url = isset($_SERVER['REQUEST_URI']) ? explode("?", $_SERVER['REQUEST_URI'])[0] : null;
 		$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
+		
+		//$url = isset($_SERVER['REQUEST_URI']) ? explode("?", $_SERVER['REQUEST_URI'])[0] : null;
+		$url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
+
+
+		$request = new self($method, $url);
 
-		$request = new self($url, $method);
 		/*
 		$request->setGet(isset($_GET) ? $_GET : []);
 		$request->setPost(isset($_POST) ? $_POST : []);

+ 41 - 25
src/App/Response.php

@@ -2,15 +2,18 @@
 
 namespace KarmaFW\App;
 
+// TODO: a remplacer par ou rendre compatible avec GuzzleHttp\Psr7\Response
+
 
 class Response
 {
 	protected $headers = [];
-	protected $content = '';
+	protected $body = '';
 	protected $status = 200;
-	protected $status_name = 'OK';
+	protected $reasonPhrase = 'OK';
 	protected $content_type = '';
 	protected $headers_sent = false;
+	protected $protocol = null;
 
 	/* public */ const http_status_codes = [
 		100 => 'Continue',
@@ -54,28 +57,34 @@ class Response
 	];
 
 
-	public function __construct($content='', $content_type='text/html')
+	public function __construct($status=200, array $headers=[], $body=null, $version='1.1', $reason=null) // Psr7
 	{
-		$this->content = $content;
-		$this->content_type = $content_type;
+		$this->body = $body;
+		$this->setStatus($status);
+		$this->setheaders($headers);
+
+        $this->protocol = $version;
 	}
+	//public ResponseText::__construct($body=null, $content_type='text/plain', $status=200) { return Response($status, ['Content-Type' => $content_type], $body); }
+	//public ResponseHtml::__construct($body=null, $status=200) { return ResponseText($body, 'html', $status); }
+
 
 	public function getStatus()
 	{
 		return $this->status;
 	}
 
-	public function getStatusName()
+	public function getReasonPhrase()
 	{
-		return $this->status_name;
+		return $this->reasonPhrase;
 	}
 
 	public function setStatus($status=200)
 	{
 		$this->status = $status;
 
-		$status_name = ! empty(self::http_status_codes[$status]) ? self::http_status_codes[$status] : "Unknown status";
-		$this->status_name = $status_name;
+		$reasonPhrase = ! empty(self::http_status_codes[$status]) ? self::http_status_codes[$status] : "Unknown status";
+		$this->reasonPhrase = $reasonPhrase;
 	}
 
 	public function getContentType()
@@ -88,34 +97,41 @@ class Response
 		$this->content_type = $content_type;
 	}
 
-	public function getBody()
+	public function getContent()
 	{
-		return $this->getContent(); 
+		// DEPRECATED
+		return $this->getBody(); 
 	}
 
-	public function getContent()
+	public function getBody()
 	{
-		return $this->content;
+		return $this->body;
 	}
 
 	public function getContentLength()
 	{
-		return strlen($this->content);
+		return strlen($this->body);
+	}
+
+	public function setContent($body)
+	{
+		// DEPRECATED
+		return $this->setBody($body);
 	}
 
-	public function setContent($content)
+	public function setBody($body)
 	{
-		$this->content = $content;
+		$this->body = $body;
 	}
 
-	public function append($content)
+	public function append($body)
 	{
-		$this->content .= $content;
+		$this->body .= $body;
 	}
 
-	public function prepend($content)
+	public function prepend($body)
 	{
-		$this->content = $content . $this->content;
+		$this->body = $body . $this->body;
 	}
 
 	public function getHeaders()
@@ -152,9 +168,9 @@ class Response
 		}
 
 		if (! empty($this->status)) {
-			$status_name = empty($this->status_name) ? "Unknown http status" : $this->status_name;
-			header('HTTP/1.0 ' . $this->status . ' ' . $status_name);
-			$this->headers['X-Status'] = $this->status . ' ' . $status_name;
+			$reasonPhrase = empty($this->reasonPhrase) ? "Unknown http status" : $this->reasonPhrase;
+			header('HTTP/1.0 ' . $this->status . ' ' . $reasonPhrase);
+			$this->headers['X-Status'] = $this->status . ' ' . $reasonPhrase;
 		}
 
 		if (empty($this->headers['Content-Type']) && ! empty($this->content_type)) {
@@ -181,8 +197,8 @@ class Response
 			$this->sendHeaders();
 		}
 
-		if (strlen($this->content) > 0) {
-			echo $this->content;
+		if (strlen($this->body) > 0) {
+			echo $this->body;
 		}
 	}
 

+ 7 - 7
src/App/ResponseError.php

@@ -3,19 +3,19 @@
 namespace KarmaFW\App;
 
 
-class ResponseError extends Response
+class ResponseError extends ResponseText
 {
 	protected $status = 500;
-	protected $status_name = 'Server Error';
+	protected $reasonPhrase = 'Server Error';
 
 
-	public function __construct($status=500, $content=null, $content_type='text/html')
+	public function __construct($status=500, $body=null, $content_type='text/html')
 	{
-		parent::__construct($content, $content_type);
+		parent::__construct($body, $status, $content_type);
 
-		if (is_null($content)) {
-			$this->content  = '<title>' . $this->status . " " . $this->status_name . '</title>';
-			$this->content .= '<h1>' . $this->status . " " . $this->status_name . '</h1>';
+		if (is_null($body)) {
+			$this->body  = '<title>' . $this->status . " " . $this->reasonPhrase . '</title>';
+			$this->body .= '<h1>' . $this->status . " " . $this->reasonPhrase . '</h1>';
 		}
 
 		$this->setStatus($status);

+ 14 - 0
src/App/ResponseHtml.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace KarmaFW\App;
+
+
+class ResponseHtml extends ResponseText
+{
+
+	public function __construct($body=null, $status=200)
+	{
+		parent::__construct($body, $status, 'text/html');
+	}
+
+}

+ 16 - 0
src/App/ResponseText.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace KarmaFW\App;
+
+
+class ResponseText extends Response
+{
+
+	public function __construct($body=null, $status=200, $content_type='text/plain', $headers=[])
+	{
+		$headers['Content-Type'] = $content_type;
+
+		parent::__construct($status, $headers, $body);
+	}
+
+}