From badb6c8931e7add1224580792b4b7be96f39e737 Mon Sep 17 00:00:00 2001 From: Kevin Jansen Date: Mon, 27 Nov 2023 16:56:24 +0100 Subject: [PATCH] Add PSR Server Request from Globals helper function --- .github/workflows/code-analysis.yaml | 36 ++++++++++++++++++++ .github/workflows/phpunit.yaml | 42 +++++++++++++++++++++++ .gitignore | 5 +++ README.md | 28 +++++++++++++++ composer.json | 51 ++++++++++++++++++++++++++++ phpcs.xml | 14 ++++++++ phpstan.neon | 4 +++ phpunit.xml.dist | 29 ++++++++++++++++ src/PsrServerRequestHelper.php | 30 ++++++++++++++++ tests/ServerRequestTest.php | 30 ++++++++++++++++ 10 files changed, 269 insertions(+) create mode 100644 .github/workflows/code-analysis.yaml create mode 100644 .github/workflows/phpunit.yaml create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 phpcs.xml create mode 100644 phpstan.neon create mode 100644 phpunit.xml.dist create mode 100644 src/PsrServerRequestHelper.php create mode 100644 tests/ServerRequestTest.php diff --git a/.github/workflows/code-analysis.yaml b/.github/workflows/code-analysis.yaml new file mode 100644 index 0000000..d561bd2 --- /dev/null +++ b/.github/workflows/code-analysis.yaml @@ -0,0 +1,36 @@ +name: Static Code Analysis + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run php tests + run: composer run-script analyse \ No newline at end of file diff --git a/.github/workflows/phpunit.yaml b/.github/workflows/phpunit.yaml new file mode 100644 index 0000000..252196b --- /dev/null +++ b/.github/workflows/phpunit.yaml @@ -0,0 +1,42 @@ +name: PHPUnit tests + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run php tests + run: composer run-script phpunit-clover + + - name: Upload coverage results to Coveralls + env: + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} + run: | + vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32d55f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/vendor/ +.idea/ +.phpunit.cache/ +composer.lock +.phpunit.result.cache \ No newline at end of file diff --git a/README.md b/README.md index f73ce78..d864dd9 100644 --- a/README.md +++ b/README.md @@ -1 +1,29 @@ # PSR Server Request + +[![Static Code Analysis](https://github.com/paynl/psr-server-request/actions/workflows/code-analysis.yaml/badge.svg)](https://github.com/paynl/psr-server-request/actions/workflows/code-analysis.yaml) +[![PHPUnit tests](https://github.com/paynl/psr-server-request/actions/workflows/phpunit.yaml/badge.svg)](https://github.com/paynl/psr-server-request/actions/workflows/phpunit.yaml) +[![Coverage Status](https://coveralls.io/repos/github/paynl/psr-server-request/badge.svg?branch=feature/psr-server-request-package)](https://coveralls.io/github/paynl/psr-server-request?branch=feature/psr-server-request-package) + +This package adds a helper function to create a PSR Server Request from the Global PHP variables. + +## Requirements + +To install this package you need: + +* PHP >= 7.4; +* composer. + +## Installation + +``` +composer require paynl/psr-server-request +``` + +This will install the package into your project and autoload the helper function. + +## Usage + +To create a PSR Server Request from the PHP Globals you can use the following method: +```php +$serverRequest = create_psr_server_request(); +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fe059d3 --- /dev/null +++ b/composer.json @@ -0,0 +1,51 @@ +{ + "name": "paynl/psr-server-request", + "description": "Create a PSR Server Request from PHP Globals", + "type": "library", + "license": "proprietary", + "minimum-stability": "stable", + "support": { + "email" : "support@pay.nl" + }, + "authors": [ + { + "name": "Kevin Jansen", + "email": "k.jansen@pay.nl", + "role": "Maintainer" + }, + { + "name": "Wesley de Kanter", + "email": "wesley@pay.nl", + "role": "Maintainer" + } + ], + "require": { + "php": "^7.4 | ^8", + "nyholm/psr7": "^1.8", + "psr/http-message": "^2.0", + "nyholm/psr7-server": "^1.1" + }, + "require-dev": { + "phpstan/phpstan": "^1.10", + "squizlabs/php_codesniffer": "^3.7", + "phpunit/phpunit": "^9", + "php-coveralls/php-coveralls": "^2.7", + "phpunit/phpcov": "^8.2" + }, + "autoload": { + "files": [ + "src/PsrServerRequestHelper.php" + ] + }, + "scripts": { + "phpcs": "vendor/bin/phpcs --standard=phpcs.xml", + "phpcbf" : "vendor/bin/phpcbf", + "phpstan": "vendor/bin/phpstan", + "phpunit" : "vendor/bin/phpunit", + "phpunit-clover": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover build/logs/clover.xml", + "analyse": [ + "@phpcs", + "@phpstan" + ] + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..31b902b --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + src + + + diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..a1c637a --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: max + paths: + - src diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..1adfe0d --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + tests + + + + + + src + + + \ No newline at end of file diff --git a/src/PsrServerRequestHelper.php b/src/PsrServerRequestHelper.php new file mode 100644 index 0000000..ef76b61 --- /dev/null +++ b/src/PsrServerRequestHelper.php @@ -0,0 +1,30 @@ +fromGlobals(); + + // This is to mitigate a bug in the ServerRequestCreator where it sets the Host header twice. + // Once this is fixed within the ServerRequestCreator this mitigation becomes obsolete + // @see https://github.com/Nyholm/psr7-server/issues/48 + // @see https://github.com/Nyholm/psr7-server/pull/49 + if ($serverRequest->hasHeader('Host')) { + $serverRequest = $serverRequest->withHeader('Host', $serverRequest->getHeader('Host')[0]); + } + + return $serverRequest; + } +} diff --git a/tests/ServerRequestTest.php b/tests/ServerRequestTest.php new file mode 100644 index 0000000..178ac10 --- /dev/null +++ b/tests/ServerRequestTest.php @@ -0,0 +1,30 @@ +assertInstanceOf(RequestInterface::class, $request); + + return $request; + } + + /** @depends testItCanCreateARequest */ + public function testItHasOnlyOneHostHeader(RequestInterface $request): void + { + $this->assertCount(1, $request->getHeader('Host')); + $this->assertEquals('hostname.com', $request->getHeaderLine('Host')); + } +} \ No newline at end of file