Skip to content

Commit

Permalink
Stable PSR-17 (#2)
Browse files Browse the repository at this point in the history
* Use released version of PSR-17
* Nullable types require at least PHP 7.1
* Document changes
  • Loading branch information
tuupola authored Aug 2, 2018
1 parent 21f6362 commit e6b5ee2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 41 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ sudo: false
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly
Expand All @@ -19,9 +17,6 @@ matrix:
fast_finish: true
allow_failures:
- php: nightly
exclude:
- php: 5.6
env: PSR7_LIBRARY="nyholm/psr7"

before_script:
- travis_retry composer self-update
Expand Down
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.3.0 - 2017-07-16
## [0.4.0](https://github.com/tuupola/http-factory/compare/0.3.0...0.4.0) - 2018-08-02
### Added
- Support for the stable version of PSR-17.

### Changed
- PHP 7.1 is now minimal requirement.

## [0.3.0](https://github.com/tuupola/http-factory/compare/0.2.0...0.3.0) - 2017-07-16
### Added
- Factories for URI, server request and uploaded file

## 0.2.0 - 2017-07-15
## [0.2.0](https://github.com/tuupola/http-factory/compare/0.1.1...0.2.0) - 2017-07-15
### Added
- Unit and integration tests for request, response and stream.

## 0.1.1 - 2017-06-01
## [0.1.1](https://github.com/tuupola/http-factory/compare/0.1.0...0.1.1) - 2017-06-01
### Fixed
- Slim request factory was throwing errors.

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
"sort-packages": true
},
"require": {
"http-interop/http-factory": "^0.3.0"
"php": "^7.1",
"http-interop/http-factory-tests": "^0.5.0",
"psr/http-factory": "^1.0"
},
"require-dev": {
"http-interop/http-factory-tests": "^0.3.0",
"overtrue/phplint": "^1.0",
"phpunit/phpunit": "^5.7",
"phpunit/phpunit": "^6.5",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
Expand Down
5 changes: 3 additions & 2 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
use Slim\Http\Headers as SlimHeaders;
use Zend\Diactoros\Request as DiactorosRequest;

use Interop\Http\Factory\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\RequestFactoryInterface;

final class RequestFactory implements RequestFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createRequest($method, $uri)
public function createRequest(string $method, $uri): RequestInterface
{
if (class_exists(DiactorosRequest::class)) {
return new DiactorosRequest($uri, $method);
Expand Down
5 changes: 3 additions & 2 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
use Slim\Http\Response as SlimResponse;
use Zend\Diactoros\Response as DiactorosResponse;

use Interop\Http\Factory\ResponseFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;

final class ResponseFactory implements ResponseFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createResponse($code = 200)
public function createResponse(int $code = 200, string $reason = ""): ResponseInterface
{
if (class_exists(DiactorosResponse::class)) {
return new DiactorosResponse("php://memory", $code);
Expand Down
5 changes: 3 additions & 2 deletions src/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
use Zend\Diactoros\ServerRequest as DiactorosServerRequest;
use Zend\Diactoros\ServerRequestFactory as DiactorosServerRequestFactory;

use Interop\Http\Factory\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;

final class ServerRequestFactory implements ServerRequestFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createServerRequest($method, $uri)
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
if (class_exists(DiactorosServerRequest::class)) {
return new DiactorosServerRequest([], [], $uri, $method);
Expand Down
9 changes: 5 additions & 4 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
use Slim\Http\Stream as SlimStream;
use Zend\Diactoros\Stream as DiactorosStream;

use Interop\Http\Factory\StreamFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

class StreamFactory implements StreamFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createStream($content = "")
public function createStream(string $content = ""): StreamInterface
{
$resource = fopen("php://temp", "r+");
$stream = $this->createStreamFromResource($resource);
Expand All @@ -39,7 +40,7 @@ public function createStream($content = "")
/**
* {@inheritdoc}
*/
public function createStreamFromFile($filename, $mode = "r")
public function createStreamFromFile(string $filename, string $mode = "r"): StreamInterface
{
$resource = fopen($filename, $mode);
return $this->createStreamFromResource($resource);
Expand All @@ -48,7 +49,7 @@ public function createStreamFromFile($filename, $mode = "r")
/**
* {@inheritdoc}
*/
public function createStreamFromResource($resource)
public function createStreamFromResource($resource): StreamInterface
{
if (class_exists(DiactorosStream::class)) {
return new DiactorosStream($resource);
Expand Down
37 changes: 19 additions & 18 deletions src/UploadedFileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,29 @@
use Slim\Http\UploadedFile as SlimUploadedFile;
use Zend\Diactoros\UploadedFile as DiactorosUploadedFile;

use Interop\Http\Factory\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\StreamInterface;

final class UploadedFileFactory implements UploadedFileFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createUploadedFile(
$file,
$size = null,
$error = \UPLOAD_ERR_OK,
$clientFilename = null,
$clientMediaType = null
) {
StreamInterface $stream,
?int $size = null,
int $error = \UPLOAD_ERR_OK,
?string $clientFilename = null,
?string $clientMediaType = null
): UploadedFileInterface {
if ($size === null) {
if (is_string($file)) {
$size = filesize($file);
} else {
$stats = fstat($file);
$size = $stats['size'];
}
$size = $stream->getSize();
}

if (class_exists(DiactorosUploadedFile::class)) {
return new DiactorosUploadedFile(
$file,
$stream,
$size,
$error,
$clientFilename,
Expand All @@ -55,7 +52,7 @@ public function createUploadedFile(

if (class_exists(NyholmUploadedFile::class)) {
return new NyholmUploadedFile(
$file,
$stream,
$size,
$error,
$clientFilename,
Expand All @@ -64,8 +61,12 @@ public function createUploadedFile(
}

if (class_exists(SlimUploadedFile::class)) {
if (is_resource($file)) {
$file = stream_get_meta_data($file)["uri"];
$meta = $stream->getMetadata();
$file = $meta["uri"];

if ($file === "php://temp") {
$file = tempnam(sys_get_temp_dir(), "factory-test");
file_put_contents($file, (string) $stream);
}

return new SlimUploadedFile(
Expand All @@ -79,7 +80,7 @@ public function createUploadedFile(

if (class_exists(GuzzleUploadedFile::class)) {
return new GuzzleUploadedFile(
$file,
$stream,
$size,
$error,
$clientFilename,
Expand Down
5 changes: 3 additions & 2 deletions src/UriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
use Slim\Http\Uri as SlimUri;
use Zend\Diactoros\Uri as DiactorosUri;

use Interop\Http\Factory\UriFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;

final class UriFactory implements UriFactoryInterface
{
/**
* {@inheritdoc}
*/
public function createUri($uri = "")
public function createUri(string $uri = ""): UriInterface
{
if (class_exists(DiactorosUri::class)) {
return new DiactorosUri($uri);
Expand Down

0 comments on commit e6b5ee2

Please sign in to comment.