-
Notifications
You must be signed in to change notification settings - Fork 39
Add message, stream and URI factories for Slim Framework #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
021f8e3
9e13119
f5302d5
fb74a28
bdce0e2
ea80b63
869d658
d41f5e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,22 @@ | |
"php": ">=5.4", | ||
"psr/http-message": "^1.0", | ||
"php-http/message-factory": "^1.0.2", | ||
"clue/stream-filter": "^1.3" | ||
"clue/stream-filter": "^1.3", | ||
"slim/slim": "^3.5" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only dev requirement, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be yes. But this way it still wont still work because. You cannot do the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure I understand you. Also, please see my previous comment about moving the removal to before_install instead of before_script with --no-update. --no-update causes to update composer.json, but does not actually call install. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah disregard last comment. I did not know you can run |
||
}, | ||
"require-dev": { | ||
"zendframework/zend-diactoros": "^1.0", | ||
"guzzlehttp/psr7": "^1.0", | ||
"ext-zlib": "*", | ||
"phpspec/phpspec": "^2.4", | ||
"henrikbjorn/phpspec-code-coverage" : "^1.0", | ||
"coduo/phpspec-data-provider-extension": "^1.0" | ||
"coduo/phpspec-data-provider-extension": "^1.0", | ||
"akeneo/phpspec-skip-example-extension": "^1.0" | ||
}, | ||
"suggest": { | ||
"zendframework/zend-diactoros": "Used with Diactoros Factories", | ||
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories", | ||
"slim/slim": "Used with Slim Factories", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Diactoros and Guzzle PSR-7 are obviously meaning message factories while Slim is a framework. Can you please add some extra indication that it means message/PSR-7 factories? Just to avoid confusion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about wording. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any of those work for me |
||
"ext-zlib": "Used with compressor/decompressor streams" | ||
}, | ||
"autoload": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace spec\Http\Message\MessageFactory; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
|
||
/** | ||
* @require Slim\Http\Request | ||
*/ | ||
class SlimMessageFactorySpec extends ObjectBehavior | ||
{ | ||
use MessageFactoryBehavior; | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\MessageFactory\SlimMessageFactory'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace spec\Http\Message\StreamFactory; | ||
|
||
use Slim\Http\Stream; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
/** | ||
* @require Slim\Http\Stream | ||
*/ | ||
class SlimStreamFactorySpec extends ObjectBehavior | ||
{ | ||
use StreamFactoryBehavior; | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\StreamFactory\SlimStreamFactory'); | ||
} | ||
|
||
function it_creates_a_stream_from_stream() | ||
{ | ||
$resource = fopen('php://memory', 'rw'); | ||
$this->createStream(new Stream($resource)) | ||
->shouldHaveType('Psr\Http\Message\StreamInterface'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace spec\Http\Message\UriFactory; | ||
|
||
use Psr\Http\Message\UriInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
/** | ||
* @require Slim\Http\Uri | ||
*/ | ||
class SlimUriFactorySpec extends ObjectBehavior | ||
{ | ||
use UriFactoryBehavior; | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Message\UriFactory\SlimUriFactory'); | ||
} | ||
|
||
/** | ||
* TODO: Remove this when https://github.com/phpspec/phpspec/issues/825 is resolved | ||
*/ | ||
function it_creates_a_uri_from_uri(UriInterface $uri) | ||
{ | ||
$this->createUri($uri)->shouldReturn($uri); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Http\Message\MessageFactory; | ||
|
||
use Http\Message\StreamFactory\SlimStreamFactory; | ||
use Http\Message\UriFactory\SlimUriFactory; | ||
use Http\Message\MessageFactory; | ||
use Slim\Http\Request; | ||
use Slim\Http\Response; | ||
use Slim\Http\Headers; | ||
|
||
/** | ||
* Creates Slim 3 messages. | ||
* | ||
* @author Mika Tuupola <[email protected]> | ||
*/ | ||
final class SlimMessageFactory implements MessageFactory | ||
{ | ||
/** | ||
* @var SlimStreamFactory | ||
*/ | ||
private $streamFactory; | ||
|
||
/** | ||
* @var SlimUriFactory | ||
*/ | ||
private $uriFactory; | ||
|
||
public function __construct() | ||
{ | ||
$this->streamFactory = new SlimStreamFactory(); | ||
$this->uriFactory = new SlimUriFactory(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createRequest( | ||
$method, | ||
$uri, | ||
array $headers = [], | ||
$body = null, | ||
$protocolVersion = '1.1' | ||
) { | ||
return (new Request( | ||
$method, | ||
$this->uriFactory->createUri($uri), | ||
new Headers($headers), | ||
[], | ||
[], | ||
$this->streamFactory->createStream($body), | ||
[] | ||
))->withProtocolVersion($protocolVersion); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createResponse( | ||
$statusCode = 200, | ||
$reasonPhrase = null, | ||
array $headers = [], | ||
$body = null, | ||
$protocolVersion = '1.1' | ||
) { | ||
return (new Response( | ||
$statusCode, | ||
new Headers($headers), | ||
$this->streamFactory->createStream($body) | ||
))->withProtocolVersion($protocolVersion); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Http\Message\StreamFactory; | ||
|
||
use Http\Message\StreamFactory; | ||
use Psr\Http\Message\StreamInterface; | ||
use Slim\Http\Stream; | ||
|
||
/** | ||
* Creates Slim 3 streams. | ||
* | ||
* @author Mika Tuupola <[email protected]> | ||
*/ | ||
final class SlimStreamFactory implements StreamFactory | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createStream($body = null) | ||
{ | ||
if ($body instanceof StreamInterface) { | ||
return $body; | ||
} | ||
|
||
if (is_resource($body)) { | ||
$stream = new Stream($body); | ||
} else { | ||
$resource = fopen('php://memory', 'rw'); | ||
$stream = new Stream($resource); | ||
|
||
if (null !== $body) { | ||
$stream->write((string) $body); | ||
} | ||
} | ||
|
||
$stream->rewind(); | ||
|
||
return $stream; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Http\Message\UriFactory; | ||
|
||
use Http\Message\UriFactory; | ||
use Psr\Http\Message\UriInterface; | ||
use Slim\Http\Uri; | ||
|
||
/** | ||
* Creates Slim 3 URI. | ||
* | ||
* @author Mika Tuupola <[email protected]> | ||
*/ | ||
final class SlimUriFactory implements UriFactory | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function createUri($uri) | ||
{ | ||
if ($uri instanceof UriInterface) { | ||
return $uri; | ||
} | ||
|
||
if (is_string($uri)) { | ||
return Uri::createFromString($uri); | ||
} | ||
|
||
throw new \InvalidArgumentException('URI must be a string or UriInterface'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this to before install AND add --no-update flag to composer. That way we only have to run compose once.