PSR-7: Replaced implementation with `Nyholm\Psr7`

This commit is contained in:
Igor Scheller 2019-04-17 10:09:50 +02:00 committed by msquare
parent d7ad7b0b13
commit 58ca7113f3
7 changed files with 35 additions and 31 deletions

View File

@ -28,7 +28,7 @@
"illuminate/database": "5.5.*", "illuminate/database": "5.5.*",
"illuminate/support": "5.5.*", "illuminate/support": "5.5.*",
"nikic/fast-route": "^1.3", "nikic/fast-route": "^1.3",
"php-extended/php-http-message-factory-psr17": "^1.0", "nyholm/psr7": "^1.1",
"psr/container": "^1.0", "psr/container": "^1.0",
"psr/http-server-middleware": "^1.0", "psr/http-server-middleware": "^1.0",
"psr/log": "^1.0", "psr/log": "^1.0",

View File

@ -3,7 +3,7 @@
namespace Engelsystem\Http; namespace Engelsystem\Http;
use PhpExtended\HttpMessage\StringStream; use Nyholm\Psr7\Stream;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
/** /**
@ -213,7 +213,8 @@ trait MessageTrait
*/ */
public function getBody() public function getBody()
{ {
$stream = new StringStream($this->getContent()); $stream = Stream::create($this->getContent());
$stream->rewind();
return $stream; return $stream;
} }

View File

