Skip to content

Commit

Permalink
Merge pull request #34 from sunrise-php/release/v3.1.0
Browse files Browse the repository at this point in the history
v3.1
  • Loading branch information
fenric authored Oct 30, 2024
2 parents 9914570 + 62fa8ba commit 946f98b
Show file tree
Hide file tree
Showing 53 changed files with 725 additions and 1,495 deletions.
90 changes: 63 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,72 @@
composer require sunrise/http-message
```

## Documentation navigation
## Documentation Navigation

- [Server request from global environment](#server-request-from-global-environment)
- [HTML and JSON responses](#html-and-json-responses)
- - [HTML response](#html-response)
- - [JSON response](#json-response)
- [Server Request from Global Environment](#server-request-from-global-environment)
- [Typed Messages](#typed-messages)
- [Streams](#streams)
- - [File stream](#file-stream)
- - [PHP input stream](#php-input-stream)
- - [PHP memory stream](#php-memory-stream)
- - [PHP temporary stream](#php-temporary-stream)
- - [Temporary file stream](#temporary-file-stream)
- - [File Stream](#file-stream)
- - [PHP Input Stream](#php-input-stream)
- - [PHP Memory Stream](#php-memory-stream)
- - [PHP Temporary Stream](#php-temporary-stream)
- - [Temporary File Stream](#temporary-file-stream)
- [PSR-7 and PSR-17](#psr-7-and-psr-17)
- [Exceptions](#exceptions)

## How to use
## How to Use

We highly recommend that you study [PSR-7](https://www.php-fig.org/psr/psr-7/) and [PSR-17](https://www.php-fig.org/psr/psr-17/) because only superficial examples will be presented below.

### Server request from global environment
### Server Request from Global Environment

```php
use Sunrise\Http\Message\ServerRequestFactory;

$request = ServerRequestFactory::fromGlobals();
```

### HTML and JSON responses
### Typed Messages

#### HTML response
#### JSON Request

```php
use Sunrise\Http\Message\Response\HtmlResponse;
use Sunrise\Http\Message\Request\JsonRequest;

/** @var $html string|Stringable */
/** @var $data mixed */

$response = new HtmlResponse(200, $html);
$request = new JsonRequest('GET', '/', $data);
```

You can also specify [encoding flags](https://www.php.net/manual/en/json.constants.php#constant.json-hex-tag) and maximum nesting depth like below:

```php
$request = new JsonRequest('GET', '/', $data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE, 512);
```

#### URL Encoded Request

```php
use Sunrise\Http\Message\Request\UrlEncodedRequest;

/** @var $data mixed */

$request = new UrlEncodedRequest('GET', '/', $data);
```

#### JSON response
You can also specify [encoding type](https://www.php.net/manual/ru/url.constants.php#constant.php-query-rfc1738) like below:

```php
use Sunrise\Http\Message\Request\UrlEncodedRequest;

$encodingType = UrlEncodedRequest::ENCODING_TYPE_RFC1738;
// or
$encodingType = UrlEncodedRequest::ENCODING_TYPE_RFC3986;

$request = new UrlEncodedRequest('GET', '/', $data, $encodingType);
```

#### JSON Response

```php
use Sunrise\Http\Message\Response\JsonResponse;
Expand All @@ -70,17 +96,27 @@ You can also specify [encoding flags](https://www.php.net/manual/en/json.constan
$response = new JsonResponse(200, $data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE, 512);
```

#### HTML Response

```php
use Sunrise\Http\Message\Response\HtmlResponse;

/** @var $html string|Stringable */

$response = new HtmlResponse(200, $html);
```

### Streams

#### File stream
#### File Stream

```php
use Sunrise\Http\Message\Stream\FileStream;

$fileStream = new FileStream('/folder/file', 'r+b');
```

#### PHP input stream
#### PHP Input Stream

More details about the stream at the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).

Expand All @@ -90,7 +126,7 @@ use Sunrise\Http\Message\Stream\PhpInputStream;
$inputStream = new PhpInputStream();
```

#### PHP memory stream
#### PHP Memory Stream

More details about the stream at the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.memory).

Expand All @@ -100,7 +136,7 @@ use Sunrise\Http\Message\Stream\PhpMemoryStream;
$memoryStream = new PhpMemoryStream('r+b');
```

#### PHP temporary stream
#### PHP Temporary Stream

More details about the stream at the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.memory).

Expand All @@ -110,7 +146,7 @@ use Sunrise\Http\Message\Stream\PhpTempStream;
$tempStream = new PhpTempStream('r+b');
```

You can also specify the memory limit, when the limit is reached, PHP will start using the temporary file instead of memory.
You can also specify the memory limit; when the limit is reached, PHP will start using the temporary file instead of memory.

