From 021f8e3210615afed8e13232bb1b19cfc26f4e37 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Thu, 13 Oct 2016 22:49:24 +0300 Subject: [PATCH 1/8] Add message, stream and URI factories for Slim Framework --- composer.json | 4 +- .../MessageFactory/SlimMessageFactorySpec.php | 15 ++++ spec/UriFactory/SlimUriFactorySpec.php | 24 +++++++ src/MessageFactory/SlimMessageFactory.php | 72 +++++++++++++++++++ src/StreamFactory/SlimStreamFactory.php | 40 +++++++++++ src/UriFactory/SlimUriFactory.php | 31 ++++++++ 6 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 spec/MessageFactory/SlimMessageFactorySpec.php create mode 100644 spec/UriFactory/SlimUriFactorySpec.php create mode 100644 src/MessageFactory/SlimMessageFactory.php create mode 100644 src/StreamFactory/SlimStreamFactory.php create mode 100644 src/UriFactory/SlimUriFactory.php diff --git a/composer.json b/composer.json index 0efd08a..9af696b 100644 --- a/composer.json +++ b/composer.json @@ -22,11 +22,13 @@ "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", + "slim/slim": "^3.5" }, "suggest": { "zendframework/zend-diactoros": "Used with Diactoros Factories", "guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories", + "slim/slim": "Used with Slim Factories", "ext-zlib": "Used with compressor/decompressor streams" }, "autoload": { diff --git a/spec/MessageFactory/SlimMessageFactorySpec.php b/spec/MessageFactory/SlimMessageFactorySpec.php new file mode 100644 index 0000000..6dda618 --- /dev/null +++ b/spec/MessageFactory/SlimMessageFactorySpec.php @@ -0,0 +1,15 @@ +shouldHaveType('Http\Message\MessageFactory\SlimMessageFactory'); + } +} diff --git a/spec/UriFactory/SlimUriFactorySpec.php b/spec/UriFactory/SlimUriFactorySpec.php new file mode 100644 index 0000000..aac1772 --- /dev/null +++ b/spec/UriFactory/SlimUriFactorySpec.php @@ -0,0 +1,24 @@ +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); + } +} diff --git a/src/MessageFactory/SlimMessageFactory.php b/src/MessageFactory/SlimMessageFactory.php new file mode 100644 index 0000000..cdad2ec --- /dev/null +++ b/src/MessageFactory/SlimMessageFactory.php @@ -0,0 +1,72 @@ + + */ +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); + } +} diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php new file mode 100644 index 0000000..0fc040e --- /dev/null +++ b/src/StreamFactory/SlimStreamFactory.php @@ -0,0 +1,40 @@ + + */ +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; + } +} diff --git a/src/UriFactory/SlimUriFactory.php b/src/UriFactory/SlimUriFactory.php new file mode 100644 index 0000000..c013d54 --- /dev/null +++ b/src/UriFactory/SlimUriFactory.php @@ -0,0 +1,31 @@ + + */ +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'); + } +} From 9e131190c607811c1eafc3cdf7b63c73610740f1 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 13:17:36 +0300 Subject: [PATCH 2/8] Do not test Slim factories with PHP 5.4 --- .travis.yml | 3 +++ composer.json | 5 +++-- phpspec.ci.yml | 1 + phpspec.yml.dist | 1 + spec/MessageFactory/SlimMessageFactorySpec.php | 3 +++ spec/UriFactory/SlimUriFactorySpec.php | 3 +++ 6 files changed, 14 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 95980fc..68321d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,9 @@ before_install: install: - travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction +before_script: + - if [[ "${TRAVIS_PHP_VERSION}" == "5.4" ]]; then composer remove slim/slim; fi + script: - $TEST_COMMAND diff --git a/composer.json b/composer.json index 9af696b..9605cc8 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "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" }, "require-dev": { "zendframework/zend-diactoros": "^1.0", @@ -23,7 +24,7 @@ "phpspec/phpspec": "^2.4", "henrikbjorn/phpspec-code-coverage" : "^1.0", "coduo/phpspec-data-provider-extension": "^1.0", - "slim/slim": "^3.5" + "akeneo/phpspec-skip-example-extension": "^1.0" }, "suggest": { "zendframework/zend-diactoros": "Used with Diactoros Factories", diff --git a/phpspec.ci.yml b/phpspec.ci.yml index f08e148..a4a288f 100644 --- a/phpspec.ci.yml +++ b/phpspec.ci.yml @@ -6,6 +6,7 @@ formatter.name: pretty extensions: - PhpSpec\Extension\CodeCoverageExtension - Coduo\PhpSpec\DataProvider\DataProviderExtension + - Akeneo\SkipExampleExtension code_coverage: format: clover output: build/coverage.xml diff --git a/phpspec.yml.dist b/phpspec.yml.dist index 8d38a4e..56d3c89 100644 --- a/phpspec.yml.dist +++ b/phpspec.yml.dist @@ -5,3 +5,4 @@ suites: formatter.name: pretty extensions: - Coduo\PhpSpec\DataProvider\DataProviderExtension + - Akeneo\SkipExampleExtension diff --git a/spec/MessageFactory/SlimMessageFactorySpec.php b/spec/MessageFactory/SlimMessageFactorySpec.php index 6dda618..e39452e 100644 --- a/spec/MessageFactory/SlimMessageFactorySpec.php +++ b/spec/MessageFactory/SlimMessageFactorySpec.php @@ -4,6 +4,9 @@ use PhpSpec\ObjectBehavior; +/** + * @require Slim\Http\Request + */ class SlimMessageFactorySpec extends ObjectBehavior { use MessageFactoryBehavior; diff --git a/spec/UriFactory/SlimUriFactorySpec.php b/spec/UriFactory/SlimUriFactorySpec.php index aac1772..b373f5f 100644 --- a/spec/UriFactory/SlimUriFactorySpec.php +++ b/spec/UriFactory/SlimUriFactorySpec.php @@ -5,6 +5,9 @@ use Psr\Http\Message\UriInterface; use PhpSpec\ObjectBehavior; +/** + * @require Slim\Http\Uri + */ class SlimUriFactorySpec extends ObjectBehavior { use UriFactoryBehavior; From f5302d5af547743767b98576138f22bb13b3c94e Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 13:18:18 +0300 Subject: [PATCH 3/8] Add missing test for Slim stream factory --- spec/StreamFactory/SlimStreamFactorySpec.php | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 spec/StreamFactory/SlimStreamFactorySpec.php diff --git a/spec/StreamFactory/SlimStreamFactorySpec.php b/spec/StreamFactory/SlimStreamFactorySpec.php new file mode 100644 index 0000000..c3bfef3 --- /dev/null +++ b/spec/StreamFactory/SlimStreamFactorySpec.php @@ -0,0 +1,26 @@ +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'); + } +} From fb74a28d0025ebe4e40446d5f2fc77e6bda37df8 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 14:00:32 +0300 Subject: [PATCH 4/8] Slim should be development dependency --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 9605cc8..f8e32de 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,7 @@ "php": ">=5.4", "psr/http-message": "^1.0", "php-http/message-factory": "^1.0.2", - "clue/stream-filter": "^1.3", - "slim/slim": "^3.5" + "clue/stream-filter": "^1.3" }, "require-dev": { "zendframework/zend-diactoros": "^1.0", @@ -24,7 +23,8 @@ "phpspec/phpspec": "^2.4", "henrikbjorn/phpspec-code-coverage" : "^1.0", "coduo/phpspec-data-provider-extension": "^1.0", - "akeneo/phpspec-skip-example-extension": "^1.0" + "akeneo/phpspec-skip-example-extension": "^1.0", + "slim/slim": "^3.5" }, "suggest": { "zendframework/zend-diactoros": "Used with Diactoros Factories", From bdce0e2d3da1583c932dd7130d0e70a03492fa42 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 14:01:04 +0300 Subject: [PATCH 5/8] Remove slim/slim before running composer install on PHP 5.4 --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 68321d2..1d51b60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,14 +30,12 @@ matrix: before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi + - if [[ "${TRAVIS_PHP_VERSION}" == "5.4" ]]; then composer remove slim/slim --dev --no-update; fi - travis_retry composer self-update install: - travis_retry composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction -before_script: - - if [[ "${TRAVIS_PHP_VERSION}" == "5.4" ]]; then composer remove slim/slim; fi - script: - $TEST_COMMAND From ea80b634a7d16e6021246937948fc555011f3476 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 15:10:28 +0300 Subject: [PATCH 6/8] HHVM does not have rw mode, use r+ instead https://github.com/facebook/hhvm/issues/6999 --- src/StreamFactory/SlimStreamFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StreamFactory/SlimStreamFactory.php b/src/StreamFactory/SlimStreamFactory.php index 0fc040e..e779f3b 100644 --- a/src/StreamFactory/SlimStreamFactory.php +++ b/src/StreamFactory/SlimStreamFactory.php @@ -25,7 +25,7 @@ public function createStream($body = null) if (is_resource($body)) { $stream = new Stream($body); } else { - $resource = fopen('php://memory', 'rw'); + $resource = fopen('php://memory', 'r+'); $stream = new Stream($resource); if (null !== $body) { From 869d6584b80d1342cd1249e87714a4360bc7be6b Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 15:11:59 +0300 Subject: [PATCH 7/8] Better wording for suggestion --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f8e32de..9dd876b 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "suggest": { "zendframework/zend-diactoros": "Used with Diactoros Factories", "guzzlehttp/psr7": "Used with Guzzle PSR-7 Factories", - "slim/slim": "Used with Slim Factories", + "slim/slim": "Used with Slim Framework PSR-7 implementation", "ext-zlib": "Used with compressor/decompressor streams" }, "autoload": { From d41f5e731f661837186ed62ca06ddbf7cbfbe84a Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Fri, 14 Oct 2016 22:15:38 +0300 Subject: [PATCH 8/8] Mention Slim Framework factories --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce2c410..99f4cce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## Unreleased + +### Added + +- Message, stream and URI factories for [Slim Framework](https://github.com/slimphp/Slim) ## 1.3.1 - 2016-07-15