@ -3,10 +3,7 @@
namespace Engelsystem\Http; namespace Engelsystem\Http;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use PhpExtended\HttpMessage\ResponseFactory; use Nyholm\Psr7\Factory\Psr17Factory;
use PhpExtended\HttpMessage\ServerRequestFactory;
use PhpExtended\HttpMessage\StreamFactory;
use PhpExtended\HttpMessage\UploadedFileFactory;
use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestFactoryInterface;
@ -21,20 +18,26 @@ class Psr7ServiceProvider extends ServiceProvider
{ {
public function register() public function register()
{ {
$psr17Factory = Psr17Factory::class;
foreach ( foreach (
[ [
ServerRequestFactory::class => ['psr7.factory.request', ServerRequestFactoryInterface::class], 'psr7.factory.request',
ResponseFactory::class => ['psr7.factory.response', ResponseFactoryInterface::class], ServerRequestFactoryInterface::class,
UploadedFileFactory::class => ['psr7.factory.upload', UploadedFileFactoryInterface::class], 'psr7.factory.response',
StreamFactory::class => ['psr7.factory.stream', StreamFactoryInterface::class], ResponseFactoryInterface::class,
PsrHttpFactory::class => ['psr7.factory', HttpMessageFactoryInterface::class], 'psr7.factory.upload',
] as $class => $aliases UploadedFileFactoryInterface::class,
'psr7.factory.stream',
StreamFactoryInterface::class,
] as $alias
) { ) {
foreach ($aliases as $alias) { $this->app->bind($alias, $psr17Factory);
$this->app->bind($alias, $class);
}
} }
$this->app->bind('psr7.factory', PsrHttpFactory::class);
$this->app->bind(HttpMessageFactoryInterface::class, PsrHttpFactory::class);
$this->app->bind('psr7.request', 'request'); $this->app->bind('psr7.request', 'request');
$this->app->bind(ServerRequestInterface::class, 'psr7.request'); $this->app->bind(ServerRequestInterface::class, 'psr7.request');

View File

@ -2,8 +2,8 @@
namespace Engelsystem\Http; namespace Engelsystem\Http;
use PhpExtended\HttpMessage\UploadedFile; use Nyholm\Psr7\UploadedFile;
use PhpExtended\HttpMessage\Uri; use Nyholm\Psr7\Uri;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
@ -207,7 +207,7 @@ class Request extends SymfonyRequest implements ServerRequestInterface
{ {
$uri = parent::getUri(); $uri = parent::getUri();
return Uri::parseFromString($uri); return new Uri($uri);
} }
/** /**
@ -332,11 +332,11 @@ class Request extends SymfonyRequest implements ServerRequestInterface
/** @var SymfonyFile $file */ /** @var SymfonyFile $file */
$files[] = new UploadedFile( $files[] = new UploadedFile(
$file->getClientOriginalName(),
$file->getRealPath(), $file->getRealPath(),
$file->getMimeType(),
$file->getSize(), $file->getSize(),
$file->getError() $file->getError(),
$file->getClientOriginalName(),
$file->getMimeType()
); );
} }
@ -464,10 +464,10 @@ class Request extends SymfonyRequest implements ServerRequestInterface
* This method obviates the need for a hasAttribute() method, as it allows * This method obviates the need for a hasAttribute() method, as it allows
* specifying a default value to return if the attribute is not found. * specifying a default value to return if the attribute is not found.
* *
* @see getAttributes()
* @param string $name The attribute name. * @param string $name The attribute name.
* @param mixed $default Default value to return if the attribute does not exist. * @param mixed $default Default value to return if the attribute does not exist.
* @return mixed * @return mixed
* @see getAttributes()
*/ */
public function getAttribute($name, $default = null) public function getAttribute($name, $default = null)
{ {
@ -484,10 +484,10 @@ class Request extends SymfonyRequest implements ServerRequestInterface
* immutability of the message, and MUST return an instance that has the * immutability of the message, and MUST return an instance that has the
* updated attribute. * updated attribute.
* *
* @see getAttributes()
* @param string $name The attribute name. * @param string $name The attribute name.
* @param mixed $value The value of the attribute. * @param mixed $value The value of the attribute.
* @return static * @return static
* @see getAttributes()
*/ */
public function withAttribute($name, $value) public function withAttribute($name, $value)
{ {
@ -509,9 +509,9 @@ class Request extends SymfonyRequest implements ServerRequestInterface
* immutability of the message, and MUST return an instance that removes * immutability of the message, and MUST return an instance that removes
* the attribute. * the attribute.
* *
* @see getAttributes()
* @param string $name The attribute name. * @param string $name The attribute name.
* @return static * @return static
* @see getAttributes()
*/ */
public function withoutAttribute($name) public function withoutAttribute($name)
{ {

View File

@ -3,7 +3,7 @@
namespace Engelsystem\Test\Unit\Http; namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Test\Unit\Http\Stub\MessageTraitRequestImplementation; use Engelsystem\Test\Unit\Http\Stub\MessageTraitRequestImplementation;
use PhpExtended\HttpMessage\StringStream; use Nyholm\Psr7\Stream;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class MessageTraitRequestTest extends TestCase class MessageTraitRequestTest extends TestCase
@ -36,7 +36,7 @@ class MessageTraitRequestTest extends TestCase
*/ */
public function testWithBody() public function testWithBody()
{ {
$stream = new StringStream('Test content'); $stream = Stream::create('Test content');
$message = new MessageTraitRequestImplementation(); $message = new MessageTraitRequestImplementation();
$newMessage = $message->withBody($stream); $newMessage = $message->withBody($stream);

View File

@ -3,7 +3,7 @@
namespace Engelsystem\Test\Unit\Http; namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Test\Unit\Http\Stub\MessageTraitResponseImplementation; use Engelsystem\Test\Unit\Http\Stub\MessageTraitResponseImplementation;
use PhpExtended\HttpMessage\StringStream; use Nyholm\Psr7\Stream;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Http\Message\MessageInterface; use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
@ -145,7 +145,7 @@ class MessageTraitResponseTest extends TestCase
*/ */
public function testWithBody() public function testWithBody()
{ {
$stream = new StringStream('Test content'); $stream = Stream::create('Test content');
$message = new MessageTraitResponseImplementation(); $message = new MessageTraitResponseImplementation();
$newMessage = $message->withBody($stream); $newMessage = $message->withBody($stream);

View File

@ -3,7 +3,7 @@
namespace Engelsystem\Test\Unit\Http; namespace Engelsystem\Test\Unit\Http;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use PhpExtended\HttpMessage\UploadedFile; use Nyholm\Psr7\UploadedFile;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject; use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\RequestInterface;
@ -307,7 +307,7 @@ class RequestTest extends TestCase
{ {
$filename = tempnam(sys_get_temp_dir(), 'test'); $filename = tempnam(sys_get_temp_dir(), 'test');
file_put_contents($filename, 'LoremIpsum!'); file_put_contents($filename, 'LoremIpsum!');
$file = new UploadedFile('test.txt', $filename, 'text/plain', 11, UPLOAD_ERR_OK); $file = new UploadedFile($filename, 11, UPLOAD_ERR_OK, 'test.txt', 'text/plain');
$request = new Request(); $request = new Request();
$new = $request->withUploadedFiles([$file]); $new = $request->withUploadedFiles([$file]);