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 39 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 14 additions & 12 deletions .travis.yml
Expand Up @@ -6,24 +6,26 @@ dist: trusty

matrix:
include:
- php: 7.0
- 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 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
159 changes: 150 additions & 9 deletions README.md
Expand Up @@ -15,24 +15,163 @@ 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

Create an index.php file with the following contents:
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
```php
<?php
require 'vendor/autoload.php';

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

/**
* 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
);

/**
* 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
*/
$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.


/**
* 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
*/
$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 Response Emitter
```php
<?php
require 'vendor/autoload.php';

$app = new Slim\App();
use Zend\Diactoros\ResponseFactory;
use Zend\Diactoros\ServerRequestFactory;
use Zend\HttpHandlerRunner\Emitter\SapiEmitter;

$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->write("Hello, " . $args['name']);
return $response->getBody()->write("Hello, " . $args['name']);
});

$app->run();
/**
* 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
*/
$request = ServerRequestFactory::fromGlobals();
$response = $app->handle($request);

/**
* 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 and Zend Diactoros
```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;

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

/**
* 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
*/
$app = new Slim\App($decoratedResponseFactory);
$app->get('/hello/{name}', function ($request, $response, $args) {
return $response->withJson(['Hello' => 'World']);
});

/**
* 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
*/
$request = ServerRequestFactory::fromGlobals();
$decoratedServerRequest = new ServerRequestDecorator($request);
$app->run($decoratedServerRequest);
```

## 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;

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

/**
* The App::run() Method takes 1 parameters
* @param ServerRequestInterface An instantiation of a ServerRequest
*/
$request = ServerRequest::fromGlobals();
$app->run($request);
```

You may quickly test this using the built-in PHP server:
Expand All @@ -45,11 +184,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 +215,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