Skip to content
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

PSR-7 Implementation Decoupling #2529

Merged
merged 48 commits into from
Nov 25, 2018
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0d6a979
add/remove dependencies and scripts from composer.json
l0gicgate Nov 1, 2018
2f41cd9
update Travis-CI configuration
l0gicgate Nov 1, 2018
72a7abc
update README
l0gicgate Nov 1, 2018
53e3c5e
decouple PSR-7 implementation and remove respond and finalize methods…
l0gicgate Nov 1, 2018
475fbcb
add ResponseEmitter
l0gicgate Nov 1, 2018
39390d8
remove PSR-7 dependant method renderWithBody from AbstractErrorRenderer
l0gicgate Nov 1, 2018
6b45c23
modify base ErrorHandler to use PSR-7 generic methods
l0gicgate Nov 1, 2018
467200d
add ResponseFactory dependency injection in ErrorMiddleware
l0gicgate Nov 1, 2018
2b81dd4
modify request method assertion in MethodOverrideMiddleware
l0gicgate Nov 1, 2018
7e7a3cb
add StreamFactory dependency injection in OutputBufferingMiddleware
l0gicgate Nov 1, 2018
7e5fb3b
refactor entire test suite
l0gicgate Nov 1, 2018
6f4cdf6
add Slim-Http decorators example to README
l0gicgate Nov 1, 2018
a97929d
fix mistake in README example
l0gicgate Nov 1, 2018
bcff3c5
add phpstan.neon.dist config file
l0gicgate Nov 1, 2018
53cbd6f
fix all PHPStan errors
l0gicgate Nov 1, 2018
bc765dd
add ext-simplexml to dev dependencies in composer.json
l0gicgate Nov 1, 2018
bc7017f
remove messages in phpstan config
l0gicgate Nov 1, 2018
0008898
remove tests directory from phpstan command in Travis-CI config
l0gicgate Nov 1, 2018
5859306
fix all Slim phpstan errors
l0gicgate Nov 1, 2018
410fc62
fix README comment blocks in examples
l0gicgate Nov 1, 2018
8489a4e
remove unused/reorder imports
l0gicgate Nov 1, 2018
3bbf997
add PSR7ObjectProvider to decouple Tests from specific PSR7 implement…
l0gicgate Nov 1, 2018
7daa6ed
fix PHPCS errors
l0gicgate Nov 1, 2018
dd12345
add PHP 7.3 to Travis-CI config
l0gicgate Nov 2, 2018
24c6867
add Guzzle PSR7 example to README
l0gicgate Nov 2, 2018
bd85e1b
add test coverage clover to Travis-CI config
l0gicgate Nov 2, 2018
6cdb1cb
fix after_success script for php-coveralls in Travis-CI config
l0gicgate Nov 2, 2018
f13c497
add code coverage driver to PHP 7.1 build
l0gicgate Nov 2, 2018
166ced0
fix coverage clover parameter in Travis-CI config
l0gicgate Nov 2, 2018
6d8d74f
fix Nyholm/psr7 README example
l0gicgate Nov 12, 2018
b842ba1
Merge branch '4.x' of https://github.com/slimphp/Slim into PSR7-Decou…
l0gicgate Nov 19, 2018
9f75486
fix param type in InvocationStrategyInterface
l0gicgate Nov 19, 2018
01ccbfa
fix PHPStan errors
l0gicgate Nov 19, 2018
340cadd
fix PHPStan error
l0gicgate Nov 19, 2018
d575b9a
fix PHPStan error
l0gicgate Nov 19, 2018
b4c57be
fix PHPStan errors
l0gicgate Nov 19, 2018
c168c4a
rename base Test class to TestCase
l0gicgate Nov 25, 2018
2ffaee9
change App constructor function signature to take in ResponseFactoryI…
l0gicgate Nov 25, 2018
c7aa199
fix README to reflect changes made to App constructor and run methods
l0gicgate Nov 25, 2018
3ea27ba
fix import ordering back to alphabetical in RouterInterface
l0gicgate Nov 25, 2018
8d454e2
add comment for variable reassignment in Router to avoid PHPStan warning
l0gicgate Nov 25, 2018
570872d
remove unused commented statement in AppTest::tearDownAfterClass
l0gicgate Nov 25, 2018
ba1621b
add comment for ignored error in PHPStan config
l0gicgate Nov 25, 2018
f909bb1
reorder imports alphabetically in multiple files
l0gicgate Nov 25, 2018
3f8978d
fix line length in App
l0gicgate Nov 25, 2018
1a0d4ba
fix README errors
l0gicgate Nov 25, 2018
e2d7633
remove responseChunkSize setting from App
l0gicgate Nov 25, 2018
a62881b
remove useless parameter reassignment to silence PHPStan in Router
l0gicgate Nov 25, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ dist: trusty

