Skip to content

Commit 0b0bb9d

Browse files
author
Wazabii
committed
Code structure
1 parent 9b84126 commit 0b0bb9d

23 files changed

+45
-450
lines changed

Client.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use PHPFuse\Http\Interfaces\ResponseInterface;
99
use PHPFuse\Http\Interfaces\ClientInterface;
1010
use PHPFuse\Http\Interfaces\StreamInterface;
11-
1211
use PHPFuse\Http\Exceptions\ClientException;
1312
use PHPFuse\Http\Exceptions\RequestException;
1413
use PHPFuse\Http\Exceptions\NetworkException;
@@ -66,7 +65,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
6665
$this->requestDataLength = strlen($this->requestData);
6766
$this->prepareRequest($request);
6867
$this->buildHeaders($request);
69-
68+
7069
try {
7170
if (!extension_loaded('curl')) {
7271
throw new InvalidArgumentException('You need to enable CURL on your server.');
@@ -92,7 +91,7 @@ public function sendRequest(RequestInterface $request): ResponseInterface
9291
$this->delete();
9392
break;
9493
default:
95-
throw new InvalidArgumentException('The requesr method ('.$request->getMethod().') '.
94+
throw new InvalidArgumentException('The requesr method (' . $request->getMethod() . ') ' .
9695
'is not supported.');
9796
}
9897

@@ -230,7 +229,7 @@ private function buildHeaders(RequestInterface $request): void
230229
{
231230
$data = array();
232231
foreach ($request->getHeaders() as $name => $val) {
233-
$data[] = "{$name}: ".$request->getHeaderLine($name);
232+
$data[] = "{$name}: " . $request->getHeaderLine($name);
234233
}
235234
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $data);
236235
}

Cookies.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Http;

Dir.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Http;
@@ -18,7 +17,7 @@ public function __construct($dir)
1817

1918
public function getDir(string $path = "")
2019
{
21-
return $this->dir.$path;
20+
return $this->dir . $path;
2221
}
2322

2423
public function getRoot(string $path = "")

Env.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Http;
@@ -118,7 +117,7 @@ private function put(array $data, bool $overwrite = false)
118117
{
119118
foreach ($data as $key => $value) {
120119
if (!$overwrite && getenv($key) !== false) {
121-
throw new InvalidArgumentException("The Environmental variable \"{$key}\" already exists. ".
120+
throw new InvalidArgumentException("The Environmental variable \"{$key}\" already exists. " .
122121
"It's recommended to make every variable unique.", 1);
123122
}
124123
$_ENV[$key] = $value;

Message.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class Message implements MessageInterface
1717
protected $env;
1818
protected $path;
1919
protected $headerLine;
20-
20+
2121
/**
2222
* Get server HTTP protocol version number
2323
* @return string

Request.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
use PHPFuse\Http\Interfaces\UriInterface;
77
use PHPFuse\Http\Interfaces\HeadersInterface;
88
use PHPFuse\Http\Interfaces\StreamInterface;
9-
10-
//use PHPFuse\Http\Uri;
11-
9+
use PHPFuse\Http\Uri;
1210

1311
class Request extends Message implements RequestInterface
1412
{
1513
private $method;
1614
private $uri;
1715
private $requestTarget;
18-
1916
protected $headers;
2017
protected $body;
2118

@@ -33,14 +30,14 @@ public function __construct(
3330
}
3431

3532
/**
36-
* Get the message request target
33+
* Get the message request target (path+query)
3734
* @return string
3835
*/
3936
public function getRequestTarget(): string
4037
{
41-
if (is_null($this->requestTarget)) {
42-
$parts = $this->getUriEnv();
43-
$this->requestTarget = $parts['path'].(($parts['query']) ? "?{$parts['query']}" : "");
38+
$this->requestTarget = $this->getUri()->getPath();
39+
if ($query = $this->getUri()->getQuery()) {
40+
$this->requestTarget .= '?' . $query;
4441
}
4542
return $this->requestTarget;
4643
}
@@ -97,9 +94,9 @@ public function withUri(UriInterface $uri, $preserveHost = false): RequestInterf
9794
{
9895
$inst = clone $this;
9996
if ($preserveHost) {
100-
$uri->withHost($this->getHeader("Host"));
97+
$uri = $uri->withHost($this->getHeader("Host"));
10198
}
102-
$inst->uriInst = $url;
99+
$inst->uri = $uri;
103100
return $inst;
104101
}
105102

@@ -119,7 +116,8 @@ public function isSSL(): bool
119116
*/
120117
public function getPort(): int
121118
{
122-
$port = (int)(($p = $this->env->get("SERVER_PORT")) ? $p : $this->uri->getPort());
119+
$serverPort = $this->env->get("SERVER_PORT");
120+
$port = (int)(($serverPort) ? $serverPort : $this->uri->getPort());
123121
return (int)$port;
124122
}
125123

Response.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Http;
@@ -152,7 +151,7 @@ public function withLastModified(string $date): ResponseInterface
152151
{
153152
$clone = clone $this;
154153
$clone->modDate = strtotime($date);
155-
return $clone->withHeader('Last-Modified', gmdate('D, d M Y H:i:s', $clone->modDate).' GMT');
154+
return $clone->withHeader('Last-Modified', gmdate('D, d M Y H:i:s', $clone->modDate) . ' GMT');
156155
}
157156

158157
/**
@@ -162,7 +161,7 @@ public function withLastModified(string $date): ResponseInterface
162161
*/
163162
public function withExpires(string $date): ResponseInterface
164163
{
165-
return $this->withHeader("Expires", gmdate('D, d M Y H:i:s', strtotime($date)).' GMT');
164+
return $this->withHeader("Expires", gmdate('D, d M Y H:i:s', strtotime($date)) . ' GMT');
166165
}
167166

168167
/**
@@ -175,7 +174,7 @@ public function setCache(int $time, int $ttl): ResponseInterface
175174
{
176175
return $this->withHeaders([
177176
"Cache-Control" => "max-age={$ttl}, immutable, public",
178-
"Expires" => date("D, d M Y H:i:s", $time + $ttl)." GMT",
177+
"Expires" => date("D, d M Y H:i:s", $time + $ttl) . " GMT",
179178
"Pragma" => "public"
180179
]);
181180
}

ServerRequest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PHPFuse\Http\Interfaces\ServerRequestInterface;
66
use PHPFuse\Http\Interfaces\UriInterface;
77
use PHPFuse\Http\Interfaces\EnvironmentInterface;
8-
98
use PHPFuse\Http\Stream;
109

1110
class ServerRequest extends Request implements ServerRequestInterface

UploadedFile.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Http;
@@ -16,7 +15,7 @@ class UploadedFile implements UploadedFileInterface
1615
public const ERROR_PHRASE = [
1716
UPLOAD_ERR_OK => "There is no error, the file uploaded with success",
1817
UPLOAD_ERR_INI_SIZE => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
19-
UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified ".
18+
UPLOAD_ERR_FORM_SIZE => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified " .
2019
"in the HTML form",
2120
UPLOAD_ERR_PARTIAL => "The uploaded file was only partially uploaded",
2221
UPLOAD_ERR_NO_FILE => "No file was uploaded",

Uri.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
declare(strict_types=1);
54

65
namespace PHPFuse\Http;
@@ -24,19 +23,19 @@ class Uri implements UriInterface
2423
'ldap' => 389,
2524
];
2625

27-
private $uri;
2826
private $parts = array();
2927
private $scheme;
28+
//private $uri;
3029
private $host;
3130
private $port;
3231
private $user;
3332
private $pass;
3433
private $path;
3534
private $query;
3635
private $fragment; // Anchor/after hash
36+
private $dir;
3737
private $userInfo;
3838
private $authority;
39-
private $dir;
4039
private $argv;
4140
private $encoded;
4241
private $build;
@@ -53,9 +52,23 @@ public function __construct(array|string $uri)
5352
} else {
5453
$this->parts = parse_url($uri);
5554
}
55+
$this->pollyfill();
5656
$this->fillParts();
5757
}
5858

59+
protected function pollyfill()
60+
{
61+
$this->scheme = "http";
62+
$this->host = "localhost";
63+
$this->port = 80;
64+
$this->user = "";
65+
$this->pass = "";
66+
$this->path = "";
67+
$this->query = "";
68+
$this->fragment = "";
69+
$this->dir = "";
70+
}
71+
5972
/**
6073
* Get formated URI
6174
* @return string
@@ -343,9 +356,9 @@ public function withFragment(string $fragment): UriInterface
343356
* @param array $parts E.g. (parse_url or ["scheme" => "https", ...])
344357
* @return self
345358
*/
346-
public static function withUriParts(array $parts): self
359+
public function withUriParts(array $parts): self
347360
{
348-
$inst = new self();
361+
$inst = clone $this;
349362
$inst->parts = $parts;
350363
$inst->fillParts();
351364
return $inst;
@@ -378,10 +391,11 @@ public function getPart(string $key): ?string
378391
private function fillParts(): void
379392
{
380393
$vars = get_object_vars($this);
381-
foreach ($vars as $k => $v) {
382-
$this->encoded[$k] = null;
383-
if (isset($this->parts[$k]) && ($p = $this->parts[$k])) {
384-
$this->{$k} = $p;
394+
foreach ($vars as $key => $valueNotUsed) {
395+
$this->encoded[$key] = null;
396+
$part = ($this->parts[$key] ?? null);
397+
if (!is_null($part)) {
398+
$this->{$key} = $part;
385399
}
386400
}
387401
}

0 commit comments

Comments
 (0)