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
Changes from 1 commit
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
139 changes: 65 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,47 @@ Before you can get up and running with Slim you will need to choose a PSR-7 impl
- [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']);
});

/**
* We need to instantiate our factories before instantiating Slim\App
* In the case of Nyholm/psr7 the Psr17Factory provides all the Http-Factories in one class
* which includes ResponseFactoryInterface
*/
$psr17Factory = new Psr17Factory();
$serverRequestFactory = new ServerRequestCreator(
$psr17Factory,
$psr17Factory,
$psr17Factory,
$psr17Factory
);
$request = $serverRequestFactory->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
* The App::__constructor() Method takes 1 mandatory parameter and 2 optional parameters
Copy link
Member

Choose a reason for hiding this comment

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

Method has a lowercase m unless starting a sentence.

* @param ResponseFactoryInterface Any implementation of a ResponseFactory
* @param ContainerInterface|null Any implementation of a Container
* @param array Settings array
*/
$response = $app->run($request, $psr17Factory);
$app = new Slim\App($psr17Factory);
$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.


/**
* 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
* The App::run() Method takes 1 parameters
Copy link
Member

Choose a reason for hiding this comment

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

s/parameters/parameter

* @param ServerRequestInterface An instantiation of a ServerRequest
*/
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$request = $serverRequestFactory->fromGlobals();
$app->run($request, $psr17Factory);
Copy link
Member

Choose a reason for hiding this comment

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

run() no longer takes a ResponseFactory.

```

## Example Usage With Zend Diactoros & Zend HttpHandleRunner

Create an index.php file with the following contents:

## Example Usage With Zend Diactoros & Zend HttpHandleRunner Response Emitter
```php
<?php
require 'vendor/autoload.php';
Expand All @@ -81,32 +74,38 @@ use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequestFactory;
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;

$app = new Slim\App();
$responseFactory = new ResponseFactory();
$serverRequestFactory = new ServerRequestFactory();

/**
* The App::__constructor() Method takes 1 mandatory parameter and 2 optional parameters
* @param ResponseFactoryInterface Any implementation of a ResponseFactory
* @param ContainerInterface|null Any implementation of a Container
* @param array Settings array
*/
$app = new Slim\App($responseFactory);
$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
* The App::handle() Method takes 1 parameters
* Note we are using handle() and not run() since we want to emit the response using Zend's Response Emitter
* @param ServerRequestInterface An instantiation of a ServerRequest
* @param ResponseFactoryInterface An instantiation of a ResponseFactory
*/
$response = $app->run($request, $responseFactory);
$request = ServerRequestFactory::fromGlobals();
$response = $app->handle($request);

/**
* Once you have obtained the ResponseInterface from App::run()
* Once you have obtained the ResponseInterface from App::handle()
* 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
## Example Usage With Slim-Http Decorators and Zend Diactoros
```php
<?php
require 'vendor/autoload.php';
Expand All @@ -116,71 +115,63 @@ 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
* The App::__constructor() Method takes 1 mandatory parameter and 2 optional parameters
* Note that we pass in the decorated response factory which will give us access to the Slim\Http
* decorated Response methods like withJson()
* @param ResponseFactoryInterface Any implementation of a ResponseFactory
* @param ContainerInterface|null Any implementation of a Container
* @param array Settings array
*/
$response = $app->run($decoratedRequest, $decoratedResponseFactory);
$app = new Slim\App($decoratedResponseFactory);
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->withJson(['Hello' => 'World']);
});

/**
* 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
* The App::run() Method takes 1 parameters
* Note that we pass in the decorated server request object which will give us access to the Slim\Http
* decorated ServerRequest methods like withRedirect()
* @param ServerRequestInterface An instantiation of a ServerRequest
*/
$responseEmitter = new SapiEmitter();
$responseEmitter->emit($response);
$request = ServerRequestFactory::fromGlobals();
$decoratedServerRequest = new ServerRequestDecorator($request);
$app->run($decoratedServerRequest);
```

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

Create an index.php file with the following contents:

## Example Usage With Guzzle PSR-7 and Guzzle HTTP Factory
```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->getBody()->write("Hello, " . $args['name']);
});

$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
* The App::__constructor() Method takes 1 mandatory parameter and 2 optional parameters
* @param ResponseFactoryInterface Any implementation of a ResponseFactory
* @param ContainerInterface|null Any implementation of a Container
* @param array Settings array
*/
$response = $app->run($request, $responseFactory);
$app = new Slim\App($responseFactory);
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->getBody()->write("Hello, " . $args['name']);
});

/**
* 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
* The App::run() Method takes 1 parameters
* @param ServerRequestInterface An instantiation of a ServerRequest
*/
$responseEmitter = new ResponseEmitter();
$responseEmitter->emit($response);
$request = ServerRequest::fromGlobals();
$app->run($request);
```

You may quickly test this using the built-in PHP server:
Expand Down