Skip to content

Commit

Permalink
update to psr/http-factory 1.0.0, copyright 2018 for new files, decla…
Browse files Browse the repository at this point in the history
…re strict types, php 7.0
  • Loading branch information
danopz committed Sep 16, 2018
1 parent 66dbe0f commit 4a898f0
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 129 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 11 additions & 6 deletions src/Factory/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
22 changes: 17 additions & 5 deletions src/Factory/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@
* 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
{
/**
* 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;
}
}
56 changes: 23 additions & 33 deletions src/Factory/ServerRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
21 changes: 12 additions & 9 deletions src/Factory/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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));
}
Expand All @@ -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);
}
Expand Down
51 changes: 21 additions & 30 deletions src/Factory/UploadedFileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,55 @@
* 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
{
/**
* 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);
Expand Down
20 changes: 9 additions & 11 deletions src/Factory/UriFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Loading

0 comments on commit 4a898f0

Please sign in to comment.