matrix:
include:
- php: 7.0
- php: 7.0
env: COMPOSER_ARGS='--prefer-lowest'
- php: 7.1
env: ANALYSIS='true'
- php: 7.2
- php: nightly
- php: 7.1
env: ANALYSIS='true'
- php: 7.2
- php: 7.3
- php: nightly
allow_failures:
- php: nightly
- php: nightly

before_script:
- composer update $COMPOSER_ARGS
- composer require php-coveralls/php-coveralls:^2.1.0
- composer install -n

script:
- if [[ "$ANALYSIS" != 'true' ]]; then vendor/bin/phpunit ; fi
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpcs ; fi
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
- vendor/bin/phpunit
- vendor/bin/phpcs
- vendor/bin/phpstan analyse Slim

after_script:
- if [[ "$ANALYSIS" == 'true' ]]; then php vendor/bin/coveralls --coverage_clover=clover.xml -v ; fi
after_success:
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/php-coveralls --coverage_clover=clover.xml -v ; fi

notifications:
slack: slimphp:0RNzx2JuhkAqIf0MXcUZ0asT
158 changes: 151 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,166 @@ It's recommended that you use [Composer](https://getcomposer.org/) to install Sl
$ composer require slim/slim "^4.0"
```

This will install Slim and all required dependencies. Slim requires PHP 7.0.0 or newer.
This will install Slim and all required dependencies. Slim requires PHP 7.1 or newer.

## Usage
## Choose a PSR-7 Implementation

Before you can get up and running with Slim you will need to choose a PSR-7 implementation that best fits your application. A few notable ones:
- [Nyholm/psr7](https://github.com/Nyholm/psr7) - This is the fastest, strictest and most lightweight implementation at the moment
- [Guzzle/psr7](https://github.com/guzzle/psr7) - This is the implementation used by the Guzzle Client. It is not as strict but adds some nice functionality for Streams and file handling. It is the second fastest implementation but is a bit bulkier
- [zend-diactoros](https://github.com/zendframework/zend-diactoros) - This is the Zend implementation. It is the slowest implementation of the 3.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the slowest

Is it important to mention it here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s a fact, so yes I feel it’s important. That’s as per the benchmarks reported on Nyholm/psr7 it’s public knowledge.


## Example Usage With Nyholm/psr7 and Nyholm/psr7-server

Create an index.php file with the following contents:

```php
<?php
require 'vendor/autoload.php';

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
use Slim\ResponseEmitter;

$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->getBody()->write("Hello, " . $args['name']);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lose this blank line.

$psr17Factory = new Psr17Factory();
$request = ServerRequestCreator::fromGlobals();

/**
* The App::run() Method takes 2 parameters
* In the case of Nyholm/psr7 the Psr17Factory provides all the Http-Factories in one class
* which include ResponseFactoryInterface
* @param ServerRequestInterface An instantiation of a ServerRequest
* @param ResponseFactoryInterface An instantiation of a ResponseFactory
*/
$response = $app->run($request, $psr17Factory);

/**
* Once you have obtained the ResponseInterface from App::run()
* You will need to emit the response by using an emitter of your choice
* We will use Slim ResponseEmitter for this example
* But you could use Zend HttpHandleRunner SapiEmitter or other
*/
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
```

## Example Usage With Zend Diactoros & Zend HttpHandleRunner

Create an index.php file with the following contents:

```php
<?php
require 'vendor/autoload.php';

