Skip to content

Commit

Permalink
[bc] adds support for PSR-15 interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dwendrich committed Jun 21, 2018
1 parent 655cb00 commit 78863b0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 7.0
- 7.1

before_script:
Expand All @@ -15,4 +14,4 @@ after_success:
- bash <(curl -s https://codecov.io/bash)

notifications:
email: false
email: false
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# expressive-session-middleware
Session handling middleware based on zend-session for use in zend expressive 2.0 applications.
Session handling middleware based on zend-session for use in zend expressive 3 applications.

[![Build Status](https://travis-ci.org/dwendrich/expressive-session-middleware.svg?branch=master)](https://travis-ci.org/dwendrich/expressive-session-middleware)
[![Coverage Status](https://img.shields.io/codecov/c/github/dwendrich/expressive-session-middleware.svg?style=flat)](https://codecov.io/gh/dwendrich/expressive-session-middleware)
[![Latest Stable Version](http://img.shields.io/packagist/v/dwendrich/expressive-session-middleware.svg?style=flat)](https://packagist.org/packages/dwendrich/expressive-session-middleware)

## PSR-15 Support
This version supports [PSR-15](https://www.php-fig.org/psr/psr-15) instead of http-interop/http-middleware interfaces,
as currently implemented by zend expressive 3. For use with older versions of zend expressive, please refer to version
0.1.9.

## Requirements
* PHP 7.0 or above
* PHP 7.1 or above
* [zendframework/zend-session](https://docs.zendframework.com/zend-session/)

## Installation
Install the latest version with composer. For information on how to get composer or how to use it, please refer to [getcomposer.org](http://getcomposer.org).
```sh
$ composer require dwendrich/expressive-soap-middleware
$ composer require dwendrich/expressive-session-middleware
```

If during installation you are prompted to inject `Zend\Session\ConfigProvider` into your configuration, you can simply
ignore and continue without it. All relevant configuration is part of `SessionMiddleware\ConfigProvider`.

As part of a zend-expressive 2.0 application add `SessionMiddleware\ConfigProvider::class` to `config/config.php`:
As part of a zend-expressive application add `SessionMiddleware\ConfigProvider::class` to `config/config.php`:
```php
$aggregator = new ConfigAggregator([

Expand Down Expand Up @@ -49,7 +54,7 @@ You can add the middleware to the file `config/pipeline.php`:
$app->pipe(SessionMiddleware::class);

// Register the routing middleware in the middleware pipeline
$app->pipeRoutingMiddleware();
$app->pipe(\Zend\Expressive\Router\Middleware\RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);
Expand All @@ -58,7 +63,7 @@ Depending on which middleware should get access to the session, you should prepe
Commonly before registering the routing middleware is a good way to go.

This way the middleware is invoked on every request to your application. Since session handling may produce some
overhead which isn't always needed there is an alternative.
overhead, which isn't always needed, there is an alternative:

#### 2. Add the middleware to a specific route
Add a route definition to either `config/routes.php` or a `RouteDelegator` as part of your application:
Expand All @@ -75,7 +80,7 @@ $app->route(
```
This way session handling is bound to a specific path in your application where it may be needed.

For further information on programmatic pipelines and routing in zend expressive 2.0 please refer to the
For further information on programmatic pipelines and routing in zend expressive please refer to the
[documentation](https://docs.zendframework.com/zend-expressive/cookbook/autowiring-routes-and-pipelines/).

## Basic usage
Expand All @@ -92,7 +97,7 @@ by testing against the request attribute:
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
{
$sessionManager = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE, false);

Expand Down Expand Up @@ -180,4 +185,4 @@ return [
],
],
];
```
```
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "dwendrich/expressive-session-middleware",
"description": "Session handling middleware for use with zend expressive 2.0 based on zend-session.",
"description": "Session handling middleware for use with zend expressive 3 based on zend-session.",
"keywords": [
"session handling",
"psr-7",
"psr-15",
"zend",
"expressive",
"middleware"
Expand All @@ -27,10 +27,11 @@
}
},
"require": {
"php": "^7.0",
"php": "^7.1",
"zendframework/zend-session": "^2.7",
"zendframework/zend-servicemanager": "^3.3",
"psr/http-message": "^1.0",
"psr/http-server-middleware": "^1.0",
"http-interop/http-middleware": "^0.4.1"
},
"require-dev": {
Expand Down
10 changes: 6 additions & 4 deletions src/Middleware/SessionMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

declare(strict_types=1);

namespace SessionMiddleware\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as DelegateInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Session\SessionManager;
Expand Down Expand Up @@ -42,13 +44,13 @@ public function __construct(SessionManager $sessionManager)
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
{
// start session handling
$this->sessionManager->start();

// call next middleware in stack and directly return response
return $delegate->process(
return $delegate->handle(
// pass on session manager as request attribute
$request->withAttribute(self::SESSION_ATTRIBUTE, $this->sessionManager)
);
Expand Down
6 changes: 3 additions & 3 deletions test/Middleware/SessionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SessionMiddlewareTest\Middleware;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Server\RequestHandlerInterface as DelegateInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use PHPUnit\Framework\TestCase;
Expand All @@ -27,7 +27,7 @@ public function testSessionStartIsCalledAndRequestIsDelegated()
$response = $this->prophesize(ResponseInterface::class)->reveal();
$delegate = $this->prophesize(DelegateInterface::class);

$delegate->process(Argument::any())->will(function () use(&$invoked, $response) {
$delegate->handle(Argument::any())->will(function () use(&$invoked, $response) {
$invoked = true;
return $response;
});
Expand All @@ -47,4 +47,4 @@ public function testSessionStartIsCalledAndRequestIsDelegated()
$this->assertSame($response, $return);
$this->assertTrue($invoked);
}
}
}

0 comments on commit 78863b0

Please sign in to comment.