-
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
021f8e3
Add message, stream and URI factories for Slim Framework
tuupola 9e13119
Do not test Slim factories with PHP 5.4
tuupola f5302d5
Add missing test for Slim stream factory
tuupola fb74a28
Slim should be development dependency
tuupola bdce0e2
Remove slim/slim before running composer install on PHP 5.4
tuupola ea80b63
HHVM does not have rw mode, use r+ instead
tuupola 869d658
Better wording for suggestion
tuupola d41f5e7
Mention Slim Framework factories
tuupola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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'); | ||
} | ||
} |
This file contains hidden or 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,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'); | ||
} | ||
} |
This file contains hidden or 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,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); | ||
} | ||
} |
This file contains hidden or 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,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 <tuupola@appelsiini.net> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains hidden or 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 | ||
|
||
namespace Http\Message\StreamFactory; | ||
|
||
use Http\Message\StreamFactory; | ||
use Psr\Http\Message\StreamInterface; | ||
use Slim\Http\Stream; | ||
|
||
/** | ||
* Creates Slim 3 streams. | ||
* | ||
* @author Mika Tuupola <tuupola@appelsiini.net> | ||
*/ | ||
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; | ||
} | ||
} |
This file contains hidden or 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,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 <tuupola@appelsiini.net> | ||
*/ | ||
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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about wording. Maybe
"Used with Slim Framework PSR-7 implementation"
or"Used with Slim Framework PSR-7 messages"
?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.
Any of those work for me