use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequestFactory;
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;

$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->getBody()->write("Hello, " . $args['name']);
});

$responseFactory = new ResponseFactory();
$serverRequestFactory = new ServerRequestFactory();
$request = ServerRequestFactory::fromGlobals();

/**
* The App::run() Method takes 2 parameters
* @param ServerRequestInterface An instantiation of a ServerRequest
* @param ResponseFactoryInterface An instantiation of a ResponseFactory
*/
$response = $app->run($request, $responseFactory);

/**
* Once you have obtained the ResponseInterface from App::run()
* You will need to emit the response by using an emitter of your choice
* We will use Zend HttpHandleRunner SapiEmitter for this example
*/
$responseEmitter = new SapiEmitter();
$responseEmitter->emit($response);
```

## Example Usage With Slim-Http Decorators, Zend Diactoros & Zend HttpHandleRunner
```php
<?php
require 'vendor/autoload.php';

use Slim\Http\Factory\DecoratedResponseFactory;
use Slim\Http\Decorators\ServerRequestDecorator;
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\StreamFactory;
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;

$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->withJson(['Hello' => 'World']);
});

$responseFactory = new ResponseFactory();
$streamFactory = new StreamFactory();
$decoratedResponseFactory = new DecoratedResponseFactory($responseFactory, $streamFactory);

$serverRequestFactory = new ServerRequestFactory();
$request = ServerRequestFactory::fromGlobals();
$decoratedServerRequest = new ServerRequestDecorator($request);

/**
* The App::run() Method takes 2 parameters
* @param ServerRequestInterface An instantiation of a ServerRequest
* @param ResponseFactoryInterface An instantiation of a ResponseFactory
*/
$response = $app->run($decoratedRequest, $decoratedResponseFactory);

/**
* Once you have obtained the ResponseInterface from App::run()
* You will need to emit the response by using an emitter of your choice
* We will use Zend HttpHandleRunner SapiEmitter for this example
*/
$responseEmitter = new SapiEmitter();
$responseEmitter->emit($response);
```

## Example Usage With Guzzle PSR-7, Guzzle HTTP Factory

Create an index.php file with the following contents:

```php
<?php
require 'vendor/autoload.php';

use GuzzleHttp\Psr7\ServerRequest;
use Http\Factory\Guzzle\ResponseFactory;
use Slim\ResponseEmitter;

$app = new Slim\App();
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->write("Hello, " . $args['name']);
return $response->getBody()->write("Hello, " . $args['name']);
});

$app->run();
$responseFactory = new ResponseFactory();
$request = ServerRequest::fromGlobals();

/**
* The App::run() Method takes 2 parameters
* @param ServerRequestInterface An instantiation of a ServerRequest
* @param ResponseFactoryInterface An instantiation of a ResponseFactory
*/
$response = $app->run($request, $responseFactory);

/**
* Once you have obtained the ResponseInterface from App::run()
* You will need to emit the response by using an emitter of your choice
* We will use Slim ResponseEmitter for this example
*/
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
```

You may quickly test this using the built-in PHP server:
Expand All @@ -45,11 +187,12 @@ Going to http://localhost:8000/hello/world will now display "Hello, world".
For more information on how to configure your web server, see the [Documentation](https://www.slimframework.com/docs/start/web-servers.html).

## Tests

To execute the test suite, you'll need phpunit.
To execute the test suite, you'll need to install all development dependencies.

```bash
$ phpunit
$ git clone https://github.com/slimphp/Slim-Http
$ composer install
$ composer test
```

## Contributing
Expand All @@ -75,6 +218,7 @@ If you discover security related issues, please email security@slimframework.com
- [Josh Lockhart](https://github.com/codeguy)
- [Andrew Smith](https://github.com/silentworks)
- [Rob Allen](https://github.com/akrabat)
- [Pierre Bérubé](https://github.com/l0gicgate)
- [Gabriel Manricks](https://github.com/gmanricks)
- [All Contributors](../../contributors)

Expand Down
Loading