Skip to content

Commit

Permalink
Merge branch 'master' of github.com:PhpGt/Http
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Mar 2, 2023
2 parents ffeee11 + e3e4cf5 commit 12f84de
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 47 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Cache Composer dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: /tmp/composer-cache
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}

- uses: php-actions/composer@v5
- name: Composer
uses: php-actions/composer@v6
with:
php_version: '8.1'

- name: Archive build
run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./

- name: Upload build archive for test runners
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: build-artifact
path: /tmp/github-actions
Expand All @@ -31,7 +34,7 @@ jobs:
needs: [composer]

steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v3
with:
name: build-artifact
path: /tmp/github-actions
Expand All @@ -42,7 +45,7 @@ jobs:
- name: PHP Unit tests
uses: php-actions/phpunit@v3
with:
php_version: 8.0
php_version: '8.1'
php_extensions: xdebug
memory_limit: 256M
configuration: test/phpunit/phpunit.xml
Expand All @@ -53,7 +56,7 @@ jobs:
needs: [composer]

steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v3
with:
name: build-artifact
path: /tmp/github-actions
Expand All @@ -64,5 +67,6 @@ jobs:
- name: PHP Static Analysis
uses: php-actions/phpstan@v3
with:
php_version: '8.1'
path: src/
level: 6
4 changes: 2 additions & 2 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
build:
environment:
php: 8.0.0
php: 8.1.0

nodes:
analysis:
Expand Down Expand Up @@ -29,4 +29,4 @@ checks:
filter:
excluded_paths:
- test/*
- vendor/*
- vendor/*
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ This repository is an implementation of the PSR-7 interfaces for use within PHP.
***

<a href="https://github.com/PhpGt/Http/actions" target="_blank">
<img src="https://badge.status.php.gt/http-build" alt="PHP.Gt/Http build status" />
<img src="https://badge.status.php.gt/http-build.svg" alt="PHP.Gt/Http build status" />
</a>
<a href="https://scrutinizer-ci.com/g/PhpGt/Http" target="_blank">
<img src="https://badge.status.php.gt/http-quality" alt="PHP.Gt/Http code quality" />
<img src="https://badge.status.php.gt/http-quality.svg" alt="PHP.Gt/Http code quality" />
</a>
<a href="https://scrutinizer-ci.com/g/PhpGt/Http" target="_blank">
<img src="https://badge.status.php.gt/http-coverage" alt="PHP.Gt/Http code coverage" />
<img src="https://badge.status.php.gt/http-coverage.svg" alt="PHP.Gt/Http code coverage" />
</a>
<a href="https://packagist.PhpGt/packages/PhpGt/Http" target="_blank">
<img src="https://badge.status.php.gt/http-version" alt="PHP.Gt/Http latest release" />
<img src="https://badge.status.php.gt/http-version.svg" alt="PHP.Gt/Http latest release" />
</a>
<a href="http://www.php.gt/Http" target="_blank">
<img src="https://badge.status.php.gt/http-docs" alt="PHP.Gt/Http documentation" />
<img src="https://badge.status.php.gt/http-docs.svg" alt="PHP.Gt/Http documentation" />
</a>

[fig]: https://www.php-fig.org/psr/psr-7/
[fig]: https://www.php-fig.org/psr/psr-7/
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",

"require": {
"php": ">=8.0",
"php": ">=8.1",
"phpgt/input": "^v1",
"psr/http-message": "^v1.0.1",
"willdurand/negotiation": "v3.1.0"
Expand Down
52 changes: 28 additions & 24 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Psr\Http\Message\StreamInterface;

trait Message {
protected Headers $headers;
public readonly Headers $headers;
protected string $protocol;
protected StreamInterface $stream;

Expand All @@ -17,7 +17,7 @@ trait Message {
* @return string HTTP protocol version.
*/
public function getProtocolVersion():string {
if(strstr($this->protocol, "/")) {
if(str_contains($this->protocol, "/")) {
return substr(
$this->protocol,
strpos($this->protocol, "/") + 1
Expand All @@ -41,7 +41,7 @@ public function getProtocolVersion():string {
* @return static
* @throws InvalidProtocolHttpException
*/
public function withProtocolVersion($version) {
public function withProtocolVersion($version):self {
if(!is_numeric($version)) {
throw new InvalidProtocolHttpException($version);
}
Expand Down
20 changes: 20 additions & 0 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ public function createServerRequestFromGlobalState(
$uri = new Uri($server["REQUEST_URI"] ?? null);
$headers = new RequestHeaders();

if($secure = $server["HTTPS"] ?? null) {
$uri = $uri->withScheme("https");
}
else {
$uri = $uri->withScheme("http");
}

if($port = $server["SERVER_PORT"] ?? null) {
$uri = $uri->withPort((int)$port);
}

if($host = $server["HTTP_HOST"] ?? null) {
$host = strtok($host, ":");
$uri = $uri->withHost($host);
}

if($query = $server["QUERY_STRING"] ?? null) {
$uri = $uri->withQuery($query);
}

foreach($server as $key => $value) {
if(str_starts_with($key, "HTTP_")) {
$headerKey = substr($key, strlen("HTTP_"));
Expand Down
12 changes: 9 additions & 3 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Response implements ResponseInterface {
public function __construct(
private ?int $statusCode = null,
ResponseHeaders $headers = null,
private ?Request $request = null,
) {
$this->headers = $headers ?? new ResponseHeaders();
$this->stream = new Stream();
Expand All @@ -28,13 +29,18 @@ public function setExitCallback(callable $callback):void {
$this->exitCallback = $callback;
}

public function reload():void {
$this->redirect("./");
public function reload(bool $keepQuery = true):void {
$uri = $this->request?->getUri() ?? new Uri();
$uri = $uri->withPath("./");
if(!$keepQuery) {
$uri = $uri->withQuery("");
}
$this->redirect($uri);
}

public function redirect(string|UriInterface $uri, int $statusCode = 303):void {
$this->statusCode = $statusCode;
$this->headers->set("Location", $uri);
$this->headers->set("Location", (string)$uri);
if(isset($this->exitCallback)) {
call_user_func($this->exitCallback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected function rawurlencodeMatchZero(array $match):string {
protected function filterUserInfo(string $user, string $pass = null):string {
$userInfo = $user;

if(strlen($pass) > 0) {
if(strlen($pass ?? "") > 0) {
$userInfo .= ":";
$userInfo .= $pass;
}
Expand Down
16 changes: 16 additions & 0 deletions test/phpunit/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ public function testCreateServerRequestFromGlobals_uri():void {
self::assertEquals("/path/to/somewhere", $request->getUri()->getPath());
}

public function testCreateServerRequestFromGlobals_uri_withAllParts():void {
$sut = new RequestFactory();
$request = $sut->createServerRequestFromGlobalState([
"REQUEST_METHOD" => "POST",
"REQUEST_URI" => "/path/to/somewhere/",
"SERVER_PORT" => 8080,
"HTTP_HOST" => "localhost:8080",
"QUERY_STRING" => "example=123",
], [], [], []);

self::assertSame(
"http://localhost:8080/path/to/somewhere/?example=123",
(string)$request->getUri()
);
}

public function testCreateServerRequestFromGlobals_header():void {
$sut = new RequestFactory();
$request = $sut->createServerRequestFromGlobalState([
Expand Down
Loading

0 comments on commit 12f84de

Please sign in to comment.