From 4a898f08d10f9c2fa1ac1791debd14069de0071f Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Tue, 31 Jul 2018 22:05:51 +0200 Subject: [PATCH] update to psr/http-factory 1.0.0, copyright 2018 for new files, declare strict types, php 7.0 --- composer.json | 4 +- src/Factory/RequestFactory.php | 17 ++++--- src/Factory/ResponseFactory.php | 22 +++++++-- src/Factory/ServerRequestFactory.php | 56 +++++++++------------- src/Factory/StreamFactory.php | 21 ++++---- src/Factory/UploadedFileFactory.php | 51 ++++++++------------ src/Factory/UriFactory.php | 20 ++++---- tests/Factory/RequestFactoryTest.php | 2 +- tests/Factory/ResponseFactoryTest.php | 2 +- tests/Factory/ServerRequestFactoryTest.php | 20 +------- tests/Factory/StreamFactoryTest.php | 2 +- tests/Factory/UploadedFileFactoryTest.php | 17 ++++++- tests/Factory/UriFactoryTest.php | 12 +---- 13 files changed, 117 insertions(+), 129 deletions(-) diff --git a/composer.json b/composer.json index 69655a8..cfc8a51 100644 --- a/composer.json +++ b/composer.json @@ -25,14 +25,14 @@ "require": { "php": ">=7.0.0", "psr/http-message": "^1.0", - "http-interop/http-factory": "0.3.0" + "psr/http-factory": "^1.0" }, "require-dev": { "squizlabs/php_codesniffer": "^2.5", "phpunit/phpunit": "^6.0|^7.0", "php-http/psr7-integration-tests": "dev-master", "phpstan/phpstan": "^0.9", - "http-interop/http-factory-tests": "dev-master" + "http-interop/http-factory-tests": "^0.5.0" }, "provide": { "psr/http-message-implementation": "1.0", diff --git a/src/Factory/RequestFactory.php b/src/Factory/RequestFactory.php index a033353..3382a5b 100644 --- a/src/Factory/RequestFactory.php +++ b/src/Factory/RequestFactory.php @@ -3,13 +3,16 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ + +declare(strict_types=1); + namespace Slim\Http\Factory; -use Interop\Http\Factory\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\UriInterface; use Slim\Http\Headers; use Slim\Http\Request; @@ -19,17 +22,19 @@ class RequestFactory implements RequestFactoryInterface /** * Create a new request. * - * @param string $method - * @param UriInterface|string $uri + * @param string $method The HTTP method associated with the request. + * @param UriInterface|string $uri The URI associated with the request. If + * the value is a string, the factory MUST create a UriInterface + * instance based on it. * * @return RequestInterface */ - public function createRequest($method, $uri) + public function createRequest(string $method, $uri): RequestInterface { if (is_string($uri)) { $uri = (new UriFactory())->createUri($uri); } elseif (!$uri instanceof UriInterface) { - throw new \InvalidArgumentException(); + throw new \InvalidArgumentException('URI must either be string or instance of ' . UriInterface::class); } $body = (new StreamFactory())->createStream(); diff --git a/src/Factory/ResponseFactory.php b/src/Factory/ResponseFactory.php index 8d7c393..1fbdefe 100644 --- a/src/Factory/ResponseFactory.php +++ b/src/Factory/ResponseFactory.php @@ -3,13 +3,16 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ + +declare(strict_types=1); + namespace Slim\Http\Factory; -use Interop\Http\Factory\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ResponseFactoryInterface; use Slim\Http\Response; class ResponseFactory implements ResponseFactoryInterface @@ -17,12 +20,21 @@ class ResponseFactory implements ResponseFactoryInterface /** * Create a new response. * - * @param integer $code HTTP status code + * @param int $code HTTP status code; defaults to 200 + * @param string $reasonPhrase Reason phrase to associate with status code + * in generated response; if none is provided implementations MAY use + * the defaults as suggested in the HTTP specification. * * @return ResponseInterface */ - public function createResponse($code = 200) + public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface { - return new Response($code); + $res = new Response($code); + + if ($reasonPhrase !== '') { + $res = $res->withStatus($code, $reasonPhrase); + } + + return $res; } } diff --git a/src/Factory/ServerRequestFactory.php b/src/Factory/ServerRequestFactory.php index c878c30..3c02209 100644 --- a/src/Factory/ServerRequestFactory.php +++ b/src/Factory/ServerRequestFactory.php @@ -3,13 +3,16 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ + +declare(strict_types=1); + namespace Slim\Http\Factory; -use Interop\Http\Factory\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\UriInterface; use Slim\Http\Cookies; use Slim\Http\Headers; @@ -20,49 +23,36 @@ class ServerRequestFactory implements ServerRequestFactoryInterface /** * Create a new server request. * - * @param string $method - * @param UriInterface|string $uri + * Note that server-params are taken precisely as given - no parsing/processing + * of the given values is performed, and, in particular, no attempt is made to + * determine the HTTP method or URI, which must be provided explicitly. + * + * @param string $method The HTTP method associated with the request. + * @param UriInterface|string $uri The URI associated with the request. If + * the value is a string, the factory MUST create a UriInterface + * instance based on it. + * @param array $serverParams Array of SAPI parameters with which to seed + * the generated request instance. * * @return ServerRequestInterface */ - public function createServerRequest($method, $uri) + public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface { if (is_string($uri)) { $uri = (new UriFactory())->createUri($uri); } elseif (!$uri instanceof UriInterface) { - throw new \InvalidArgumentException(); + throw new \InvalidArgumentException('URI must either be string or instance of ' . UriInterface::class); } $body = (new StreamFactory())->createStream(); + $headers = new Headers(); + $cookies = []; - return new Request($method, $uri, new Headers(), [], [], $body); - } - - /** - * Create a new server request from server variables. - * - * @param array $server Typically $_SERVER or similar structure. - * - * @return ServerRequestInterface - * - * @throws \InvalidArgumentException - * If no valid method or URI can be determined. - */ - public function createServerRequestFromArray(array $server) - { - if (!isset($server['REQUEST_METHOD'])) { - throw new \InvalidArgumentException(); + if (!empty($serverParams)) { + $headers = Headers::createFromGlobals($serverParams); + $cookies = Cookies::parseHeader($headers->get('Cookie', [])); } - $method = $server['REQUEST_METHOD']; - $uri = (new UriFactory())->createFromGlobals($server); - $headers = Headers::createFromGlobals($server); - $cookies = Cookies::parseHeader($headers->get('Cookie', [])); - $serverParams = $server; - $body = (new StreamFactory())->createStream(); - - $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); - - return $request; + return new Request($method, $uri, $headers, $cookies, $serverParams, $body); } } diff --git a/src/Factory/StreamFactory.php b/src/Factory/StreamFactory.php index 8b5dacf..cd84f13 100644 --- a/src/Factory/StreamFactory.php +++ b/src/Factory/StreamFactory.php @@ -3,13 +3,16 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ + +declare(strict_types=1); + namespace Slim\Http\Factory; -use Interop\Http\Factory\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; +use Psr\Http\Message\StreamFactoryInterface; use Slim\Http\Stream; class StreamFactory implements StreamFactoryInterface @@ -19,11 +22,11 @@ class StreamFactory implements StreamFactoryInterface * * The stream SHOULD be created with a temporary resource. * - * @param string $content + * @param string $content String content with which to populate the stream. * * @return StreamInterface */ - public function createStream($content = '') + public function createStream(string $content = ''): StreamInterface { $resource = fopen('php://temp', 'r+'); fwrite($resource, $content); @@ -40,12 +43,12 @@ public function createStream($content = '') * * The `$filename` MAY be any string supported by `fopen()`. * - * @param string $filename - * @param string $mode + * @param string $filename Filename or stream URI to use as basis of stream. + * @param string $mode Mode with which to open the underlying filename/stream. * * @return StreamInterface */ - public function createStreamFromFile($filename, $mode = 'r') + public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface { return $this->createStreamFromResource(fopen($filename, $mode)); } @@ -55,11 +58,11 @@ public function createStreamFromFile($filename, $mode = 'r') * * The stream MUST be readable and may be writable. * - * @param resource $resource + * @param resource $resource PHP resource to use as basis of stream. * * @return StreamInterface */ - public function createStreamFromResource($resource) + public function createStreamFromResource($resource): StreamInterface { return new Stream($resource); } diff --git a/src/Factory/UploadedFileFactory.php b/src/Factory/UploadedFileFactory.php index ec52a76..78a78a7 100644 --- a/src/Factory/UploadedFileFactory.php +++ b/src/Factory/UploadedFileFactory.php @@ -3,13 +3,17 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ + +declare(strict_types=1); + namespace Slim\Http\Factory; -use Interop\Http\Factory\UploadedFileFactoryInterface; +use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UploadedFileInterface; +use Psr\Http\Message\UploadedFileFactoryInterface; use Slim\Http\UploadedFile; class UploadedFileFactory implements UploadedFileFactoryInterface @@ -17,50 +21,37 @@ class UploadedFileFactory implements UploadedFileFactoryInterface /** * Create a new uploaded file. * - * If a string is used to create the file, a temporary resource will be - * created with the content of the string. - * * If a size is not provided it will be determined by checking the size of * the file. * * @see http://php.net/manual/features.file-upload.post-method.php * @see http://php.net/manual/features.file-upload.errors.php * - * @param string|resource $file - * @param integer $size in bytes - * @param integer $error PHP file upload error - * @param string $clientFilename - * @param string $clientMediaType + * @param StreamInterface $stream Underlying stream representing the + * uploaded file content. + * @param int $size in bytes + * @param int $error PHP file upload error + * @param string $clientFilename Filename as provided by the client, if any. + * @param string $clientMediaType Media type as provided by the client, if any. * * @return UploadedFileInterface * - * @throws \InvalidArgumentException - * If the file resource is not readable. + * @throws \InvalidArgumentException If the file resource is not readable. */ public function createUploadedFile( - $file, - $size = null, - $error = \UPLOAD_ERR_OK, - $clientFilename = null, - $clientMediaType = null - ) { - if (is_resource($file)) { - $meta = stream_get_meta_data($file); - - if (!isset($meta['uri'])) { - throw new \InvalidArgumentException('Stream is not readable'); - } - - $file = $meta['uri']; - } elseif (!is_string($file)) { - throw new \InvalidArgumentException('File must be string or resource'); - } + StreamInterface $stream, + int $size = null, + int $error = \UPLOAD_ERR_OK, + string $clientFilename = null, + string $clientMediaType = null + ): UploadedFileInterface { + $file = $stream->getMetadata('uri'); if (!is_readable($file)) { throw new \InvalidArgumentException('File is not readable'); } if (!isset($size)) { - $size = filesize($file); + $size = $stream->getSize(); } return new UploadedFile($file, $clientFilename, $clientMediaType, $size, $error); diff --git a/src/Factory/UriFactory.php b/src/Factory/UriFactory.php index 88b29ec..aff4fb8 100644 --- a/src/Factory/UriFactory.php +++ b/src/Factory/UriFactory.php @@ -3,13 +3,16 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ + +declare(strict_types=1); + namespace Slim\Http\Factory; -use Interop\Http\Factory\UriFactoryInterface; use Psr\Http\Message\UriInterface; +use Psr\Http\Message\UriFactoryInterface; use Slim\Http\Collection; use Slim\Http\Uri; @@ -22,15 +25,10 @@ class UriFactory implements UriFactoryInterface * * @return UriInterface * - * @throws \InvalidArgumentException - * If the given URI cannot be parsed. + * @throws \InvalidArgumentException If the given URI cannot be parsed. */ - public function createUri($uri = '') + public function createUri(string $uri = ''): UriInterface { - if (!is_string($uri) && !method_exists($uri, '__toString')) { - throw new \InvalidArgumentException('Uri must be a string'); - } - $parts = parse_url($uri); if ($parts === false) { @@ -83,12 +81,12 @@ public function createFromGlobals(array $globals) $host = $matches[1]; if (isset($matches[2])) { - $port = (int) substr($matches[2], 1); + $port = (int)substr($matches[2], 1); } } else { $pos = strpos($host, ':'); if ($pos !== false) { - $port = (int) substr($host, $pos + 1); + $port = (int)substr($host, $pos + 1); $host = strstr($host, ':', true); } } diff --git a/tests/Factory/RequestFactoryTest.php b/tests/Factory/RequestFactoryTest.php index 6489611..600cf45 100644 --- a/tests/Factory/RequestFactoryTest.php +++ b/tests/Factory/RequestFactoryTest.php @@ -3,7 +3,7 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ namespace Slim\Tests\Http\Factory; diff --git a/tests/Factory/ResponseFactoryTest.php b/tests/Factory/ResponseFactoryTest.php index ffc56fb..45d50ac 100644 --- a/tests/Factory/ResponseFactoryTest.php +++ b/tests/Factory/ResponseFactoryTest.php @@ -3,7 +3,7 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ namespace Slim\Tests\Http\Factory; diff --git a/tests/Factory/ServerRequestFactoryTest.php b/tests/Factory/ServerRequestFactoryTest.php index d3e7e10..23a1b6e 100644 --- a/tests/Factory/ServerRequestFactoryTest.php +++ b/tests/Factory/ServerRequestFactoryTest.php @@ -3,7 +3,7 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ namespace Slim\Tests\Http\Factory; @@ -32,22 +32,6 @@ protected function createUri($uri) return (new UriFactory())->createUri($uri); } - /** - * @covers \Slim\Http\Factory\ServerRequestFactory::createServerRequestFromArray - */ - public function testCreateFromGlobals() - { - $env = Environment::mock([ - 'SCRIPT_NAME' => '/index.php', - 'REQUEST_URI' => '/foo', - 'REQUEST_METHOD' => 'POST', - ]); - - $request = $this->createServerRequestFactory()->createServerRequestFromArray($env); - $this->assertEquals('POST', $request->getMethod()); - $this->assertEquals($env, $request->getServerParams()); - } - /******************************************************************************* * Protocol ******************************************************************************/ @@ -55,7 +39,7 @@ public function testCreateFromGlobals() public function testGetProtocolVersion() { $env = Environment::mock(['SERVER_PROTOCOL' => 'HTTP/1.0']); - $request = $this->createServerRequestFactory()->createServerRequestFromArray($env); + $request = $this->createServerRequestFactory()->createServerRequest('GET', '', $env); $this->assertEquals('1.0', $request->getProtocolVersion()); } diff --git a/tests/Factory/StreamFactoryTest.php b/tests/Factory/StreamFactoryTest.php index 12bb3d3..efda7dc 100644 --- a/tests/Factory/StreamFactoryTest.php +++ b/tests/Factory/StreamFactoryTest.php @@ -3,7 +3,7 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ namespace Slim\Tests\Http\Factory; diff --git a/tests/Factory/UploadedFileFactoryTest.php b/tests/Factory/UploadedFileFactoryTest.php index f4556bf..7760766 100644 --- a/tests/Factory/UploadedFileFactoryTest.php +++ b/tests/Factory/UploadedFileFactoryTest.php @@ -3,12 +3,14 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ namespace Slim\Tests\Http\Factory; use Interop\Http\Factory\UploadedFileFactoryTestCase; +use Psr\Http\Message\StreamInterface; +use Slim\Http\Factory\StreamFactory; use Slim\Http\Factory\UploadedFileFactory; class UploadedFileFactoryTest extends UploadedFileFactoryTestCase @@ -20,4 +22,17 @@ protected function createUploadedFileFactory() { return new UploadedFileFactory(); } + + /** + * @return StreamInterface + */ + protected function createStream($content) + { + $file = tempnam(sys_get_temp_dir(), 'Slim_Http_UploadedFileTest_'); + $resource = fopen($file, 'r+'); + fwrite($resource, $content); + rewind($resource); + + return (new StreamFactory())->createStreamFromResource($resource); + } } diff --git a/tests/Factory/UriFactoryTest.php b/tests/Factory/UriFactoryTest.php index 43d4b38..87efc67 100644 --- a/tests/Factory/UriFactoryTest.php +++ b/tests/Factory/UriFactoryTest.php @@ -3,7 +3,7 @@ * Slim Framework (https://slimframework.com) * * @link https://github.com/slimphp/Slim-Http - * @copyright Copyright (c) 2011-2017 Josh Lockhart + * @copyright Copyright (c) 2011-2018 Josh Lockhart * @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE (MIT License) */ namespace Slim\Tests\Http\Factory; @@ -85,16 +85,6 @@ public function testCreateFromString() $this->assertEquals('abc=123', $uri->getQuery()); } - /** - * @covers \Slim\Http\Factory\UriFactory::createUri - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Uri must be a string - */ - public function testCreateFromStringWithInvalidType() - { - $this->createUriFactory()->createUri(['https://example.com:8080/foo/bar?abc=123']); - } - public function testCreateFromGlobals() { $globals = Environment::mock([