forked from slimphp/Slim-Psr7
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
884 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.