Skip to content

Commit

Permalink
psr17 http-factory, port of slimphp/Slim-Http#27 - resolves slimphp#14
Browse files Browse the repository at this point in the history
  • Loading branch information
danopz authored and akrabat committed Nov 2, 2018
1 parent 75702fa commit 8fb8863
Show file tree
Hide file tree
Showing 22 changed files with 884 additions and 439 deletions.
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false

[*.json]
indent_size = 2
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
],
"require": {
"php": ">=7.0.0",
"psr/http-message": "^1.0"
"psr/http-message": "^1.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"
"php-http/psr7-integration-tests": "dev-master",
"http-interop/http-factory-tests": "^0.5.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.1/phpunit.xsd"
backupGlobals="true"
colors="true"
bootstrap="tests/bootstrap.php">
<testsuites>
Expand Down
64 changes: 64 additions & 0 deletions src/Factory/RequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim-Psr7
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE (MIT License)
*/

declare(strict_types=1);

namespace Slim\Psr7\Factory;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Slim\Psr7\Headers;
use Slim\Psr7\Request;

class RequestFactory implements RequestFactoryInterface
{
/** @var StreamFactoryInterface */
protected $streamFactory;
/** @var UriFactoryInterface */
protected $uriFactory;

public function __construct(StreamFactoryInterface $streamFactory = null, UriFactoryInterface $uriFactory = null)
{
if (!isset($streamFactory)) {
$streamFactory = new StreamFactory();
}
if (!isset($uriFactory)) {
$uriFactory = new UriFactory();
}

$this->streamFactory = $streamFactory;
$this->uriFactory = $uriFactory;
}

/**
* Create a new request.
*
* @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(string $method, $uri): RequestInterface
{
if (is_string($uri)) {
$uri = $this->uriFactory->createUri($uri);
} elseif (!$uri instanceof UriInterface) {
throw new \InvalidArgumentException('URI must either be string or instance of ' . UriInterface::class);
}

$body = $this->streamFactory->createStream();

return new Request($method, $uri, new Headers(), [], [], $body);
}
}
40 changes: 40 additions & 0 deletions src/Factory/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim-Psr7
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE (MIT License)
*/

declare(strict_types=1);

namespace Slim\Psr7\Factory;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Slim\Psr7\Response;

class ResponseFactory implements ResponseFactoryInterface
{
/**
* Create a new response.
*
* @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(int $code = 200, string $reasonPhrase = ''): ResponseInterface
{
$res = new Response($code);

if ($reasonPhrase !== '') {
$res = $res->withStatus($code, $reasonPhrase);
}

return $res;
}
}
78 changes: 78 additions & 0 deletions src/Factory/ServerRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim-Psr7
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE (MIT License)
*/

declare(strict_types=1);

namespace Slim\Psr7\Factory;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
use Slim\Psr7\Cookies;
use Slim\Psr7\Headers;
use Slim\Psr7\Request;

class ServerRequestFactory implements ServerRequestFactoryInterface
{
/** @var StreamFactoryInterface */
protected $streamFactory;
/** @var UriFactoryInterface */
protected $uriFactory;

public function __construct(StreamFactoryInterface $streamFactory = null, UriFactoryInterface $uriFactory = null)
{
if (!isset($streamFactory)) {
$streamFactory = new StreamFactory();
}
if (!isset($uriFactory)) {
$uriFactory = new UriFactory();
}

$this->streamFactory = $streamFactory;
$this->uriFactory = $uriFactory;
}

/**
* Create a new server request.
*
* 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(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
if (is_string($uri)) {
$uri = $this->uriFactory->createUri($uri);
} elseif (!$uri instanceof UriInterface) {
throw new \InvalidArgumentException('URI must either be string or instance of ' . UriInterface::class);
}

$body = $this->streamFactory->createStream();
$headers = new Headers();
$cookies = [];

if (!empty($serverParams)) {
$headers = Headers::createFromGlobals($serverParams);
$cookies = Cookies::parseHeader($headers->get('Cookie', []));
}

return new Request($method, $uri, $headers, $cookies, $serverParams, $body);
}
}
69 changes: 69 additions & 0 deletions src/Factory/StreamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim-Psr7
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE (MIT License)
*/

declare(strict_types=1);

namespace Slim\Psr7\Factory;

use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Slim\Psr7\Stream;

class StreamFactory implements StreamFactoryInterface
{
/**
* Create a new stream from a string.
*
* The stream SHOULD be created with a temporary resource.
*
* @param string $content String content with which to populate the stream.
*
* @return StreamInterface
*/
public function createStream(string $content = ''): StreamInterface
{
$resource = fopen('php://temp', 'r+');
fwrite($resource, $content);
rewind($resource);

return $this->createStreamFromResource($resource);
}

/**
* Create a stream from an existing file.
*
* The file MUST be opened using the given mode, which may be any mode
* supported by the `fopen` function.
*
* The `$filename` MAY be any string supported by `fopen()`.
*
* @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(string $filename, string $mode = 'r'): StreamInterface
{
return $this->createStreamFromResource(fopen($filename, $mode));
}

/**
* Create a new stream from an existing resource.
*
* The stream MUST be readable and may be writable.
*
* @param resource $resource PHP resource to use as basis of stream.
*
* @return StreamInterface
*/
public function createStreamFromResource($resource): StreamInterface
{
return new Stream($resource);
}
}
59 changes: 59 additions & 0 deletions src/Factory/UploadedFileFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @link https://github.com/slimphp/Slim-Psr7
* @copyright Copyright (c) 2011-2018 Josh Lockhart
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE (MIT License)
*/

declare(strict_types=1);

namespace Slim\Psr7\Factory;

use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Slim\Psr7\UploadedFile;

class UploadedFileFactory implements UploadedFileFactoryInterface
{
/**
* Create a new uploaded file.
*
* 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 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.
*/
public function createUploadedFile(
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 = $stream->getSize();
}

return new UploadedFile($file, $clientFilename, $clientMediaType, $size, $error);
}
}
Loading

0 comments on commit 8fb8863

Please sign in to comment.