Skip to content

Commit 51a0aa5

Browse files
tuupolaNyholm
authored andcommitted
Add message, stream and URI factories for Slim Framework (#53)
* Add message, stream and URI factories for Slim Framework * Do not test Slim factories with PHP 5.4 * Add missing test for Slim stream factory * Slim should be development dependency * Remove slim/slim before running composer install on PHP 5.4 * HHVM does not have rw mode, use r+ instead facebook/hhvm#6999 * Better wording for suggestion * Mention Slim Framework factories
1 parent acde7da commit 51a0aa5

File tree

11 files changed

+226
-1
lines changed

11 files changed

+226
-1
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ matrix:
3030

3131
before_install:
3232
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
33+
- if [[ "${TRAVIS_PHP_VERSION}" == "5.4" ]]; then composer remove slim/slim --dev --no-update; fi
3334
- travis_retry composer self-update
3435

3536
install:

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
### Added
6+
7+
- Message, stream and URI factories for [Slim Framework](https://github.com/slimphp/Slim)
38

49
## 1.3.1 - 2016-07-15
510

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
"ext-zlib": "*",
2323
"phpspec/phpspec": "^2.4",
2424
"henrikbjorn/phpspec-code-coverage" : "^1.0",
25-
"coduo/phpspec-data-provider-extension": "^1.0"
25+
"coduo/phpspec-data-provider-extension": "^1.0",
26+
"akeneo/phpspec-skip-example-extension": "^1.0",
27+
"slim/slim": "^3.5"
2628
},
2729
"suggest": {
2830
"zendframework/zend-diactoros": "Used with Diactoros Factories",
2931
"guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories",
32+
"slim/slim": "Used with Slim Framework PSR-7 implementation",
3033
"ext-zlib": "Used with compressor/decompressor streams"
3134
},
3235
"autoload": {

phpspec.ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ formatter.name: pretty
66
extensions:
77
- PhpSpec\Extension\CodeCoverageExtension
88
- Coduo\PhpSpec\DataProvider\DataProviderExtension
9+
- Akeneo\SkipExampleExtension
910
code_coverage:
1011
format: clover
1112
output: build/coverage.xml

phpspec.yml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ suites:
55
formatter.name: pretty
66
extensions:
77
- Coduo\PhpSpec\DataProvider\DataProviderExtension
8+
- Akeneo\SkipExampleExtension
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace spec\Http\Message\MessageFactory;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
/**
8+
* @require Slim\Http\Request
9+
*/
10+
class SlimMessageFactorySpec extends ObjectBehavior
11+
{
12+
use MessageFactoryBehavior;
13+
14+
function it_is_initializable()
15+
{
16+
$this->shouldHaveType('Http\Message\MessageFactory\SlimMessageFactory');
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace spec\Http\Message\StreamFactory;
4+
5+
use Slim\Http\Stream;
6+
use PhpSpec\ObjectBehavior;
7+
8+
/**
9+
* @require Slim\Http\Stream
10+
*/
11+
class SlimStreamFactorySpec extends ObjectBehavior
12+
{
13+
use StreamFactoryBehavior;
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\StreamFactory\SlimStreamFactory');
18+
}
19+
20+
function it_creates_a_stream_from_stream()
21+
{
22+
$resource = fopen('php://memory', 'rw');
23+
$this->createStream(new Stream($resource))
24+
->shouldHaveType('Psr\Http\Message\StreamInterface');
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace spec\Http\Message\UriFactory;
4+
5+
use Psr\Http\Message\UriInterface;
6+
use PhpSpec\ObjectBehavior;
7+
8+
/**
9+
* @require Slim\Http\Uri
10+
*/
11+
class SlimUriFactorySpec extends ObjectBehavior
12+
{
13+
use UriFactoryBehavior;
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\UriFactory\SlimUriFactory');
18+
}
19+
20+
/**
21+
* TODO: Remove this when https://github.com/phpspec/phpspec/issues/825 is resolved
22+
*/
23+
function it_creates_a_uri_from_uri(UriInterface $uri)
24+
{
25+
$this->createUri($uri)->shouldReturn($uri);
26+
}
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Http\Message\MessageFactory;
4+
5+
use Http\Message\StreamFactory\SlimStreamFactory;
6+
use Http\Message\UriFactory\SlimUriFactory;
7+
use Http\Message\MessageFactory;
8+
use Slim\Http\Request;
9+
use Slim\Http\Response;
10+
use Slim\Http\Headers;
11+
12+
/**
13+
* Creates Slim 3 messages.
14+
*
15+
* @author Mika Tuupola <[email protected]>
16+
*/
17+
final class SlimMessageFactory implements MessageFactory
18+
{
19+
/**
20+
* @var SlimStreamFactory
21+
*/
22+
private $streamFactory;
23+
24+
/**
25+
* @var SlimUriFactory
26+
*/
27+
private $uriFactory;
28+
29+
public function __construct()
30+
{
31+
$this->streamFactory = new SlimStreamFactory();
32+
$this->uriFactory = new SlimUriFactory();
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function createRequest(
39+
$method,
40+
$uri,
41+
array $headers = [],
42+
$body = null,
43+
$protocolVersion = '1.1'
44+
) {
45+
return (new Request(
46+
$method,
47+
$this->uriFactory->createUri($uri),
48+
new Headers($headers),
49+
[],
50+
[],
51+
$this->streamFactory->createStream($body),
52+
[]
53+
))->withProtocolVersion($protocolVersion);
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function createResponse(
60+
$statusCode = 200,
61+
$reasonPhrase = null,
62+
array $headers = [],
63+
$body = null,
64+
$protocolVersion = '1.1'
65+
) {
66+
return (new Response(
67+
$statusCode,
68+
new Headers($headers),
69+
$this->streamFactory->createStream($body)
70+
))->withProtocolVersion($protocolVersion);
71+
}
72+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Http\Message\StreamFactory;
4+
5+
use Http\Message\StreamFactory;
6+
use Psr\Http\Message\StreamInterface;
7+
use Slim\Http\Stream;
8+
9+
/**
10+
* Creates Slim 3 streams.
11+
*
12+
* @author Mika Tuupola <[email protected]>
13+
*/
14+
final class SlimStreamFactory implements StreamFactory
15+
{
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function createStream($body = null)
20+
{
21+
if ($body instanceof StreamInterface) {
22+
return $body;
23+
}
24+
25+
if (is_resource($body)) {
26+
$stream = new Stream($body);
27+
} else {
28+
$resource = fopen('php://memory', 'r+');
29+
$stream = new Stream($resource);
30+
31+
if (null !== $body) {
32+
$stream->write((string) $body);
33+
}
34+
}
35+
36+
$stream->rewind();
37+
38+
return $stream;
39+
}
40+
}

src/UriFactory/SlimUriFactory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Http\Message\UriFactory;
4+
5+
use Http\Message\UriFactory;
6+
use Psr\Http\Message\UriInterface;
7+
use Slim\Http\Uri;
8+
9+
/**
10+
* Creates Slim 3 URI.
11+
*
12+
* @author Mika Tuupola <[email protected]>
13+
*/
14+
final class SlimUriFactory implements UriFactory
15+
{
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function createUri($uri)
20+
{
21+
if ($uri instanceof UriInterface) {
22+
return $uri;
23+
}
24+
25+
if (is_string($uri)) {
26+
return Uri::createFromString($uri);
27+
}
28+
29+
throw new \InvalidArgumentException('URI must be a string or UriInterface');
30+
}
31+
}

0 commit comments

Comments
 (0)