Skip to content

Commit 513b1e6

Browse files
author
Wazabii
committed
Structural improvement
1 parent 241a244 commit 513b1e6

File tree

9 files changed

+78
-79
lines changed

9 files changed

+78
-79
lines changed

Environment.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public function __construct(array $env = [])
1717

1818
/**
1919
* Get request/server environment data
20-
* @param string $key Server key
21-
* @param string $default Default value, returned if Env data is empty
22-
* @return string|null
20+
* @param string $key Server key
21+
* @param string|null $default Default value, returned if Env data is empty
22+
* @return string
2323
*/
24-
public function get(string $key, ?string $default = ""): ?string
24+
public function get(string $key, ?string $default = ""): string
2525
{
2626
$key = strtoupper($key);
27-
return ($this->env[$key] ?? $default);
27+
return ($this->env[$key] ?? (string)$default);
2828
}
2929

3030
/**
@@ -62,7 +62,7 @@ public function getUriParts(array $add): array
6262
$arr['path'] = $this->getPath();
6363
$arr['query'] = $this->get("QUERY_STRING");
6464
$arr['fragment'] = null;
65-
if (!is_null($arr['port'])) {
65+
if (!is_numeric($arr['port'])) {
6666
$arr['port'] = (int)$arr['port'];
6767
}
6868

Interfaces/EnvironmentInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ interface EnvironmentInterface
66
{
77
/**
88
* Get request/server environment data
9-
* @param string $key Server key
10-
* @param string $default Default value, returned if Env data is empty
11-
* @return string|null
9+
* @param string $key Server key
10+
* @param string|null $default Default value, returned if Env data is empty
11+
* @return string
1212
*/
13-
public function get(string $key, ?string $default = ""): ?string;
13+
public function get(string $key, ?string $default = ""): string;
1414

1515
/**
1616
* Check if environment data exists

Interfaces/RequestInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,9 @@ public function withUri(UriInterface $uri, $preserveHost = false);
134134
*
135135
*/
136136

137+
/**
138+
* Get Cli arguments
139+
* @return array
140+
*/
141+
public function getCliArgs(): array;
137142
}

Interfaces/ServerRequestInterface.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,4 @@ public function withAttribute($name, $value);
258258
* @return static
259259
*/
260260
public function withoutAttribute($name);
261-
262-
/**
263-
*
264-
* Custom methods outside of PSR
265-
* But is used by the framework
266-
*
267-
*/
268-
269-
/**
270-
* Get Cli arguments
271-
* @return array
272-
*/
273-
public function getCliArgs(): array;
274261
}

Request.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Request extends Message implements RequestInterface
1515
private $requestTarget;
1616
protected $headers;
1717
protected $body;
18+
protected $cliKeywords;
19+
protected $cliArgs;
1820

1921
public function __construct(
2022
string $method,
@@ -153,4 +155,55 @@ private function resolveRequestStream(StreamInterface|array|string|null $body):
153155
}
154156
return $stream;
155157
}
158+
159+
/**
160+
* Get Cli keyword
161+
* @return string|null
162+
*/
163+
public function getCliKeyword(): ?string
164+
{
165+
if (is_null($this->cliKeywords)) {
166+
$new = array();
167+
$arg = $this->getUri()->getArgv();
168+
foreach ($arg as $val) {
169+
if (is_string($val)) {
170+
if ((strpos($val, "--") === 0) || (strpos($val, "-") === 0)) {
171+
break;
172+
} else {
173+
$new[] = $val;
174+
}
175+
}
176+
}
177+
array_shift($new);
178+
$this->cliKeywords = implode("/", $new);
179+
}
180+
181+
return $this->cliKeywords;
182+
}
183+
184+
/**
185+
* Get Cli arguments
186+
* @return array
187+
*/
188+
public function getCliArgs(): array
189+
{
190+
if (is_null($this->cliArgs)) {
191+
$args = $this->getUri()->getArgv();
192+
$this->cliArgs = array();
193+
foreach ($args as $arg) {
194+
if (is_string($arg)) {
195+
$arg = str_replace("&", "#", $arg);
196+
if ((($pos1 = strpos($arg, "--")) === 0) || (strpos($arg, "-") === 0)) {
197+
parse_str(substr($arg, ($pos1 !== false ? 2 : 1)), $result);
198+
foreach ($result as &$val) {
199+
$val = str_replace("#", "&", $val);
200+
}
201+
$this->cliArgs = array_merge($this->cliArgs, $result);
202+
}
203+
}
204+
}
205+
}
206+
207+
return $this->cliArgs;
208+
}
156209
}

