Skip to content

Commit 46ddd56

Browse files
committed
Rename and add dependency httpsoft/http-message
1 parent ac45cfe commit 46ddd56

24 files changed

+975
-1362
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# HTTP Request Change Log
1+
# HTTP Server Request Change Log
22

33
## 1.0.0 - under development
44

README.md

Lines changed: 40 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# HTTP Request
1+
# HTTP Server Request
22

33
[![License](https://poser.pugx.org/httpsoft/http-request/license)](https://packagist.org/packages/httpsoft/http-request)
44
[![Latest Stable Version](https://poser.pugx.org/httpsoft/http-request/v)](https://packagist.org/packages/httpsoft/http-request)
@@ -7,90 +7,66 @@
77
[![Scrutinizer Code Coverage](https://scrutinizer-ci.com/g/httpsoft/http-request/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/httpsoft/http-request/?branch=master)
88
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/httpsoft/http-request/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/httpsoft/http-request/?branch=master)
99

10-
This package implements [Psr\Http\Message\RequestInterface](https://github.com/php-fig/http-message/blob/master/src/RequestInterface.php) and [Psr\Http\Message\ServerRequestInterface](https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php) from [PSR-7 HTTP Message](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md) in accordance with the [RFC 7230](https://tools.ietf.org/html/rfc7230) and [RFC 7231](https://tools.ietf.org/html/rfc7231) specifications.
10+
This package makes it easy and flexible to create PSR-7 components [ServerRequest](https://github.com/php-fig/http-message/blob/master/src/ServerRequestInterface.php) and [UploadedFile](https://github.com/php-fig/http-message/blob/master/src/UploadedFileInterface.php).
11+
12+
Depends on the [httpsoft/http-message](https://github.com/httpsoft/http-message) package.
1113

1214
## Documentation
1315

14-
* [In English language](https://httpsoft.org/docs/request).
15-
* [In Russian language](https://httpsoft.org/ru/docs/request).
16+
* [In English language](https://httpsoft.org/docs/server-request).
17+
* [In Russian language](https://httpsoft.org/ru/docs/server-request).
1618

1719
## Installation
1820

1921
This package requires PHP version 7.4 or later.
2022

2123
```
22-
composer require httpsoft/http-request
24+
composer require httpsoft/http-server-request
2325
```
2426

25-
## Usage Request
27+
## Usage ServerRequestCreator
2628

2729
```php
28-
use HttpSoft\Request\Request;
29-
use HttpSoft\Request\RequestFactory;
30-
31-
$request = new Request('POST', 'http://example.com', ['Content-Type' => 'text/html'], 'data://,Content', '2');
32-
// Or using the factory:
33-
$request = RequestFactory::create(
34-
'POST', // Request method
35-
'https://example.com', // Request URI
36-
['Content-Type' => 'text/html' /* Other headers */],
37-
'data://,Content', // HTTP message body
38-
'2' // HTTP protocol version
39-
);
40-
41-
$request->getMethod(); // 'POST'
42-
$request->getProtocolVersion(); // '2'
43-
$request->getBody()->getContents(); // 'Content'
44-
(string) $request->getUri(); // 'https://example.com/path'
45-
$request->getHeaders(); // ['Host' => ['example.com'], 'Content-Type' => ['text/html']]
46-
```
30+
use HttpSoft\ServerRequest\ServerRequestCreator;
4731

48-
## Usage ServerRequest
49-
50-
```php
51-
use HttpSoft\Request\ServerRequest;
52-
use HttpSoft\Request\ServerRequestFactory;
53-
54-
$request = new ServerRequest(
55-
$_SERVER,
56-
$_FILES,
57-
$_COOKIE,
58-
$_GET,
59-
$_POST,
60-
'GET', // Request method
61-
'https://example.com', // Request URI
62-
[/* Headers */],
63-
'php://input', // HTTP message body
64-
'2' // HTTP protocol version
65-
);
66-
// Or using the factory (all necessary data will be received automatically):
67-
$request = ServerRequestFactory::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);
32+
// All necessary data will be received automatically:
33+
$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST);
6834
// equivalently to:
69-
$request = ServerRequestFactory::createFromGlobals();
35+
$request = ServerRequestCreator::createFromGlobals();
7036
// equivalently to:
71-
$request = ServerRequestFactory::create();
72-
73-
$request->getMethod(); // 'GET'
74-
$request->getProtocolVersion(); // '2'
75-
$request->getBody()->getContents(); // ''
76-
(string) $request->getUri(); // 'https://example.com'
77-
$request->getHeaders(); // ['Host' => ['example.com']]
78-
$request->getServerParams(); // $_SERVER
79-
$request->getUploadedFiles(); // $_FILES
80-
$request->getCookieParams(); // $_COOKIE
81-
$request->getQueryParams(); // $_GET
82-
$request->getParsedBody(); // $_POST
83-
$request->getAttributes(); // []
37+
$request = ServerRequestCreator::create();
8438
```
8539

86-
By default [HttpSoft\Request\SapiNormalizer](https://github.com/httpsoft/http-request/blob/master/src/SapiNormalizer.php) is used for normalization of server parameters. You can use your own server parameters normalizer, for this you need to implement an [HttpSoft\Request\ServerNormalizerInterface](https://github.com/httpsoft/http-request/blob/master/src/ServerNormalizerInterface.php) interface.
40+
By default [HttpSoft\ServerRequest\SapiNormalizer](https://github.com/httpsoft/http-server-request/blob/master/src/SapiNormalizer.php) is used for normalization of server parameters. You can use your own server parameters normalizer, for this you need to implement an [HttpSoft\ServerRequest\ServerNormalizerInterface](https://github.com/httpsoft/http-server-request/blob/master/src/ServerNormalizerInterface.php) interface.
8741

8842
```php
8943
$normalizer = new YouCustomServerNormalizer();
9044

91-
$request = ServerRequestFactory::create($normalizer);
45+
$request = ServerRequestCreator::create($normalizer);
9246
// equivalently to:
93-
$request = ServerRequestFactory::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST, $normalizer);
94-
// or with custom superglobals
95-
$request = ServerRequestFactory::createFromGlobals($server, $files, $cookie, $get, $post, $normalizer);
47+
$request = ServerRequestCreator::createFromGlobals($_SERVER, $_FILES, $_COOKIE, $_GET, $_POST, $normalizer);
48+
// or with custom superglobals:
49+
$request = ServerRequestCreator::createFromGlobals($server, $files, $cookie, $get, $post, $normalizer);
50+
```
51+
52+
## Usage UploadedFileCreator
53+
54+
```php
55+
use HttpSoft\ServerRequest\UploadedFileCreator;
56+
57+
/** @var StreamInterface|string|resource $streamOrFile */
58+
$uploadedFile = UploadedFileCreator::create($streamOrFile, 1024, UPLOAD_ERR_OK, 'file.txt', 'text/plain');
59+
60+
// Create a new `HttpSoft\UploadedFile\UploadedFile` instance from array (the item `$_FILES`)
61+
$uploadedFile = UploadedFileCreator::createFromArray([
62+
'name' => 'filename.jpg', // optional
63+
'type' => 'image/jpeg', // optional
64+
'tmp_name' => '/tmp/php/php6hst32',
65+
'error' => 0, // UPLOAD_ERR_OK
66+
'size' => 98174,
67+
]);
68+
69+
// Normalizes the superglobal structure and converts each array
70+
// value to an instance of `Psr\Http\Message\UploadedFileInterface`.
71+
$uploadedFiles = UploadedFileCreator::createFromGlobals($_FILES);
9672
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "httpsoft/http-server-request",
3-
"description": "Functionality for creating a PSR-7 ServerRequest",
3+
"description": "Infrastructure for creating PSR-7 ServerRequest and UploadedFile",
44
"keywords": ["php", "http", "http-server-request", "psr-7", "http-message"],
55
"homepage": "https://httpsoft.org/",
66
"type": "library",

phpcs.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
2-
<ruleset name="HttpSoft Request Coding Standard">
3-
<description>HttpSoft Request Coding Standard</description>
2+
<ruleset name="HttpSoft Server Request Coding Standard">
3+
<description>HttpSoft Server Request Coding Standard</description>
44

55
<!-- display progress -->
66
<arg value="p"/>

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</php>
2020

2121
<testsuites>
22-
<testsuite name="HttpSoft Request Test Suite">
22+
<testsuite name="HttpSoft Server Request Test Suite">
2323
<directory>./tests/</directory>
2424
</testsuite>
2525
</testsuites>

psalm.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
</projectFiles>
1515

1616
<issueHandlers>
17-
<DocblockTypeContradiction errorLevel="info" />
18-
<LessSpecificImplementedReturnType errorLevel="info" />
19-
<MixedArgumentTypeCoercion errorLevel="info" />
17+
<InvalidReturnStatement errorLevel="info" />
18+
<InvalidReturnType errorLevel="info" />
19+
<MixedArgument errorLevel="info" />
2020
<MixedAssignment errorLevel="info" />
21-
<MixedPropertyTypeCoercion errorLevel="info" />
22-
<PropertyNotSetInConstructor errorLevel="info" />
23-
<RedundantConditionGivenDocblockType errorLevel="info" />
21+
<MixedArgumentTypeCoercion errorLevel="info" />
22+
<PossiblyNullArgument errorLevel="info" />
2423
</issueHandlers>
2524
</psalm>

src/PhpInputStream.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HttpSoft\ServerRequest;
6+
7+
use HttpSoft\Message\StreamTrait;
8+
use Psr\Http\Message\StreamInterface;
9+
10+
final class PhpInputStream implements StreamInterface
11+
{
12+
use StreamTrait {
13+
read as private readInternal;
14+
getContents as private getContentsInternal;
15+
}
16+
17+
/**
18+
* @var string
19+
*/
20+
private string $cache = '';
21+
22+
/**
23+
* @var bool
24+
*/
25+
private bool $isEof = false;
26+
27+
/**
28+
* @param string|resource $stream
29+
*/
30+
public function __construct($stream = 'php://input')
31+
{
32+
$this->init($stream, 'r');
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function __toString(): string
39+
{
40+
if (!$this->isEof) {
41+
$this->getContents();
42+
}
43+
44+
return $this->cache;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function isWritable(): bool
51+
{
52+
return false;
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function read($length): string
59+
{
60+
$result = $this->readInternal($length);
61+
62+
if (!$this->isEof) {
63+
$this->cache .= $result;
64+
}
65+
66+
if ($this->eof()) {
67+
$this->isEof = true;
68+
}
69+
70+
return $result;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getContents(): string
77+
{
78+
if ($this->isEof) {
79+
return $this->cache;
80+
}
81+
82+
$result = $this->getContentsInternal();
83+
$this->cache .= $result;
84+
$this->isEof = true;
85+
86+
return $result;
87+
}
88+
}

src/Request.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/RequestFactory.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)