> Please note that the default memory limit is 2MB.
Expand All @@ -120,7 +156,7 @@ $maxMemory = 1e+6; // 1MB
$tempStream = new PhpTempStream('r+b', $maxMemory);
```

#### Temporary file stream
#### Temporary File Stream

More details about the temporary file behaviour at [the official page](https://www.php.net/manual/en/function.tmpfile).

Expand Down Expand Up @@ -168,7 +204,7 @@ The following classes implement PSR-17:

### Exceptions

Any exceptions of this package can be caught through the interface:
Any exceptions from this package can be caught through the interface:

```php
use Sunrise\Http\Message\Exception\ExceptionInterface;
Expand All @@ -182,13 +218,13 @@ try {

---

## Test run
## Test Run

```bash
composer test
```

## Useful links
## Useful Links

- https://tools.ietf.org/html/rfc7230
- https://www.php-fig.org/psr/psr-7/
Expand Down
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
"psr/http-message": "^1.0"
},
"require-dev": {
"sunrise/coding-standard": "~1.0.0",
"phpunit/phpunit": "~9.5.0",
"php-http/psr7-integration-tests": "^1.1"
"php-http/psr7-integration-tests": "^1.4",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^9.6",
"sunrise/coding-standard": "^1.0",
"vimeo/psalm": "^5.26"
},
"autoload": {
"files": [
Expand All @@ -59,9 +61,9 @@
},
"scripts": {
"test": [
"phpcs",
"phpcs --colors",
"psalm --no-cache",
"phpstan analyse src --level=9",
"phpstan analyse src --level=9 --memory-limit=-1",
"XDEBUG_MODE=coverage phpunit --coverage-text --colors=always"
],
"build": [
Expand All @@ -76,5 +78,8 @@
"sunrise/http-server-request": "*",
"sunrise/stream": "*",
"sunrise/uri": "*"
},
"config": {
"sort-packages": true
}
}
31 changes: 5 additions & 26 deletions functions/server_request_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,14 @@

namespace Sunrise\Http\Message;

/**
* Import classes
*/
use Sunrise\Http\Message\Stream\FileStream;

/**
* Import functions
*/
use function is_array;

/**
* Import constants
*/
use const UPLOAD_ERR_OK;
use const UPLOAD_ERR_NO_FILE;

/**
* Gets the request's uploaded files
*
* Please note that unsent files will not be handled,
* also note that if a file fails to upload successfully,
* a stream will not be created for it.
*
* @param array|null $files
*
* @return array
*
* @link http://php.net/manual/en/reserved.variables.files.php
* @link https://www.php.net/manual/ru/features.file-upload.post-method.php
* @link https://www.php.net/manual/ru/features.file-upload.multiple.php
Expand All @@ -49,22 +30,20 @@ function server_request_files(?array $files = null): array

$walker = static function ($path, $size, $error, $name, $type) use (&$walker) {
if (!is_array($path)) {
// It makes no sense to create a stream
// if the file has not been successfully uploaded.
$stream = UPLOAD_ERR_OK <> $error ? null : new FileStream($path, 'rb');
$stream = $error === UPLOAD_ERR_OK ? new FileStream($path, 'rb') : null;

return new UploadedFile($stream, $size, $error, $name, $type);
}

$result = [];
foreach ($path as $key => $_) {
if (UPLOAD_ERR_NO_FILE <> $error[$key]) {
if ($error[$key] !== UPLOAD_ERR_NO_FILE) {
$result[$key] = $walker(
$path[$key],
$size[$key],
$error[$key],
$name[$key],
$type[$key]
$type[$key],
);
}
}
Expand All @@ -74,13 +53,13 @@ function server_request_files(?array $files = null): array

$result = [];
foreach ($files as $key => $file) {
if (UPLOAD_ERR_NO_FILE <> $file['error']) {
if ($file['error'] !== UPLOAD_ERR_NO_FILE) {
$result[$key] = $walker(
$file['tmp_name'],
$file['size'],
$file['error'],
$file['name'],
$file['type']
$file['type'],
);
}
}
Expand Down
9 changes: 1 addition & 8 deletions functions/server_request_headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,13 @@

namespace Sunrise\Http\Message;

/**
* Import functions
*/
use function strncmp;
use function strtolower;
use function strtr;
use function substr;
use function ucwords;

/**
* Gets the request headers
*
* @param array|null $serverParams
*
* @return array<string, string>
*
* @link http://php.net/manual/en/reserved.variables.server.php
Expand All @@ -46,7 +39,7 @@ function server_request_headers(?array $serverParams = null): array

$result = [];
foreach ($serverParams as $key => $value) {
if (0 <> strncmp('HTTP_', $key, 5)) {
if (strncmp('HTTP_', $key, 5) !== 0) {
continue;
}

Expand Down
14 changes: 1 addition & 13 deletions functions/server_request_method.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,11 @@
namespace Sunrise\Http\Message;

/**
* Import classes
*/
use Fig\Http\Message\RequestMethodInterface;

/**
* Gets the request method
*
* @param array|null $serverParams
*
* @return string
*
* @link http://php.net/manual/en/reserved.variables.server.php
* @link https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.12
*/
function server_request_method(?array $serverParams = null): string
{
$serverParams ??= $_SERVER;

return $serverParams['REQUEST_METHOD'] ?? RequestMethodInterface::METHOD_GET;
return $serverParams['REQUEST_METHOD'] ?? 'GET';
}
10 changes: 0 additions & 10 deletions functions/server_request_protocol_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,10 @@

namespace Sunrise\Http\Message;

/**
* Import functions
*/
use function sprintf;
use function sscanf;

/**
* Gets the request's protocol version
*
* @param array|null $serverParams
*
* @return string
*
* @link http://php.net/manual/en/reserved.variables.server.php
* @link https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.16
*/
function server_request_protocol_version(?array $serverParams = null): string
Expand Down
Loading

0 comments on commit 946f98b

Please sign in to comment.