ServerRequest.php

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class ServerRequest extends Request implements ServerRequestInterface
1111
{
1212
protected $attr = array();
1313
protected $env;
14-
protected $cliKeywords;
15-
protected $cliArgs;
16-
1714
protected $queryParams;
1815
protected $parsedBody;
1916

@@ -324,54 +321,6 @@ public function withoutAttribute($name): self
324321
}
325322

326323

327-
/**
328-
* Get Cli keyword
329-
* @return string|null
330-
*/
331-
public function getCliKeyword(): ?string
332-
{
333-
if (is_null($this->cliKeywords)) {
334-
$new = array();
335-
$arg = $this->getUri()->getArgv();
336-
foreach ($arg as $v) {
337-
if ((($pos1 = strpos($v, "--")) === 0) || (($pos2 = strpos($v, "-")) === 0)) {
338-
break;
339-
} else {
340-
$new[] = $v;
341-
}
342-
}
343-
array_shift($new);
344-
$this->cliKeywords = implode("/", $new);
345-
}
346-
347-
return $this->cliKeywords;
348-
}
349-
350-
/**
351-
* Get Cli arguments
352-
* @return array
353-
*/
354-
public function getCliArgs(): array
355-
{
356-
if (is_null($this->cliArgs)) {
357-
$arg = $this->getUri()->getArgv();
358-
$this->cliArgs = array();
359-
foreach ($arg as $v) {
360-
$v = str_replace("&", "#", $v);
361-
if ((($pos1 = strpos($v, "--")) === 0) || (($pos2 = strpos($v, "-")) === 0)) {
362-
parse_str(substr($v, ($pos1 !== false ? 2 : 1)), $result);
363-
foreach ($result as &$val) {
364-
$val = str_replace("#", "&", $val);
365-
}
366-
$this->cliArgs = array_merge($this->cliArgs, $result);
367-
}
368-
}
369-
}
370-
371-
return $this->cliArgs;
372-
}
373-
374-
375324
/*
376325
public function getEnv() {
377326
return $this->env;

Stream.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class Stream implements StreamInterface
3232
private $readable;
3333
private $writable;
3434
private $seekable;
35-
//private $output;
36-
3735

3836
/**
3937
* PSR-7 Stream
@@ -49,6 +47,11 @@ public function __construct(mixed $stream = null, string $permission = "r+")
4947
if (is_resource($stream)) {
5048
$this->resource = $stream;
5149
$this->meta = $this->getMetadata();
50+
51+
if (is_null($this->meta)) {
52+
throw new RuntimeException("Could not access the stream meta data.", 1);
53+
}
54+
5255
$this->stream = $this->meta['stream_type'];
5356
$this->permission = $this->meta['mode'];
5457
} else {

Uri.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ public function withUriParts(array $parts): self
364364
/**
365365
* Return part if object found and has not yet been encoded
366366
* @param string $key
367-
* @return string|null
367+
* @return string|int|float|null
368368
*/
369-
private function getUniquePart(string $key): ?string
369+
private function getUniquePart(string $key): string|int|float|null
370370
{
371371
return (!is_null($this->{$key}) && is_null($this->encoded[$key])) ? $this->{$key} : null;
372372
}

Url.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ public function getRealPath(): string
175175
public function getDirPath(): string
176176
{
177177
if (is_null($this->dirPath)) {
178-
$this->dirPath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $this->request->getUri()->getDir());
178+
$root = (isset($_SERVER['DOCUMENT_ROOT'])) ? $_SERVER['DOCUMENT_ROOT'] : "";
179+
$root = htmlspecialchars($root, ENT_QUOTES, 'UTF-8');
180+
$this->dirPath = str_replace($root, "", $this->request->getUri()->getDir());
179181
}
180182
if (!is_string($this->dirPath)) {
181183
throw new \Exception("Could not create dirPath", 1);

0 commit comments

Comments
 (0)