-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
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 2f41cd9
update Travis-CI configuration
l0gicgate 72a7abc
update README
l0gicgate 53e3c5e
decouple PSR-7 implementation and remove respond and finalize methods…
l0gicgate 475fbcb
add ResponseEmitter
l0gicgate 39390d8
remove PSR-7 dependant method renderWithBody from AbstractErrorRenderer
l0gicgate 6b45c23
modify base ErrorHandler to use PSR-7 generic methods
l0gicgate 467200d
add ResponseFactory dependency injection in ErrorMiddleware
l0gicgate 2b81dd4
modify request method assertion in MethodOverrideMiddleware
l0gicgate 7e7a3cb
add StreamFactory dependency injection in OutputBufferingMiddleware
l0gicgate 7e5fb3b
refactor entire test suite
l0gicgate 6f4cdf6
add Slim-Http decorators example to README
l0gicgate a97929d
fix mistake in README example
l0gicgate bcff3c5
add phpstan.neon.dist config file
l0gicgate 53cbd6f
fix all PHPStan errors
l0gicgate bc765dd
add ext-simplexml to dev dependencies in composer.json
l0gicgate bc7017f
remove messages in phpstan config
l0gicgate 0008898
remove tests directory from phpstan command in Travis-CI config
l0gicgate 5859306
fix all Slim phpstan errors
l0gicgate 410fc62
fix README comment blocks in examples
l0gicgate 8489a4e
remove unused/reorder imports
l0gicgate 3bbf997
add PSR7ObjectProvider to decouple Tests from specific PSR7 implement…
l0gicgate 7daa6ed
fix PHPCS errors
l0gicgate dd12345
add PHP 7.3 to Travis-CI config
l0gicgate 24c6867
add Guzzle PSR7 example to README
l0gicgate bd85e1b
add test coverage clover to Travis-CI config
l0gicgate 6cdb1cb
fix after_success script for php-coveralls in Travis-CI config
l0gicgate f13c497
add code coverage driver to PHP 7.1 build
l0gicgate 166ced0
fix coverage clover parameter in Travis-CI config
l0gicgate 6d8d74f
fix Nyholm/psr7 README example
l0gicgate b842ba1
Merge branch '4.x' of https://github.com/slimphp/Slim into PSR7-Decou…
l0gicgate 9f75486
fix param type in InvocationStrategyInterface
l0gicgate 01ccbfa
fix PHPStan errors
l0gicgate 340cadd
fix PHPStan error
l0gicgate d575b9a
fix PHPStan error
l0gicgate b4c57be
fix PHPStan errors
l0gicgate c168c4a
rename base Test class to TestCase
l0gicgate 2ffaee9
change App constructor function signature to take in ResponseFactoryI…
l0gicgate c7aa199
fix README to reflect changes made to App constructor and run methods
l0gicgate 3ea27ba
fix import ordering back to alphabetical in RouterInterface
l0gicgate 8d454e2
add comment for variable reassignment in Router to avoid PHPStan warning
l0gicgate 570872d
remove unused commented statement in AppTest::tearDownAfterClass
l0gicgate ba1621b
add comment for ignored error in PHPStan config
l0gicgate f909bb1
reorder imports alphabetically in multiple files
l0gicgate 3f8978d
fix line length in App
l0gicgate 1a0d4ba
fix README errors
l0gicgate e2d7633
remove responseChunkSize setting from App
l0gicgate a62881b
remove useless parameter reassignment to silence PHPStan in Router
l0gicgate 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 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 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 |
---|---|---|
|
@@ -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. | ||
|
||
## 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']); | ||
}); | ||
|
||
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. 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: | ||
|
@@ -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 | ||
|
@@ -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) | ||
|
||
|
Oops, something went wrong.
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.
Is it important to mention it here?
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.
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.